35

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.

Thom Smith
  • 13,916
  • 6
  • 45
  • 91
user1171901
  • 395
  • 3
  • 5
  • 1
    If you talk about the "language" then yes, but C is not merely the language. – Khaled Alshaya Aug 21 '12 at 16:19
  • I would either question the documentation on the description of the `malloc()` function or the book itself. If I were to guess which one was wrong I would guess the book. I can tell you that malloc() is written in the same language as everything else. – Security Hound Aug 21 '12 at 16:19
  • 2
    `malloc()` is part of the C library, but I think what the book is saying that intrinsically, the language does not specify a necessity for heap allocation. – wkl Aug 21 '12 at 16:21
  • 4
    the key phrase here is _the language does not define_. – pb2q Aug 21 '12 at 16:21
  • For instance, by opposition, Java and C# do define a memory area where objects can be created. 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'. – zneak Aug 21 '12 at 16:22
  • @pb2q - Which simply means there is no standard library method to modify the heap itself. The `malloc()` method of course returns an address from the heap. As `zneak` points out C# has a special area of memory called the `heap` which is signifigantly different then the `stack`. – Security Hound Aug 21 '12 at 16:23
  • @Ramhound I don't think that's what it means at all, it means that the language doesn't make requirements about how this space is implemented, it only supplies e.g. a `malloc` function. – pb2q Aug 21 '12 at 16:25
  • @Ramhound - the C standard does not make any suggestions as to where `malloc` actually gets its space from. The definition in the standard just specifies `malloc allocates space for an object whose size is specified by size and whose value is indeterminate`. The guarantee set forth by the standard is that items allocated through the `malloc` family have lifetimes that last from allocation to explicit deallocation. – wkl Aug 21 '12 at 16:27

4 Answers4

30

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

Ben Zotto
  • 70,108
  • 23
  • 141
  • 204
7

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.

Khaled Alshaya
  • 94,250
  • 39
  • 176
  • 234
3

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

Joe
  • 46,419
  • 33
  • 155
  • 245
2

malloc gets memory allocated to it from the kernel of the operating system. All languages do this. This is how the operating system makes sure programs have available space though it does not or may not prevent them, particularly those written in C, from attempting to go beyond any limits.

Rob
  • 14,746
  • 28
  • 47
  • 65
  • 2
    Just a slight nitpick: If you look at Java, `malloc` might very well be called by the JRE to _pre-allocate_ a heap that is then used by the language construct `new` but using `new` in Java isn't a call to `malloc`. – HonkyTonk Aug 22 '12 at 16:22