简体   繁体   English

使用malloc()和free()函数时出错

[英]error when using malloc() and free() function

i want to create my own strcpy() function without include <string.h> .i have read that whenever we call malloc() function to allocate memory,we must freeing the memory that we have allocate using free() function,but why this program give me a strange error,i never seen this type of error before and why this program doesn't give me the exact destination string. 我想创建自己的strcpy()函数而不include <string.h> 。我已经阅读到每当调用malloc()函数分配内存时,都必须使用free()函数释放已分配的内存,但是为什么这个程序给我一个奇怪的错误,我以前从未见过这种类型的错误,以及为什么这个程序没有给我确切的目标字符串。

this is the program: 这是程序:

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

char *my_gets(char *string);
char *coppy(char *dest,char*src);
int len(char *string);

int main (void){
    char *src,*dest;

    src=(char *)malloc(len(src)+1);
    printf("enter string source:");my_gets(src);

    printf("length source=%d\n",len(src));

    dest=(char *)malloc(len(src)+1);
    coppy(dest,src);
    printf("destination string:%s\n",dest);

    free(src);
    free(dest);
    return 0;
}

char *my_gets(char *string){
   char ch;

   while((*string=getchar()) !='\n'){
        string++;
   }
   if(string[len(string)-1]=='\n'){
    string[len(string)-1]='\0';
    }
    return string;
 }

char *coppy(char *dest,char *src){
   while(*src!='\0'){
    *dest++=*src++;
   }
   *dest='\0';
   return dest;
 }

int len(char *string){
   int i=0;

   while(*string!='\0'){
    string++;
    i++;
   }
   return i;
 }

this the error : 这个错误:

enter string source:i want to create my own strcpy function but i got an a strange error
length source=67
destination string:i want to crI
*** Error in `./program': free(): invalid next size (normal): 0x086b4018 ***
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(+0x767e2)[0xb76457e2]
/lib/i386-linux-gnu/libc.so.6(+0x77530)[0xb7646530]
./program[0x804855f]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf5)[0xb75e8935]

./program[0x80483d1] ======= Memory map: ======== 08048000-08049000 r-xp 00000000 08:01 4330274 /home/harianja/LUNDU/Lundu/coding/Cprogram/program 08049000-0804a000 r--p 00000000 08:01 4330274 /home/harianja/LUNDU/Lundu/coding/Cprogram/program ./program[0x80483d1] =======内存映射:======== 08048000-08049000 r-xp 00000000 08:01 4330274 / home / harianja / LUNDU / Lundu / coding / Cprogram / program 08049000 -0804a000 r–p 00000000 08:01 4330274 / home / harianja / LUNDU / Lundu / coding / Cprogram / program
0804a000-0804b000 rw-p 00001000 08:01 4330274 /home/harianja/LUNDU/Lundu/coding/Cprogram/program 0804a000-0804b000 rw-p 00001000 08:01 4330274 / home / harianja / LUNDU / Lundu / coding / Cprogram / program
086b4000-086d5000 rw-p 00000000 00:00 0 [heap] 086b4000-086d5000 rw-p 00000000 00:00 0 [堆]
b7593000-b75ae000 r-xp 00000000 08:01 4195166 /lib/i386-linux-gnu/libgcc_s.so.1 b7593000-b75ae000 r-xp 00000000 08:01 4195166 /lib/i386-linux-gnu/libgcc_s.so.1
b75ae000-b75af000 r--p 0001a000 08:01 4195166 /lib/i386-linux-gnu/libgcc_s.so.1 b75ae000-b75af000 r--p 0001a000 08:01 4195166 /lib/i386-linux-gnu/libgcc_s.so.1
b75af000-b75b0000 rw-p 0001b000 08:01 4195166 /lib/i386-linux-gnu/libgcc_s.so.1 b75af000-b75b0000 rw-p 0001b000 08:01 4195166 /lib/i386-linux-gnu/libgcc_s.so.1
b75ce000-b75cf000 rw-p 00000000 00:00 0 b75ce000-b75cf000 rw-p 00000000 00:00 0
b75cf000-b777d000 r-xp 00000000 08:01 4208177 /lib/i386-linux-gnu/libc-2.17.so b75cf000-b777d000 r-xp 00000000 08:01 4208177 /lib/i386-linux-gnu/libc-2.17.so
b777d000-b777f000 r--p 001ae000 08:01 4208177 /lib/i386-linux-gnu/libc-2.17.so b777d000-b777f000 r--p 001ae000 08:01 4208177 /lib/i386-linux-gnu/libc-2.17.so
b777f000-b7780000 rw-p 001b0000 08:01 4208177 /lib/i386-linux-gnu/libc-2.17.so b777f000-b7780000 rw-p 001b0000 08:01 4208177 /lib/i386-linux-gnu/libc-2.17.so
b7780000-b7783000 rw-p 00000000 00:00 0 b7780000-b7783000 rw-p 00000000 00:00 0
b779e000-b77a3000 rw-p 00000000 00:00 0 b779e000-b77a3000 rw-p 00000000 00:00 0
b77a3000-b77a4000 r-xp 00000000 00:00 0 [vdso] b77a3000-b77a4000 r-xp 00000000 00:00 0 [vdso]
b77a4000-b77c4000 r-xp 00000000 08:01 4208178 /lib/i386-linux-gnu/ld-2.17.so b77a4000-b77c4000 r-xp 00000000 08:01 4208178 /lib/i386-linux-gnu/ld-2.17.so
b77c4000-b77c5000 r--p 0001f000 08:01 4208178 /lib/i386-linux-gnu/ld-2.17.so b77c5000-b77c6000 rw-p 00020000 08:01 4208178 /lib/i386-linux-gnu/ld-2.17.so bf863000-bf884000 rw-p 00000000 00:00 0 [stack] Aborted b77c4000-b77c5000 r--p 0001f000 08:01 4208178 /lib/i386-linux-gnu/ld-2.17.so b77c5000-b77c6000 rw-p 00020000 08:01 4208178 /lib/i386-linux-gnu/ld-2.17。所以bf863000-bf884000 rw-p 00000000 00:00 0 [堆栈]已中止

This: 这个:

char *src,*dest;

src=(char *)malloc(len(src)+1);

is undefined behavior, src is an uninitialized pointer which you can't pass to len() since it will read from it. 是未定义的行为, src是未初始化的指针,您无法将其传递给len()因为它将从中读取该指针。

Also, len() is a re-implementation of strlen() but perhaps you knew that. 另外, len()strlen()的重新实现,但是您也许知道这一点。

This line: 这行:

src=(char *)malloc(len(src)+1); 

Will error, as src has not been initialized yet. 将会出错,因为src尚未初始化。 Initialize it to some other value relevant to what it needs to be used for. 将其初始化为与需要使用的其他值有关。

src=malloc(100); //for lack of any better value, used 100  

Your function len() will error out if you pass it an uninitialized pointer . 如果您向函数len()传递未初始化的指针,则会出错。
And do not cast the return of malloc() 并且不要转换 malloc()的返回值

When you malloc in your first line, you find the src size with len() but src is not initialized. 当您在第一行中进行malloc分配时,可以通过len()找到src大小,但是src尚未初始化。 So you malloc trash. 因此,您可以分配垃圾。

char *src,*dest;
src=(char *)malloc(len(src)+1);

You forgot to add -fclairvoyance compiler option. 您忘记添加-fclairvoyance编译器选项。 Notice that clairvoyance is still in beta and is not guaranteed to produce correct results. 请注意,透视透视仍处于beta状态,不能保证产生正确的结果。 To work around this limitation, try to estimate max string length up front: 要变通解决此限制,请尝试预先估算最大字符串长度:

src=malloc(100);

Note you should not cast the result of malloc . 注意,您不应该malloc的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM