我在代码下面被中止(核心转储)。

时间:2022-09-02 11:07:11

Hi in my below code for testing realloc() I am getting the error Aborted (Core dumped).

在下面的代码中,我要测试realloc(),我将会得到错误中止(核心转储)。

The output of the code is below

下面是代码的输出。

$ ./realloc
My Name // <<-- This works
Aborted (core dumped) // <<-- why this error occur?

calling to copyThis() function 1st time gives the correct result and produce no error. In the same way if I call copyThis() for the second time, it make error. I can not understand why is it happening. Can any body point me out where the problem is and what the tweak should I do?

调用copyThis()函数第一次给出了正确的结果,并且不会产生错误。同样地,如果我第二次调用copyThis(),它会出错。我不明白为什么会这样。有没有人能指出我的问题在哪,我该怎么办?

The code is below

下面的代码是

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

char *copyThis(char *str1, const char *str2, size_t size);

int main(void)
{
    char *ptr1 = "My Name ";
    char *ptr2 = "is Alice";
    char *ptr3;
    char *ptr4;

    ptr3 = copyThis(ptr3, ptr1, strlen(ptr1) + 1); // This works
    printf("%s\n", ptr3);

    ptr4 = copyThis(ptr4, ptr2, strlen(ptr2) +1); // this line make Aborted (core dumped)
    printf("%s\n", ptr4);
}

char *copyThis(char *str1, const char *str2, size_t size)
{
    str1 = (char *) realloc(str1, size);
    strncat(str1, str2, strlen(str2));
    str1[size] = '\0';
    return str1;
}

NOTE : Please point me any good tutorial site that can help me catch string operations well in c/

注意:请告诉我任何一个好的教程网站,它可以帮助我在c/。

3 个解决方案

#1


1  

str1[size] = '\0';

writes beyond the allocated memory, thus your program crashes. You should allocate one more for the 0 character.

写入超出分配的内存,从而导致程序崩溃。您应该为0字符再分配一个。

The other error is that you call your function fro char* as first argument (ptr3 and ptr4) that are not initialized. This has undefined behavior, anything can happen with such code.

另一个错误是,您将您的函数调用char*作为第一个未初始化的参数(ptr3和ptr4)。这是没有定义的行为,任何事情都可能发生在这样的代码中。

A proper way would be to check if that argument is 0 and then just call malloc with the appropriate size. But for such an approach to work you would have to properly initialize these variables with 0, which you should do, anyhow.

正确的方法是检查这个参数是否为0,然后用适当的大小调用malloc。但是对于这样的工作方法,您必须使用0来正确地初始化这些变量,无论如何,您都应该这样做。

Also, don't cast the return of malloc and relatives, this might hide subtle bugs.

此外,不要抛弃malloc和亲属的返回,这可能会隐藏一些微妙的bug。

#2


4  

The error is with:

的错误是:

char *ptr3;
char *ptr4;

You should initialize them to NULL:

您应该将它们初始化为NULL:

char *ptr3 = NULL;
char *ptr4 = NULL;

Since ptr3 and ptr4 are not NULL, realloc assumes they were pointers to valid memory. From man realloc:

由于ptr3和ptr4不是NULL, realloc假定它们是指向有效内存的指针。从男人realloc:

Unless ptr is NULL, it must have been returned by an earlier call to malloc(), calloc() or realloc().

除非ptr是空的,否则它必须通过先前对malloc()、calloc()或realloc()的调用返回。

Since those pointers are, instead, pointing to random addresses in memory, realloc will get confused when actually using them.

由于这些指针是指向内存中的随机地址,realloc在实际使用它们时会感到困惑。


As a more general note, in cases like this it is very helpful to enable additional diagnostics when compiling your code. For example, if you use gcc and if you compile your code with -Wall, the following warnings are emitted:

作为一个更通用的说明,在这样的情况下,在编译代码时启用额外的诊断是非常有用的。例如,如果您使用gcc,如果您用-Wall编译代码,则发出以下警告:

file.c:19:1: warning: control reaches end of non-void function [-Wreturn-type]
file.c:14:10: warning: ‘ptr3’ is used uninitialized in this function [-Wuninitialized]
file.c:17:10: warning: ‘ptr4’ is used uninitialized in this function [-Wuninitialized]

How to enable those extra warnings depends on what IDE you use for developing, if any; otherwise, if you use the commandline, just add -Wall to the call to gcc.

如何启用这些额外的警告取决于您用于开发的IDE,如果有的话;否则,如果您使用命令行,只需在调用gcc时添加-Wall。

#3


0  

The problem with your code is that you have not initialized the pointers and when you are trying to use realloc it has garbage value and gdb shows :

您的代码的问题是,您没有初始化指针,当您尝试使用realloc时,它具有垃圾值,gdb显示:

Breakpoint 1, copyThis (str1=0x7fffffffe060 "\001", str2=0x4007e4 "My Name", size=8) at prog1.c:22 22 str1 = (char *) realloc(str1,size); (gdb) n

断点1,copyThis (str1=0x7fffffffe060“\001”,str2=0x4007e4“我的名字”,大小=8)在prog1。c: 2222 str1 = (char *) realloc(str1,size);(gdb)n

Program received signal SIGSEGV, Segmentation fault. 0x00007ffff7a95b54 in realloc () from /lib/x86_64-linux-gnu/libc.so.6

程序接收信号SIGSEGV,分割故障。在realloc()中从/lib/x86_64- linuxgnu /libc.so.6中。

Due to the garbage address it was failing. So whenever you use pointers always try to initialize them before using.

由于垃圾处理,它失败了。所以无论何时使用指针,总是尝试在使用之前初始化它们。

So the correct program is :

所以正确的程序是:

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

char *copyThis(char *str1, const char *str2, size_t size);

int main(void)
{
    char *ptr1 = "My Name";
    char *ptr2 = "is Alice";
    char *ptr3 = NULL;
   char *ptr4 = NULL;
    ptr3 = copyThis(ptr3, ptr1, sizeof(ptr1) ); // This works
    printf("%s\n", ptr3);
    ptr4 = copyThis(ptr4, ptr2, sizeof(ptr2) ); // this also works
    printf("%s\n", ptr4);
}

char *copyThis(char *str1, const char *str2, size_t size)
{
    str1 = (char *) realloc(str1,size);
     strncat(str1, str2, strlen(str2));
    str1[size] = '\0';
    return str1;
}

#1


1  

str1[size] = '\0';

writes beyond the allocated memory, thus your program crashes. You should allocate one more for the 0 character.

写入超出分配的内存,从而导致程序崩溃。您应该为0字符再分配一个。

The other error is that you call your function fro char* as first argument (ptr3 and ptr4) that are not initialized. This has undefined behavior, anything can happen with such code.

另一个错误是,您将您的函数调用char*作为第一个未初始化的参数(ptr3和ptr4)。这是没有定义的行为,任何事情都可能发生在这样的代码中。

A proper way would be to check if that argument is 0 and then just call malloc with the appropriate size. But for such an approach to work you would have to properly initialize these variables with 0, which you should do, anyhow.

正确的方法是检查这个参数是否为0,然后用适当的大小调用malloc。但是对于这样的工作方法,您必须使用0来正确地初始化这些变量,无论如何,您都应该这样做。

Also, don't cast the return of malloc and relatives, this might hide subtle bugs.

此外,不要抛弃malloc和亲属的返回,这可能会隐藏一些微妙的bug。

#2


4  

The error is with:

的错误是:

char *ptr3;
char *ptr4;

You should initialize them to NULL:

您应该将它们初始化为NULL:

char *ptr3 = NULL;
char *ptr4 = NULL;

Since ptr3 and ptr4 are not NULL, realloc assumes they were pointers to valid memory. From man realloc:

由于ptr3和ptr4不是NULL, realloc假定它们是指向有效内存的指针。从男人realloc:

Unless ptr is NULL, it must have been returned by an earlier call to malloc(), calloc() or realloc().

除非ptr是空的,否则它必须通过先前对malloc()、calloc()或realloc()的调用返回。

Since those pointers are, instead, pointing to random addresses in memory, realloc will get confused when actually using them.

由于这些指针是指向内存中的随机地址,realloc在实际使用它们时会感到困惑。


As a more general note, in cases like this it is very helpful to enable additional diagnostics when compiling your code. For example, if you use gcc and if you compile your code with -Wall, the following warnings are emitted:

作为一个更通用的说明,在这样的情况下,在编译代码时启用额外的诊断是非常有用的。例如,如果您使用gcc,如果您用-Wall编译代码,则发出以下警告:

file.c:19:1: warning: control reaches end of non-void function [-Wreturn-type]
file.c:14:10: warning: ‘ptr3’ is used uninitialized in this function [-Wuninitialized]
file.c:17:10: warning: ‘ptr4’ is used uninitialized in this function [-Wuninitialized]

How to enable those extra warnings depends on what IDE you use for developing, if any; otherwise, if you use the commandline, just add -Wall to the call to gcc.

如何启用这些额外的警告取决于您用于开发的IDE,如果有的话;否则,如果您使用命令行,只需在调用gcc时添加-Wall。

#3


0  

The problem with your code is that you have not initialized the pointers and when you are trying to use realloc it has garbage value and gdb shows :

您的代码的问题是,您没有初始化指针,当您尝试使用realloc时,它具有垃圾值,gdb显示:

Breakpoint 1, copyThis (str1=0x7fffffffe060 "\001", str2=0x4007e4 "My Name", size=8) at prog1.c:22 22 str1 = (char *) realloc(str1,size); (gdb) n

断点1,copyThis (str1=0x7fffffffe060“\001”,str2=0x4007e4“我的名字”,大小=8)在prog1。c: 2222 str1 = (char *) realloc(str1,size);(gdb)n

Program received signal SIGSEGV, Segmentation fault. 0x00007ffff7a95b54 in realloc () from /lib/x86_64-linux-gnu/libc.so.6

程序接收信号SIGSEGV,分割故障。在realloc()中从/lib/x86_64- linuxgnu /libc.so.6中。

Due to the garbage address it was failing. So whenever you use pointers always try to initialize them before using.

由于垃圾处理,它失败了。所以无论何时使用指针,总是尝试在使用之前初始化它们。

So the correct program is :

所以正确的程序是:

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

char *copyThis(char *str1, const char *str2, size_t size);

int main(void)
{
    char *ptr1 = "My Name";
    char *ptr2 = "is Alice";
    char *ptr3 = NULL;
   char *ptr4 = NULL;
    ptr3 = copyThis(ptr3, ptr1, sizeof(ptr1) ); // This works
    printf("%s\n", ptr3);
    ptr4 = copyThis(ptr4, ptr2, sizeof(ptr2) ); // this also works
    printf("%s\n", ptr4);
}

char *copyThis(char *str1, const char *str2, size_t size)
{
    str1 = (char *) realloc(str1,size);
     strncat(str1, str2, strlen(str2));
    str1[size] = '\0';
    return str1;
}