Question

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

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?

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/

Était-ce utile?

La solution 2

str1[size] = '\0';

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

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.

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.

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

Autres conseils

The error is with:

char *ptr3;
char *ptr4;

You should initialize them to 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:

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

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


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:

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.

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 :

Breakpoint 1, copyThis (str1=0x7fffffffe060 "\001", str2=0x4007e4 "My Name", size=8) at prog1.c:22 22 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

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;
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top