Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory is not reallocating

I'm in the middle of a project and I'm trying to use malloc() and realloc(). I know when I malloc, it works, but when I use realloc, it doesn't change the amount of alloced memory at all. I've always though that realloc will re-allocate your already malloced memory.

Here is what I have:

This include:

#include <stdlib.h>

I have a struct:

struct student {
    int age;
    int numOfClasses;
    int gender; //0 male; 1 female
} student;

When I want to make 7 of those structs using malloc, I will use this line of code:

student stud* = (structure*) malloc(7*sizeof(student));

This line works. That line of code takes the size of the structure and multiples that by 7. In short, this will grab enough memory to make an array of 7 structures.

Now, if I want to change that to 8, I would do this where A is the previous malloced memory, and B is the new malloced (or realloced) memory:

enter image description here

Here is how I have it in code:

stud = (student*)realloc(stud, 8*sizeof(student));

From what I know, realloc takes the variable in the second parameter and mallocs that amount of memory. Then, it takes the pointer (or previous malloced), and fills in the just malloced memory with as much as it can from the given pointer. Of course, the second parameter must be bigger than the previous malloced size, or stud will lose some memory on the end. Now this is where my problem is. When I call the line above, it doesn't change anything. The malloced array is still length of 7. I'm pretty sure, also, that I have enough memory to realloc.

Am I doing this right? Where could my problem be?

like image 773
Rob Avery IV Avatar asked Mar 08 '13 13:03

Rob Avery IV


People also ask

How can we reallocate memory?

The realloc function deallocates the old object pointed to by ptr and returns a pointer to a new object that has the size specified by size . If realloc() returns a pointer to NULL , no new object is created and the old object remains unchanged at its address in memory.

Which function reallocates memory in C?

In the C Programming Language, the realloc function is used to resize a block of memory that was previously allocated. The realloc function allocates a block of memory (which be can make it larger or smaller in size than the original) and copies the contents of the old block to the new block of memory, if necessary.

Does realloc erase memory?

Will I lose my data? No, the data will be copied for you into the new block that the returned p points at, before the old block is freed. This all happens before realloc returns, so the new p points to your data still.


1 Answers

Your understanding of realloc's behaviour is nearly correct. It doesn't have to return a different pointer; it may be that there was enough unused memory after the initial block, so the heap manager can just return the same pointer back to you (but adjust its own internal state such that it knows the block is now bigger).

You have made one small mistake, though.

stud = (student*)realloc(stud, 8*sizeof(student));

Here you are replacing your stud pointer with the return value from realloc. If it happens to return NULL due to memory starvation, then you have lost your original stud pointer and the memory is leaked. You should use a temporary pointer instead.

tmp = realloc(stud, 8*sizeof(student));
if (tmp)
    stud = tmp;

Also note that you still have to actually put something in the eighth slot. After the realloc the eighth slot is valid allocated memory, but contains garbage until you store something meaningful in it.

like image 68
Graham Borland Avatar answered Sep 18 '22 03:09

Graham Borland