Dynamic Memory Allocation - malloc versus realloc

  • Thread starter Thread starter angelspikes
  • Start date Start date
  • Tags Tags
    Dynamic Memory
AI Thread Summary
Dynamic memory allocation can be achieved using malloc and realloc, with realloc allowing for resizing of an initially empty array as data is added or removed. While malloc provides a fixed size allocation, realloc offers flexibility but may incur performance costs due to data copying. The discussion highlights a preference for realloc over linked lists, which are perceived as complex and cumbersome despite their advantages in managing memory in smaller increments. Understanding the benefits of linked lists may clarify their utility in certain scenarios, especially in terms of performance and memory management. Ultimately, the choice between these methods depends on specific use cases and performance considerations.
angelspikes
Messages
10
Reaction score
0
There are two ways I'm used to allocate memory dynamically.

int * array = malloc(10 * sizeof(int));

But this type of memory allocation, have a predefined size, which is not desirable.
I keep hearing about linked list and push & pop. I've reviewed them over and over again, and they just look like an awful mess, and they have so many drawbacks. Some of the "practical" examples for a linked list has shown to be like 40 pages long. Below is my own way of allocating dynamic memory using realloc. It's plain and simple. You start with an empty array, then add data when needed, and you can delete it when you don't need it anymore, or why bother, just replace it:

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

struct IO
{
int myInput;

} *pointer;

int main ()
{
// Put the two lines below in a loop which will fill up the dynamic array
pointer = (struct IO *) realloc (pointer, n * sizeof(struct IO));
pointer[n].myInput = myInput;

free (pointer);
}

Now you tell me, which one of these do you prefer, and would you still suggest me using a linked list or push & pop?
 
Engineering news on Phys.org
There's a lot of mixing of concepts here. Perhaps if you focus on arrays vs linked lists and read this first it may help:

http://www.techrepublic.com/article/deciding-whether-to-use-arrays-or-linked-lists/#.

The malloc/free and realloc are often used in the implementation of linked lists so once you understand the advantages of linked list then it will make more sense.

In your case, using realloc might involve a copy of the original data to the new expanded area and thus incur a possible slowdown in your code whereas a linked list implementation will do things in smaller chunks and run smoother.
 
Hi all, I have a question. So from the derivation of the Isentropic process relationship PV^gamma = constant, there is a step dW = PdV, which can only be said for quasi-equilibrium (or reversible) processes. As such I believe PV^gamma = constant (and the family of equations) should not be applicable to just adiabatic processes? Ie, it should be applicable only for adiabatic + reversible = isentropic processes? However, I've seen couple of online notes/books, and...
Thread 'How can I find the cleanout for my building drain?'
I am a long distance truck driver, but I recently completed a plumbing program with Stratford Career Institute. In the chapter of my textbook Repairing DWV Systems, the author says that if there is a clog in the building drain, one can clear out the clog by using a snake augur or maybe some other type of tool into the cleanout for the building drain. The author said that the cleanout for the building drain is usually near the stack. I live in a duplex townhouse. Just out of curiosity, I...
I have an engine that uses a dry sump oiling system. The oil collection pan has three AN fittings to use for scavenging. Two of the fittings are approximately on the same level, the third is about 1/2 to 3/4 inch higher than the other two. The system ran for years with no problem using a three stage pump (one pressure and two scavenge stages). The two scavenge stages were connected at times to any two of the three AN fittings on the tank. Recently I tried an upgrade to a four stage pump...

Similar threads

Replies
3
Views
1K
Replies
2
Views
2K
Replies
8
Views
5K
Replies
1
Views
10K
Replies
3
Views
1K
Replies
7
Views
2K
Back
Top