0

For example when we write:

int * p = new int[5];

at which memory segment the memory will be allocated? In C I know that when we use malloc() it allocates memory in heap section but for C++ I am not sure. I read that, for new() memory allocated from free store and for malloc() memory allocated from heap on this link.

What is this free store? Is it part of RAM only? Is there any diagram for memory management containing free-store like we have for C?

Stubborn
  • 780
  • 1
  • 5
  • 20
  • Yes may be but I don't think it covers all I need to know. – Stubborn Mar 06 '18 at 11:28
  • In my app for debug builds, `new[] / delete[]`, `new / delete` and `malloc / free` have separate dynamic storage spaces; if called mismatched, it crashes the app (intentionally). But in release, they are all in the same dynamic storage. (Well, except for very large allocations, those have a separate large allocation storage space.) From a C++ standards perspective, it's an implementation detailed not dictated by the specification. – Eljay Mar 06 '18 at 11:29
  • 1
    @nwp: The accepted answer there is very misleading/confusing. – Lightness Races in Orbit Mar 06 '18 at 11:30

3 Answers3

3

What you're missing is that C++ does not describe, nor does it attempt to describe, these physical machine specifics. C++ is an abstraction. C++ source code doesn't list a sequence of steps to be performed by a computer: it describes the meaning of a program. Similarly, the language does not (generally) mandate such implementation specifics as a "heap".

When it says "free store", the language standard refers to the conceptual area of storage where dynamically allocated objects go. With your compiler, on your computer, in this decade, that may be a "heap" like structure in memory that you are familiar with. Or, it may be somewhere else. Ours is not to reason where.

Ultimately, the text on that website you linked to ("In case of new, memory is allocated from free store where as in malloc() memory allocation is done from heap") is simply wrong, and this is why you should not learn C++ from random websites.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • @Stubborn Same goes for RAM. The C++ standard doesn't mention RAM because it wants to be compatible with devices that don't have RAM, say, because they are a cache-only machine or use an EEPROM instead. It just says memory must come from somewhere with the specified behavior, the rest is up to the implementation. – nwp Mar 06 '18 at 12:00
  • The key point is that whether or not all target platforms have RAM or not _doesn't matter_. That the language is abstract is why it is so powerful and why compilers have some latitude in producing well-optimised executables. – Lightness Races in Orbit Mar 06 '18 at 12:04
1

The C++ standard doesn't specify.

All it states is that p has dynamic storage duration.

Practically you'll probably find that it is on the "heap" (which is an implementation concept not a language concept), much in the same way as the sister function malloc of C allocates memory on the "heap".

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
0

The free store is one of the two dynamic memory areas, allocated/freed by new/delete. Object lifetime can be less than the time the storage is allocated; that is, free store objects can have memory allocated without being immediately initialized, and can be destroyed without the memory being immediately reallocated.

Click here for details

Ak S
  • 97
  • 1
  • 8