-3

I was reading that Objects in Java are in the heap, but I was wondering if that is also the case for C++ or any programming language.

Ray Rutzer
  • 67
  • 3
  • 3
    C++ doesn't have a concept of the heap. What it has is objects with automatic storage duration and objects with dynamic storage duration. How that is implemented is up to the implementation. – NathanOliver Mar 01 '19 at 14:04
  • 5
    @NathanOliver: The fact that the C++ standard does not define the heap does not negate the fact that heaps exist and are widely used to implement C++, and asking what is in them is a valid question. – Eric Postpischil Mar 01 '19 at 14:07
  • When a heap exists and is used for dynamic storage duration objects in `c++` (implementation detail) it would contain objects if you dynamically allocated the objects. – drescherjm Mar 01 '19 at 14:11

4 Answers4

3

Heap is not a word used by the C++ language in this context. Heap is an area of virtual memory provided by the operating system (from a very abstract, high level point of view). Memory allocated from the free store in C++ is typically allocated from the heap by the language implementation. That is an implementation detail.

Does the heap in C++ contain classes ...

No. Classes do not have storage in C++. They are not contained in memory.

Objects have storage. A dynamically allocated object is stored in the free store, which is typically in the heap memory.

... and structure objects or does it just contain pointers?

Pointers, just like instances of classes (including structures), are also objects. Just like objects of other types, pointers too can be allocated in dynamic, automatic or static (and other) storage.


I was reading that Objects in Java are in the heap, but I was wondering if that is also the case for C++ ...

No. Only dynamically allocated objects "are in the heap" (note though the caveat that this is an implementation detail). Objects in automatic storage are "on the stack" (another area of virtual memory, another implementation detail). Objects with static storage are in yet another area of memory.

Given that the location is an implementation detail, it is possible for an object to only exist in a register of the processor, and not in memory at all.

... or any programming language.

It is not universally true for all programming languages. After all, I've told you that it is not true for C++. There are other languages besides C++ that have a concept of distinct storage durations. Rather obviously, C being another example.

There are however many dynamic languages for which your statement does apply.

eerorika
  • 232,697
  • 12
  • 197
  • 326
0

In C++ and C the heap -it is an abstraction- contains (conceptually) memory zones. Read wikipage on C dynamic memory allocation (most of it also applies to C++). At the operating system level, the heap is made of several segments in the virtual address space which has grown at runtime (thru system calls like mmap(2) on Linux, indirectly called by malloc and/or ::operator new). And in theory the heap could be non-existent or empty (see this malloc joke-implementation and adapt it to C++).

A memory zone (it can be in the heap, it could also be on the call stack) may contain an object. It may also contain some other aggregate (e.g. arrays). And it could also contain garbage (or a mix of all these).

Sometimes, a memory zone is not yet initialized, then (at that moment) it contains garbage. For example, the result of ::operator new, or of malloc can be uninitialized memory.

A related and important notion is undefined behavior.

What matters is the storage duration of your objects. Automatic variables are conceptually on the call stack (which is not mentioned in the C++11 standard n3337), but some of them simply sit in processor registers in practice (per the as-if rule). Read more about dynamic memory management in C++.

An important difference between Java and C++ is their memory management. The Java language mandates a garbage collector. But C++ is based on RAII so favors reference counting (which could be considered as some poor GC technique which does not handle correctly circular references) and smart pointers.

C++ is a very difficult programming language (I don't claim to master it, even if I can code in C++). You'll need years to learn C++. Start by reading some good C++ programming book. See also some C++ reference site.

I believe that the terminology and concepts of garbage collection are useful even to C++ programmers (because in some cases, you'll end up implementing a specialized GC in your application; be aware of Greenspun's tenth rule). So read the GC handbook.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • Why the downvote? – Basile Starynkevitch Mar 01 '19 at 14:07
  • Not my downvote, and the following does not apply to your answer: in general, there's plenty of junk in the C++ tag already, and bad questions should not be encouraged with answers. Now, this is not a great question, but there's just so much utter flotsam that's far worse. Hence, I don't think it's worthy of a downvote. Plenty of other junk to downvote. But it's probably a dupe, so I might spend a few seconds searching for one, instead. – Sam Varshavchik Mar 01 '19 at 14:11
0

tl;dr in C++, variables are stored directly in something akin to Java's 'stack', but they might as an implementation detail be backed up by data allocated on something akin to Java's 'heap'.

My answer only applies to common C++ implementations, and not to the language itself (e.g. I'm sure embedded systems are quite different). I'm also ignoring dynamic storage duration (what you get with the new and delete keywords in C++) because that isn't very useful most of the time.

In Java, you have the idea of the stack. It holds primitives, and it holds references to instantiations of classes (I'm not going to use the word "object" here because it means something very different in C++). The actual class instantiation sits somewhere else: on the heap. == in Java compares only the stack value and ignores the heap value.

Meanwhile, in C++, when you define a class, e.g.

class Foo {
  double data;
  std::string more_data;
};

And then instantiate it

Foo foo = {5.0, "enough data to to prevent the short string optimization from happening"};

Then that puts both of Foo's data members directly on the 'stack'. The expression sizeof(Foo) will tell you how much 'stack' memory is needed by any instantiation of Foo (note that it is always the same, and is a feature of the type Foo and doesn't vary at runtime).

Of course, Foo's std::string data member can handle arbitrarily-sized strings. In order to do that, typical std::string implementations hold a pointer to a dynamically allocated chunk of memory. That chunk is likely allocated when a variable of type Foo is instantiated with a non-empty string, and then it is deallocated (as per std::string's destructor) when that variable goes out of scope. That's your 'heap'. Pretty much anything which needs to store an amount of data which varies at runtime will use the 'heap'.

hegel5000
  • 876
  • 1
  • 7
  • 13
-2

The so-called heap is memory that a program may use for any purpose. It is used to implement objects, store data, and more.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312