When is malloc necessary in C?

2019-01-17 02:20发布

问题:

I think all malloc(sizeof(structure)) can be replaced this way:

char[sizeof(structure)]

Then when is malloc necessary?

回答1:

  • When you don't know how many object of some kind you need (e.g. linked list elements);
  • when you need to have data structures of size known only at runtime (e.g. strings based on unknown input); this is somewhat mitigated by the introduction of VLAs in C99, but see the next point:
  • when you know at compile time their size (or you can use VLAs), but it's just too big for the stack (typically a few MBs at most) and it would make no sense to make such thing global (e.g. big vectors to manipulate);
  • when you need to have an object whose lifetime is different than what automatic variables, which are scope-bound (=>are destroyed when the execution exits from the scope in which they are declared), can have (e.g. data that must be shared between different objects with different lifetimes and deleted when no one uses it anymore).

Notice that it isn't completely impossible to do without dynamic memory allocation (e.g. the whole rockbox project works almost without it), but there are cases in which you actually need to emulate it by using a big static buffer and writing your own allocator.

By the way, in C++ you will never use malloc()/free(), but the operators new and delete.


Related: a case in which trying to work without malloc has proven to be a big mess.



回答2:

You will use malloc to dynamically allocate memory, either because:

  • you don't know at compile-time how much memory will be required,
  • you want to be able to reallocate memory later on (for instance using realloc),
  • you want to be able to discard the allocated memory earlier than by waiting for its release based on the scope of your variable.

I can see your point. You could think you could always using a declarative syntax for all of these, even using variables to declare the size of your memory spaces, but that would:

  • be non-standard,
  • give you less control,
  • possibly use more memory as you will need to do copies instead of re-allocating.

You will probably get to understand this in time, don't worry.

Also, you should try to learn more about the memory model. You don't use the same memory spaces when using a dynamic allocation and when using a static allocation.

For first pointers, visit:

  • Dynamic Memory Allocation
  • Static Memory Allocation
  • Stack vs Heap
  • Stack vs Heap?
  • How C Programming Works - Dynamic Data Structures

Friendly advice: I don't know if you develop C on *NIX or Windows, but in any case if you use gcc, I recommend using the following compilation flags when you teach yourself:

  -Wall -ansi -pedantic -Wstrict-prototypes


回答3:

You should read about dynamic memory allocation. You obviously don't know what it is.

The main difference between the two is that memory allocated with malloc() exists until you say so. Static memory such as char buff[10]; only exists in the function scope.



回答4:

malloc is a dynamic memory allocator which helps u up to assign memory to ur variables according to ur need and therefore reduces the loss of memory.It is also supported by realloc() function through which u can edit the memory required which u have defined earlier through malloc() or calloc(). So in short we can say that malloc() can be used for managing the memory space and making use of the necessary memory without wasting it.



回答5:

You never should do this the way you are proposing. Others already told you about the difference of allocating storage on the heap versus allocation on the function stack. But if and when you are allocating on the stack you should just declare your variable:

structure A = { /* initialize correctly */ };

There is no sense or point in doing that as an (basically) untyped char array. If you also need the address of that beast, well, take the address of with &A.



回答6:

When you don't know how much memory to allocate at compile time. Take a very simple program, where you need to store the numbers entered by the user in linked list. Here you dont know how many numbers will be entered by the user. So as user enters a number you will create a node for it using malloc and store it in the linked list.



回答7:

If you use char[sizeof(structure)] instead of malloc, then I think no dynamic memory allocation is done.



回答8:

Besides the fact that your char[] method cannot resize or determine the size at runtime, your array might not be properly aligned for the type of structure you want to use it for. This can result in undefined behaviour.



标签: c malloc