3

I'm trying an example

int main(){
    int count = 5;
    char *a = "JOSUA";
    char *c = "JANETALAE";
    char *b = NULL;
    char *d = NULL;
    b = (char *)malloc(sizeof(char)*5);
    b = a;
    if(b == NULL){
        printf("\n malloc failed");
    }
    printf("\nChar is:%s\n",b);
    d = (char *)realloc(b,sizeof(char) * 9);
    if(d == NULL){
        printf("\n realloc failed");
    }
    else{
        b = d;
        b = c;                                                                                                                                                                             
    }
    printf("\nChar is:%s\n",b);
    return 0;
}

I'm trying a sample program to allocate and reallocate memory. But i couldn't reallocate the memory and getting a compilation break as below.

Char is:JOSUA
*** glibc detected *** ./realloc: realloc(): invalid pointer: 0x080485b4 ***
======= Backtrace: ========= /lib/libc.so.6[0x88ffd1] /lib/libc.so.6(realloc+0x2cf)[0x89613f] /lib/libc.so.6(realloc+0x2e7)[0x896157] ./realloc[0x80484aa] /lib/libc.so.6(__libc_start_main+0xe6)[0x836e16] ./realloc[0x8048391]
======= Memory map: ======== 007fc000-0081c000 r-xp 00000000 fd:00 659977     /lib/ld-2.12.90.so 0081c000-0081d000 r--p 0001f000 fd:00 659977     /lib/ld-2.12.90.so 0081d000-0081e000 rw-p 00020000 fd:00 659977     /lib/ld-2.12.90.so 00820000-009ad000 r-xp 00000000 fd:00 660018     /lib/libc-2.12.90.so 009ad000-009af000 r--p 0018c000 fd:00 660018     /lib/libc-2.12.90.so 009af000-009b0000 rw-p 0018e000 fd:00 660018     /lib/libc-2.12.90.so 009b0000-009b3000 rw-p 00000000 00:00 0  00a29000-00a45000 r-xp 00000000 fd:00 660039     /lib/libgcc_s-4.5.1-20100924.so.1 00a45000-00a46000 rw-p 0001b000 fd:00 660039     /lib/libgcc_s-4.5.1-20100924.so.1 00a84000-00a85000 r-xp 00000000 00:00 0          [vdso] 08048000-08049000 r-xp 00000000 fd:02 9440701    /home/user/beata/c_samples/realloc 08049000-0804a000 rw-p 00000000 fd:02 9440701    /home/user/beata/c_samples/realloc 092ac000-092cd000 rw-p 00000000 00:00 0          [heap] b78bc000-b78bd000 rw-p 00000000 00:00 0  b78d3000-b78d5000 rw-p 00000000 00:00 0  bfbd8000-bfbf9000 rw-p 00000000 00:00 0          [stack] Aborted (core dumped)

Could not understand why the compilation break happened.

Will Vousden
  • 32,488
  • 9
  • 84
  • 95
Angus
  • 12,133
  • 29
  • 96
  • 151
  • 2
    You allocate memory for the `b` variable, but then you assign the `b` variable to your string literal `a`. You will have to copy the contents of `a` into `b`. – Cyclonecode Nov 27 '12 at 14:45
  • Note, you're not `malloc`ing room for the `\0` that comes at the end of every string. Even if you fix the problem of not being able to copy them around, you're going to run into issues with them *not being strings anymore*. – cHao Nov 27 '12 at 14:47
  • I am reading your code and all I can think is "Does this guy even know what `b = a` means ?". Twice you overwrite the value of b, without further use. – Eregrith Nov 27 '12 at 14:50
  • 1
    The error you get is not a compilation error, it is a runtime error... – Lindydancer Nov 27 '12 at 14:51
  • 2
    Stop casting `void*` to other pointer types explicitly. This is not needed and may even be harmful! C is not C++, damnit. –  Nov 27 '12 at 14:52
  • 1
    @Eregrith: He's trying to copy the string at `a` into `b`. It's a sensible thing to try if you don't know any better (and are taught to think of `char*`s as strings), but yeah -- it doesn't do what he's thinking it does. – cHao Nov 27 '12 at 14:52
  • Yes. Also he declares `count` and never uses it. My idea is that OP did not really try that hard before posting a question. – Eregrith Nov 27 '12 at 14:54
  • I apologise tat i did this without thinking that b = a will make the pointer b to point to the memory of a. and the memory address of the b which is allocated will be lost. Thanks for your comments. – Angus Nov 27 '12 at 15:05

7 Answers7

4

You can't use realloc on a static-memory variable (which is a, actually):

b = (char *)malloc(sizeof(char)*5);
b = a;
Truncarlos
  • 175
  • 2
  • 7
  • 3
    Also is more useful to use fprintf(stderr, "..."); when debugging because it waits until the text is printed. – Carlos Vega Nov 27 '12 at 14:48
  • adding to the answer, `char *a = "JOSUA";` is basically a `const char *` so b=a; then reallocating b is an error. – fadedreamz Nov 27 '12 at 14:49
2

maybe replacing:

b=a;

with:

strcpy(b,a);

will get you heading in the right direction. I think maybe you think thats what b=a; is actually doing?

but make sure you have allocated enough memory on the heap, i.e. don't forget the '\0' implicit at the end of the string, e.g.

b = (char *)malloc(sizeof(char)*5);

should be:

b = malloc(6);

if you want to fit "JOSUA" in it ("JOSUA" is actually "JOSUA\0")

bph
  • 10,728
  • 15
  • 60
  • 135
1

You are passing an invalid pointer to realloc(), just as the error says.

It's from this line:

b = a;

where you overwrite the return value of malloc() with the address of a string literal. You cannot pass that to realloc().

Your overall logic in this program is very broken; if malloc() fails, there's little reason to believe realloc() will succeed (assuming the above line is an editing error and shouldn't be there).

Also, this is a run-time error, not a "compilation error" as you say.

Further, the typical errors apply:

  1. In C, don't cast the return value of malloc(). This applies just as well to realloc(), too.
  2. Don't use sizeof (char), it's just a very cumbersome way to write 1.
Community
  • 1
  • 1
unwind
  • 391,730
  • 64
  • 469
  • 606
1
 b = (char *)malloc(sizeof(char)*5);
 b = a;

There you just lost the address returned by malloc.

You need to pass the address returned by malloc to realloc if you pass any other address it results in Undefined Behavior and that is what your code does.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
1

You try to realloc memory that has not been malloc'd :

  9     b = (char *)malloc(sizeof(char)*5);
 10     b = a;

Your line 9 here is rendered useless by the 10th, and the adress returned by malloc is lost. Also, do not cast the return of malloc as this can hide other errors.

Community
  • 1
  • 1
Eregrith
  • 4,263
  • 18
  • 39
1

This is the line:

10     b = a;

Here, you are setting the pointer b to the address stored in the pointer a. So b now points to the string "JOSHUA".

When you try to realloc b, you are feeding it that address, which was not initially allocated by malloc. what you want to do is copy the string from a to b:

strcpy(b,a);

However, you will need to increase the size of the memory pointed to by b.

Michael McGuire
  • 1,034
  • 9
  • 20
1

Problem is b = (char *) malloc(sizeof(char) * 5); and then immediately you are changing the address pointing by b by assigning base address to which a is pointing to. i.e. b = a; /* which is the error */. For the re-alloc you have to pass the address returned by the malloc, not the base address to which the a pointing to.

Gautham Kantharaju
  • 1,735
  • 1
  • 21
  • 24