欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

realloc一个有意思的测试

程序员文章站 2022-03-01 17:28:50
...

百度百科:

      realloc 先判断当前的指针是否有足够的连续空间,如果有,扩大mem_address指向的地址,并且将mem_address返回,如果空间不够,先按照newsize指定的大小分配空间,将原有数据从头到尾拷贝到新分配的内存区域,而后释放原来mem_address所指内存区域(注意:原来指针是自动释放,不需要使用free),同时返回新分配的内存区域的首地址。即重新分配存储器块的地址。 

在本人的电脑上做的测试,gcc版本

[email protected]:~/sources/workbox/0001_app/telproxy_server$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/7/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 7.4.0-1ubuntu1~18.04.1' --with-bugurl=file:///usr/share/doc/gcc-7/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++ --prefix=/usr --with-gcc-major-version-only --program-suffix=-7 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --enable-default-pie --with-system-zlib --with-target-system-zlib --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 7.4.0 (Ubuntu 7.4.0-1ubuntu1~18.04.1)

 百度了一下gcc版本,发现7 .4 .0也不是很老,2018年12月6号发布。

GCC 9.2 released [2019-08-12]
PRU support [2019-06-12]
GCC support for TI PRU I/O processors has been added.
GCC 9.1 released [2019-05-03]
GNU Tools Cauldron 2019 [2019-04-15]
Will be held in Montréal, Canada, September 12-15 2019
GCC 8.3 released [2019-02-22]
AMD GCN support [2019-01-17]
GCC support for AMD GCN Fiji and Vega GPUs has been added. This back end was contributed by Mentor Graphics.
GCC 7.4 released [2018-12-06]
D front end added [2018-10-29]
The D programming language front end has been added to GCC. This front end was contributed by Iain Buclaw.
GCC 6.5 released [2018-10-26]

编译过程 gcc test.c 

测试一: 


#include<stdio.h>
#include<stdlib.h>

int main(int argc, char** argv)
{

        char* str = (char*)malloc (16);
        snprintf(str, 16, "%s", "yangpan");

        printf ("ptr:%p   %s\n",str,  str);

        char* nstr = realloc(str, 1024*1024);
        printf ("ptr:%p   %s\n",nstr,  nstr);

        free (str);
        free (str);
        free (str);
        free (str);
        free (str);
        free (nstr);

        printf ("ptr:%p   %s\n",str,  str);
        snprintf(str, 64, "%s", "hbxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
        printf ("ptr:%p   %s\n",str,  str);
        return 0;
}

居然我的电脑编译后能正常运行,不会蹦掉。哈哈。下面是运行结果

ptr:0x55f589948260   yangpan
ptr:0x7f6be433c010   yangpan
ptr:0x55f589948260   ` 
ptr:0x55f589948260   hbxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

测试二:

再进行测试,简单点,敲代码的方式简单的

int main(int argc, char** argv)
{

        char* str = (char*)malloc (16);
        if(!str)
        {
            printf("malloc err\n");
        }
        free (str);
        free (str);
        free (str);
        free (str);
        free (str);
        return 0;
}

上面代码运行正常运行。怎么才能让malloc崩掉呢?很烦恼。

再进行测试三:

#include<stdio.h>
#include<stdlib.h>


int main(int argc, char** argv)
{

        char* str = (char*)malloc (1024+8);

        snprintf(str, 64, "%s", "hbxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
        printf ("ptr:%p   %s\n",str,  str);

        str[1024+80] = 'y';
        printf ("str[1024+80]:%c\n", str[1024+80]);

        free (str);
        free (str);
        free (str);
        free (str);
        free (str);
        return 0;
}

诶,终于崩掉了,运行结果

[email protected]:~/sources/workbox/0001_app/telproxy_server$ ./a.out 
ptr:0x55af8c7f0260   hbxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
str[1024+80]:y
double free or corruption (!prev)
Aborted (core dumped)

再进行测试四:

#include<stdio.h>
#include<stdlib.h>


int main(int argc, char** argv)
{

        char* str = (char*)malloc (1024+8);

        snprintf(str, 64, "%s", "hbxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
        printf ("ptr:%p   %s\n",str,  str);

        str[1024+80] = 'y';
        printf ("str[1024+80]:%c\n", str[1024+80]);

        free (str);
        free (str);
        free (str);
        free (str);
        free (str);
        return 0;
}

运行有不崩了,运行结果

[email protected]:~/sources/workbox/0001_app/telproxy_server$ ./a.out 
ptr:0x55e4cb149260   hbxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
str[1024+80]:y

看来1024+8是一个坎。待续.....