Question

i am writing a file copy program,in which i faced difficulty regarding realloc(). Please look at the following snippet (which i write to understand the working of realloc()):-

int main(){
    char *p =(char *) malloc ( 10 ),*t;
    p = "this is";
    if (strlen (p)==7)
    {
        t = realloc ( p,14);
        if ( t==NULL)
        {
            printf ("no no\n");
        }
    }
    printf("%p\n%p\n%d",p,t,strlen(p));
    free (p);
    free (t);
    return 1;
}

output

no no      //because realloc () is unable to reallocate  the memory
00450357   //address of p
00000000   //address of t

so why realloc() is unable to reallocate the memory and assign it (its address) to t?

EDIT i am using Code Blocks in windows.

Was it helpful?

Solution

You have overwritten the value returned by malloc by address of a static string. Then realloc receives the address of the static string as parameter to reallocate.

char *p =(char *) malloc ( 10 ),*t;
p = "this is";
t = realloc ( p,14);

What you probably wanted is:

char *p =(char *) malloc ( 10 ),*t;
strcpy(p, "this is");
t = realloc ( p,14);

OTHER TIPS

After allocating to p you are doing

p = "this is";

which overrides the value returned by malloc(). And as memory pointed by p is not free-able the realloc() fails.

To copy the string do

strcpy(p, "this is");

although check p has enough memory allocated, otherwise use

strncpy(p, "this is", length_of_p);

The man page of realloc states that unless the first argument of realloc is NULL, it must have been returned by an earlier call to malloc, calloc, or realloc.

t = realloc (p, 14);

The above statement passes p to realloc, which points to the string literal "this is", which has static storage duration and is read-only though not const qualified. As such, your program invokes undefined behaviour. (It crashed on my machine due to segfault.)

Also, you lose handle on the dynamically allocated memory when you reassign p the address of the first element of the string literal, causing memory leak.

char *p =(char *) malloc ( 10 ),*t;
p = "this is";

What you actually need to do is copy the contents of the string literal to dynamically allocated buffer using strcpy.

// include the required headers

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

// explicitly state void in the parameter list of main
int main(void) {
    // do not cast the result of malloc
    char *p = malloc(10 * sizeof *p);
    char *t;
    if(p == NULL) {
        printf("malloc failed\n");
        return 1;
    }
    // beware of buffer overrun by strcpy
    // consider using the safer strncpy if it suits
    p = strcpy(p, "this is");
    if(strlen(p) == 7) {
        // save the result of realloc in t
        // in case, realloc fails to allocate
        t = realloc(p, 14);
        if(t == NULL) {
            printf("realloc failed\n");
            // handle it
            // p points to old buffer
        }
        else {
            t = NULL;
            // p points to new buffer
        }
    }

    // after your done with p, free it
    free(p);

    return 0;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top