Monday, 14 November 2016
malloc - there is no heap in c?
Answer
Answer
I just started reading The C Programming Language by Brian Kernighan and Dennis Ritchie,
and I found this statement:
The language does not define any storage allocation facility other than static definition and the stack discipline provided by the local variables of functions; there is no heap or garbage collection.
So does this mean that it is due to the malloc()
function, which returns some memory address from heap, that C enjoys access to the Heap memory? And then must malloc
be written in some other language, most probably assembly or B?
This may be a silly doubt, but I have to clear it. Thanks.
Answer
The C language itself does not specify directly for a heap or how it should work, but does provide pointers, etc.
malloc
and its cousins are part of something called the C Standard Library, and are functions that you link to with any standard implementation of C, and those do provide access to memory that is not static or on the stack. On every platform, the way those functions actually obtain and manage that memory can be different.
C is a long-baked language and library, and now it all appears to be of a piece together. But when K&R were writing that book, that was not so obvious, and that statement is a clarification of what belongs to the syntax of the language itself (versus what is typically provided by the supporting libraries).
c++ - Does curly brackets matter for empty constructor?
Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...
-
A fair amount of the second act of The Dark Knight Rises has a class warfare plotline. This is foreshadowed in the trailers with Selina Ky...
-
I want to create an options array from a string. How can i create an array as { width : 100, height : 200 } from a string ...
-
This is my first post. I have searched extensively for four days through Stackoverflow and other sources for the problem and have yet to fi...
No comments:
Post a Comment