The exact answer to this depends on the implementation detail of the memory management functions, but I would say that in most cases it does in the same. Some people just use realloc()
using a null as the starting value.
The penultimate code will leak the first allocation of s1
because you don’t even have a reference to it after you’ve executed the core of the code, or that you wanted to, you can’t release the memory there. Of course, in a simple code like this, it doesn’t matter, it finishes the execution and nothing bad happens, but doing it in the middle of a huge code in production and if it is a pattern that repeats itself several times can create problems by dropping orphan allocations.
Note that in this specific example you do not need to keep allocating or relocating, you can use the same space as then the value is dropped. And even more, it would be better to make allocation in stack and have less headaches. I understand that it is just an isolated example, but it is good to know this so that it does not continue to reproduce it in real code as a good wrong practice.
In real code in production this is usually done differently. It is more common to allocate a reasonable size in stack and only after you have the actual text will you copy to heap making the necessary allocation already at the proper size. See What are and where are the "stack" and "heap"?.
Beware of strlen()
, its use in some cases can cause more damage than the focus of the question.
And since the question even has a reward, let’s learn right that one should not do cast in the malloc()
in almost every situation. It is less readable, redundant, hides certain types of errors among other problems, see Typecast malloc is recommended? and What is the difference in the syntax ptr = (int*) malloc (sizeof(int)) and ptr = malloc (sizeof(int))?.
Documentation of malloc()
and realloc()
.
Is there any way to reserve memory space like this? Only by allocating a space of 200 (as for example) and only then relocating exactly the necessary memory?
I think this should answer: This prevents an array from being initialized with a variable size in C?.
What really makes a malloc() and realloc() internally in memory?
It depends on the compiler or even the library you are using, or the platform you are running on. Already knowing what the heap, understand that its allocation algorithms may vary as needed, and you can make your own way.
The most common is that this function is an abstraction that organizes the best way to work in each situation. In general it invokes the operating system to allocate a larger region of memory that may be 4KB, or normally multiples thereof, in some cases 2MB or 1GB, since these 3 are sizes of virtual memory pages commonly used in the operating system. And as it is being requested by the application it occupies these spaces. In a way it is very similar to the stack which has its entire memory allocated and waiting to be occupied by frames execution.
The problem becomes greater when it releases spaces and then it tries to reuse these spaces. There is an algorithm that tries to occupy these spaces in the best way possible, which is something complicated to reconcile everything. There occurs what we call memory fragmentation and the algorithm slows down to make allocations, unless the free()
avoid this, which it will be much slower.
It would be nice to read more on How malloc organizes memory? and Variables are randomly allocated in memory?. Will also be useful How "free()" knows how much memory has to free? and What is page fault?.
The realloc()
is a malloc()
that changes the previously allocated size. A good algorithm tries to increase its size without changing its location, but it is not always possible, especially if it is increasing its size. And whether it is decreasing or changing the site object is common to increase memory fragmentation. The remaining site may still be reused, but it will not always be easy to put something of the proper size there. And when it changes its place, it can stay away from another object that it is always associated with, which decreases the location and messes up the memory cache. So abusing him can wreak havoc.
In the background both are only manipulators of a data structure that organizes the allocations. And if you have a specific need you can write a structure and algorithms that best suits your need.
Otherwise the question needs to be more specific about what you want.
But what was the point of
whiles
? It’s just that I seestrcpy
when I suspect the intention wasstrcat
, otherwise relocation in almost all cases would not be necessary. The goal is to read word by word and join in astring
?– Isac
It is just a small code to expose my doubt about memory allocation, just to understand the functioning of malloc() and realloc(), this code is only intended to be used to learn about memory allocation
– Fábio Morais