10

The tagline says:

The heap is process memory set aside for dynamic allocation.

Which seems vague enough to cover questions that have dynamic allocation in it, but the tag wiki gives a different story:

A heap memory pool is an internal memory pool created at start-up that tasks use to dynamically allocate memory as needed. This memory pool is used by tasks that requires a lot of memory from the stack, such as tasks that use wide columns.

For example, in Sybase's Adaptive Server Enterprise, if you make a wide column or row change, the temporary buffer this task uses can be as large as 16K, which is too big to allocate from the stack. Adaptive Server dynamically allocates and frees memory during the task’s runtime.

The heap memory pool dramatically reduces the predeclared stack size for each task, while also improving the efficiency of memory usage in the server. The heap memory the task uses is returned to the heap memory pool when the task is finished.

Microsoft describes a heap for their SQL Server 2008 R2 as a table without a clustered index. Heaps have one row in sys.partitions, with index_id = 0 for each partition used by the heap. By default, a heap has a single partition. When a heap has multiple partitions, each partition has a heap structure that contains the data for that specific partition. For example, if a heap has four partitions, there are four heap structures; one in each partition.

The C++ standard has references to "dynamic allocation" and "dynamic storage duration", but AFAIK has no notion of the "heap" or "stack", which are operating system specifics. For example, here's a conversation dealing with this misconception:

pezy: the temporary object exist in the stack? or heap?

remyabel: @pezy That's an operating system detail.

pezy: @remyabel some guys say: use new would put in heap, otherwise in stack? Is it right?

MSalters: @pezy: The short answer is no, the long answer would start with "perhaps..." but not fit in a comment. As remyabel points out, the OS is really in charge and C++ just tells you how long the object lives, not where. ( "heap" => "until delete", "stack" => "until function returns", "global" => until program exits, "temporary" => until end of statement)

With that in mind, it doesn't seem appropriate to use if it's a platform-agnostic question. It looks like is a much better tag as it actually references :

Dynamic memory allocation, usually in the context or , refers to the process of asking the operating system for a variable sized block of memory.

In those languages, allocation involves the use of a few different techniques:

  • malloc (C), operator new (C++)
  • free (C), operator delete (C++)

Thoughts?

20
  • 5
    That wiki is horrendous.
    – Mat
    Commented Nov 8, 2014 at 13:45
  • @Mat Which one?
    – user3920237
    Commented Nov 8, 2014 at 13:50
  • 8
    The [heap-memory] one, talks about two database implementations of "heap" (the SQL Server thing storage too, not memory...) that just aren't what people mean in general when talking about heap memory.
    – Mat
    Commented Nov 8, 2014 at 14:09
  • 9
    OT: I get really annoyed by the pedants who say that C/C++ doesn't specify anything about heap or stack allocation. Conceptually, the heap is whatever new/malloc use, the stack is where local variables live. Whether they directly correspond with some OS feature is irrelevant.
    – Barmar
    Commented Nov 8, 2014 at 15:59
  • 1
    @Barmer While I agree that for all intents and purposes it's OK to talk about the heap and stack, my problem is people conflating the terminology and tagging questions with whatever seems vaguely relevant.
    – user3920237
    Commented Nov 8, 2014 at 16:10
  • 2
    @Barmar: I get really annoyed when people conflate semantics and implementation-details. In this specific instance, did you know that compiler optimizers could place new'ed objects on the stack? Commented Nov 8, 2014 at 16:27
  • 5
    That's an implementation detail. Memory that can conceptually last after the function returns is semantically a heap.
    – Barmar
    Commented Nov 8, 2014 at 16:49
  • The two terms are not so precise as you think. Trying to draw a distinction between them isn't helpful. In other contexts "heap" does have a precise technical meaning, but it doesn't mean anything like that here. Heap is used here to connote utter unconcern with how the contents are organized -- there are garbage-collected heaps, first-fit heaps, buddy-system heaps, best-fit heaps, heaps with no reclamation at all, and I bet ten minutes digging could turn up a dozen more. I even think I remember someone using a (real) heap (the data structure) to implement efficient first-fit.
    – jthill
    Commented Nov 8, 2014 at 23:03
  • (Yep. Here it is, and its author makes the same point I just did: "Thus, ST[l], , . . , ST[2S - I] is a “heap” in the sense of [13], Sect. 5.2.31, though we shall avoid using the word heap as it has a different meaning in the context of dynamic storage allocation. ")
    – jthill
    Commented Nov 8, 2014 at 23:09
  • @jthill That sounds like an argument in favor of retagging. Newbies get caught up on "heap" and "stack" because they don't really understand what it means.
    – user3920237
    Commented Nov 8, 2014 at 23:36
  • By that logic, all jargon should be thrown overboard, in every technical field -- and if that seems to be stretching it too far, I'll just say that if there's any technical domain where dumbing down the language to avoid confusing newbies is out of place, it's ... C++ ... :-)
    – jthill
    Commented Nov 8, 2014 at 23:43
  • 2
    @jthill It's not dumbing down the language if the heap and stack are not a part of the language. Nobody has to care about it because C++ abstracts it away. But when somebody ignores modern C++ practice and opts to use manual memory management, more likely than not, they're going to encounter the "heap" and be utterly confused. The idea is to avoid this kind of confusion by not throwing loaded words around and say exactly what you mean. i.e., "I want to allocate something on the heap", versus "I want to use dynamic memory allocation".
    – user3920237
    Commented Nov 8, 2014 at 23:54
  • 1
    @Barmar And I get really annoyed when people stubbornly refuse to accept that their choice of terminology — that they're hanging on to for no apparent reason whatsoever other than, I guess, to be obstinate in the face of more intelligent reasoning — is naively inaccurate and harmfully misleading. Your "conceptual" example is laughably incomplete. Commented Nov 9, 2014 at 2:38
  • Here is an example of someone extrapolating "stack" to "allocate space statically" when they're talking about automatic storage duration.
    – user3920237
    Commented Nov 9, 2014 at 12:46
  • @Barmar - I get really annoyed by : Very correct IMO. That dialog up there is just plain wrong. Stroustrup himself talks in many places about the free store as opposed to the stack. The concepts have nothing to do with the OS AFAIK, but with how C++ applications manage memory.
    – Vector
    Commented Nov 10, 2014 at 2:34

1 Answer 1

7

On the surface of it, yes: is more "correct" than .

However, in fact, neither of those tags is of any use at all. You're not going to get someone filtering their front page to see "dynamic allocation" questions, or searching specifically for questions on dynamic allocation. Y'know, unless they really like dynamic allocation....

Stick with and leave it at that.

5
  • 2
    Tags are also used as search terms, not just front page filters. dynamic-allocation also exists.
    – M.M
    Commented Nov 9, 2014 at 7:33
  • 1
    @MattMcNabb: I knew someone would say that. I'd argue that the same logic applies there. Commented Nov 9, 2014 at 12:43
  • Stroustrup prefers free store.
    – Vector
    Commented Nov 10, 2014 at 2:36
  • @Vector: Yep, dynamic allocation — with a default allocator — results in objects being allocated on the free store. Commented Nov 10, 2014 at 11:40
  • _ dynamic allocation... results in objects being allocated on the free store_ - he doesn't us the term dynamically allocated memory :) Enough time wasted on this nonsense. As far as tags go IMO it's not important.
    – Vector
    Commented Nov 10, 2014 at 21:09

You must log in to answer this question.