Question:
Memory Allocation && Langauge C?
James
2011-02-21 08:29:19 UTC
I have a few questions about "malloc()", but first thanks very much for your time :)

I just find it a little weird to see it referenced sometimes as "ec_malloc()".. my guess is "error check".. but if there really a difference and are there other prefixes?

but maybe Im ahead of myself.. I am still concern with why to allocate additional memory in the heap..

from my understanding of memory allocation the data and bss segments store global and static variables

and the stack stores local function variable..

I just find it weird to allocate memory in the heap when I could store it elsewhere(by declaring a variable earlier or just in a local function) .. maybe a practical application is what I need?


I also am under the impressions when using malloc its only applicable to access using pointers? just want to verify this or be corrected.

I wonder if c++ uses malloc() as well?

again,
Thank you very much and appreciate your help!
Four answers:
?
2011-02-22 08:50:31 UTC
in C and in C++ there are no "prefixes", malloc() is only malloc(). All "prefixed" mallocs are non-standard compiler extensions or, more commonly, third-party library functions that do something in addtion to allocating memory, or allocate it differently, or do something else. There are *many* such prefixed mallocs, check out http://en.wikipedia.org/wiki/Malloc#Implementations



Dynamic storage (often implemented as 'heap') is necessary when an object's life time is *dynamic*, that is, it is not the entire duration of the program (which would be static) and not the duration of any one function call (which would be automatic). For example, a window in a GUI application: your program does not start up with all possible windows already created and drawn (so they are not static), they come up when needed, persist across multiple function calls, and disappear at some later time.

If you haven't encountered tasks that require dynamic object lifetime, good for you! Static and scope-bound approaches are much less error-prone.



And yes, a dynamic object can only be accessed through a pointer.



C++ allows calls to malloc(), but it is not used except for very rare low-level tasks. Dynamic objects in that language are created with new-expressions.
Asad Siddiqi
2011-02-21 08:36:29 UTC
The answer is "it depends". It depends on your memory usage requirements. Memory allocation on stack and heap is usually a design and architecture decison with a certain tradeoffs. These include the access time requirement, read time requirements and save time requirements.



For the prefixes of malloc, the purpose of all of them is memory allocation. I know that there are some variants that are used for variations and version of the C Compiler used (like gcc, VC++, borland) based on the libraries you are using you might be using or choose to use an enhanced version for better performance on the platform you are writing for. Malloc is used for memory allocation primarily and when you allocate memory dynamically it does involve pointers. you do not access pointers using malloc.



You can use malloc in C++ but there is a "new" operator as well. Since C++ is object oriented you can do memory processing in your constructors and destructors. You can use new and delete instead of malloc and free.



Always remember to free the memory you allocate with malloc, otherwiuse your program will have memory leaks :)



Hope this helps
Zero
2011-02-21 08:38:26 UTC
c++ uses the new operator to allocation memory



Dynamic Memory is need because you may not know how many item a collection is going to have have design time so we have to allocate memory from the heap
?
2016-10-16 06:45:57 UTC
you are able to in C/C++ additionally, yet while it comprises preparation, appropriate structures and classic C text cloth strings, you in many situations could desire to get memory advert hoc. In C, there is the final objective alloc/calloc/malloc/unfastened gadget; in C++, there is new and delete for gadgets. the two artwork out of what's customary because of the fact the heap -- a chew of memory intense in the consumer's workspace. wish that helps.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...