2

Consider-

char* name1 = malloc(4 * sizeof(char));
name1 = "Mike";

Now if i want to put "Mike Smith" in the same string "name1", I need 6 more bytes of memory (one for the space and 5 for "Smith". Don't worry about the "\0" that is not the issue here)

So I will do-

name1 = realloc(name1, 10 * sizeof(char));

(10 = 4 for already existing "Mike" + 6 more for incoming " Smith")

Now as the question's title suggests, what will happen if memory following those first four block is, by any chance, already occupied. for instance before i used realloc() i used-

char* name2 = malloc(3 * sizeof(char));
name2 = "Tom";

what if these 3 bytes are those which follows the name1's 4 bytes and now if i use realloc for extra 6 bytes what will happen? How does computer manage this?

2 Answers 2

2

The answer of our friend @NullityNull is correct, as long as realloc succeeds and works well. Your question is interesting. Consider the following program fragment

char *string;
  string = (char *)malloc(100);
  string = NULL;

One of the most common failure when memory is managed explicitly is what is known as "memory leak". This situation occurs when a program gets dynamic memory, and the pointer value returned by the system is lost by mistake. In this case, it is no longer possible to invoke the free function with that pointer, and the portion of memory is reserved for the remainder of execution.

The first line declares a pointer to character. In the second you reserve a space of 100 bytes. The memory manager returns a pointer to the beginning of that block and stored in the variable string. At that time, the address of that block is not stored anywhere else. The next line assigns the same value to NULL pointer. What has happened with the memory address of the portion you just stay? It has been lost and there is no way to recover it, because string was the only copy of that value. As a result, the portion of memory reserved will marked as busy for the rest of program execution. The memory has escaped.

What happens if the assignment is not successful?

name1 = realloc(name1, 10 * sizeof(char));

then malloc returns NULL, the memory area pointed to by name1 is lost. Heap reallocation functions do not free the passed buffer if reallocation is unsuccessful. To correct the defect, assign the result of the reallocation function to a temporary, and then replace the original pointer after successful reallocation.

char* name1 = malloc(4 * sizeof(char));
char* tmp;
 if (name1 != NULL)
  {
    temp = realloc(name1, 10 * sizeof(char));
    if (tmp != NULL) 
    {
      name1 = tmp;
    }
  }
2
  • Thank you very much for very well explanation of the matter and that too in simple but precise language which is very useful for less comfortable people like me.
    – Vinz.R
    Commented Aug 8, 2016 at 17:33
  • I'm glad to be of help
    – MARS
    Commented Aug 8, 2016 at 19:05
2

If realloc can't resize the memory block you pass in, it makes a new one, copies the data, and deallocates the old one.

If I were you I'd read up a bit on specification for Realloc and malloc.

1
  • I did red some references including cs50 reference but none seems to cover the case or may be I wasn't able to understand it as i am in those who are less comfortable.
    – Vinz.R
    Commented Aug 8, 2016 at 15:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .