Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

there is no heap in c?

Tags:

c

malloc

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.

like image 909
user1171901 Avatar asked Aug 21 '12 16:08

user1171901


People also ask

Is there heap in C?

In C, there is no such place. The place from which malloc takes its memory is unspecified, and as it turns out we all call it 'the heap'.

Is there stack and heap in C?

Memory in a C/C++/Java program can either be allocated on a stack or a heap. Prerequisite: Memory layout of C program. Stack Allocation: The allocation happens on contiguous blocks of memory. We call it a stack memory allocation because the allocation happens in the function call stack.

Where is the heap C?

The heap is in the back of memory, and it's managed by the operating system. Using this is a bit more involved. You need to use the malloc function to tell the operating system how much memory you need.

What is heap memory in C language?

In certain programming languages including C and Pascal , a heap is an area of pre-reserved computer main storage ( memory ) that a program process can use to store data in some variable amount that won't be known until the program is running.


3 Answers

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).

like image 159
Ben Zotto Avatar answered Oct 16 '22 07:10

Ben Zotto


I think the authors are very precise when they say they are talking about the "language". When you talk about C, you have the language and the standard libraries. In the language itself, there is no dynamic memory allocation facility but the standard library provides those facilities.

like image 41
Khaled Alshaya Avatar answered Oct 16 '22 08:10

Khaled Alshaya


There is no heap explicitly defined in the language. Implementations, however, do use it for dynamically allocated memory.

See this discussion of various kinds of allocation, including heap:

http://en.wikipedia.org/wiki/C_dynamic_memory_allocation

like image 27
Joe Avatar answered Oct 16 '22 07:10

Joe