Question:
I still don't feel completely confident about using malloc()
or realloc()
, are these two ways equivalent?
1)
int main()
{
int x=0;
char *s2,*s1;
s1=NULL;
s2=malloc(200);
while(x++<3)
{
scanf(" %s", s2);
s1=realloc(s1, strlen(s2));
strcpy(s1, s2);
printf("%s\n", s1);
}
free(s1);
free(s2);
}
or doing
two)
while(x++<3)
{
scanf(" %s", s2);
s1=malloc(strlen(s2));
strcpy(s1, s2);
printf("%s\n", s1);
free(s1);
}
What would happen in memory when we do malloc twice, like this:
int x=0;
char *s2,*s1;
s1=malloc(100); /*reservar aqui*/
s2=malloc(200);
while(x++<3)
{
scanf(" %s", s2);
s1=malloc(strlen(s2)); /*e reservar aqui*/
strcpy(s1, s2);
printf("%s\n", s1);
free(s1);
}
Is there any way to reserve memory space this way? Just allocating a space of 200 (like for example) and only then relocating exactly the necessary memory?
scanf(" %s", s2);
s2=malloc(strlen(s2));
What does malloc()
and realloc()
internally in memory?
Answer:
Definitions of malloc
and realloc
:
malloc :
This method allocates space in the heap that has not been initialized, returns a pointer to the allocated memory. The role signature is:
void *malloc(size_t size);
Where size_t corresponds to the integral data type returned by the sizeof
operator and is used to represent the size (in bytes) of an object. It is defined (in the string.h header in C language and in the header in C++) as an unsigned integral type. It's just an indication that the type is used to hold the number of bytes of memory (not the usual unsigned int ).
The code below allocates memory for 10 integers and assigns the allocated memory address (address of the first byte of memory) to the int ptr
pointer:
int * ptr = (int*) malloc(10 * sizeof(int));
-
If the system is not able to allocate the necessary space in the
heap
then the function will returnnull
. -
If the size is zero
(malloc(0))
, then the function can return eithernull
or a valid pointer. -
By default the function returns a pointer of type void that needs to be properly casted before using it (in the example above we used
int*
to make the casting ). -
The memory returned by the function is uninitialized and contains "garbage" values.
realloc :
This method changes the size of the memory block on the heap. Suppose a pointer ( say ptr
) that is pointing to a space in memory defined by a malloc
, like what we did in the explanation of malloc . And you want to increase the size of the memory space pointed by ptr
from 10 to 20, without losing the content already allocated in memory. In that case you can use realloc
. The role signature is:
void *realloc(void *ptr, size_t size);
Where ptr
is the pointer to the allocated block, and size
is the new size (in bytes) for the new block of memory. it is possible that the function will move the memory block to another place if it is not able to allocate the space right after where it already is. In that case it will allocate a 20 integer space elsewhere and copy the content contained by the 10 integer block in size. And so the value of ptr
will change.
CARE TO BE TAKEN:
int * ptr1 = (int*) malloc(5 * sizeof(int));
int * ptr2 = ptr1;
ptr2 = (int*) realloc(ptr2, 10 * sizeof(int));
When realloc
is called, the memory location pointed to by both pointers can be deallocated (in case the contiguous space is not available right after the memory block). ptr2
will now point to the newly moved location in the heap
(returned by realloc ), but ptr1
is still pointing to the old location (which is now deallocated).
PS:
For realloc
the first parameter to the function can either be a pointer to a block of memory your code was using or it can be null pointer
, in the second case the function will behave like a malloc