Dynamic Memory Allocation - malloc versus realloc

In summary, the conversation discusses two ways of dynamically allocating memory, using malloc and realloc. The speaker expresses their preference for using realloc and questions the need for linked lists and push & pop. They suggest reading more about arrays vs linked lists and understanding the advantages of linked lists. The speaker also mentions the possibility of slowdown in code when using realloc compared to a linked list implementation.
  • #1
angelspikes
10
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
  • #2
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.
 

Related to Dynamic Memory Allocation - malloc versus realloc

1. What is dynamic memory allocation and why is it important?

Dynamic memory allocation is a process in which a computer program can request and use memory space from the operating system during runtime. This allows for efficient memory management and the ability to allocate memory as needed, rather than having to allocate a fixed amount of memory at the beginning of a program. This is important because it allows for more flexibility and efficient use of memory, which can improve the performance of a program.

2. What is the difference between malloc and realloc?

malloc and realloc are both functions used for dynamic memory allocation, but they serve different purposes. Malloc is used to allocate a block of memory of a specific size, while realloc is used to resize an existing block of memory. Malloc can also be used to allocate memory for a new variable, while realloc can only be used on previously allocated memory.

3. How do you use malloc and realloc in a C program?

To use malloc in a C program, you must first include the stdlib.h header file. Then, you can use the function malloc(size) to allocate a block of memory of the specified size. To use realloc, you must first allocate memory using malloc or another allocation function. Then, you can use the function realloc(ptr, size) to resize the previously allocated memory block ptr to the specified size.

4. What happens if malloc or realloc fails to allocate memory?

If malloc or realloc fails to allocate memory, they will return a NULL pointer. This means that the program was unable to allocate the requested memory and the program should handle this error accordingly. It is important to check for this NULL pointer and handle it appropriately to prevent any potential issues or crashes.

5. How can you avoid memory leaks when using dynamic memory allocation?

Memory leaks occur when allocated memory is not properly freed after it is no longer needed. To avoid memory leaks, it is important to always free the allocated memory when it is no longer needed. This can be done using the free(ptr) function, where ptr is a pointer to the allocated memory. It is also important to keep track of all allocated memory and ensure that it is properly freed at the end of the program.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Programming and Computer Science
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
676
  • Engineering and Comp Sci Homework Help
Replies
3
Views
760
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
  • Programming and Computer Science
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Programming and Computer Science
Replies
8
Views
4K
  • Programming and Computer Science
Replies
6
Views
2K
Back
Top