1

Im declaring a static vector, and allocating/contructing the object within a function (create). I want to know if the memory allocated was in the heap or in the stack. Im confused

// Example program
#include <iostream>
#include <vector>

using namespace std;


typedef std::vector<int> vector1Int;

static vector1Int hello;

void create(){
    hello = vector1Int(8,12);
}

int main()
{

  create();

  return 0;
}
trincot
  • 317,000
  • 35
  • 244
  • 286
cesarknbv
  • 15
  • 3
  • 1
    Possible (Maybe) duplicate of [Are global variables in C++ stored on the stack, heap or neither of them?](https://stackoverflow.com/questions/44359953/are-global-variables-in-c-stored-on-the-stack-heap-or-neither-of-them) – Arnav Borborah Feb 16 '18 at 17:32
  • The default allocator of vector allocates from the free store (heap). – eerorika Feb 16 '18 at 17:33
  • How the vector elements could be allocated on the stack if you assign it to the external (relative to the function) variable? – 273K Feb 16 '18 at 17:34
  • _"I want to know if the memory allocated was in the heap or in the stack"_ Why? – Lightness Races in Orbit Feb 16 '18 at 18:05
  • @LightnessRacesinOrbit Cause I was reading about those two classes of memory in a program. Plain curiosity. – cesarknbv Feb 16 '18 at 18:26

2 Answers2

4

I want to know if the memory allocated was in the heap or in the stack. Im confused

After the line

hello = vector1Int(8,12);

is executed, you have a mix of objects from couple of places.

  1. hello always lives in the program's global state. It's neither heap memory nor stack memory.

  2. The elements of hello live in memory that is used by the allocator to allocate memory for objects. In most cases, the default allocator, std::allocator, uses dynamically allocated memory. But it can also use global memory.

PS

Dynamically allocated memory is commonly referred to as heap memory but the standard does not use that term.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • why the 8 elements of hello live in heap memory if it was not allocated with new?. Everytime STL container vector allocates memory for its elements, does it in the heap? – cesarknbv Feb 16 '18 at 17:38
  • 1
    internally, the constructor of std::vector will do the new [] for you – Jorge Y. Feb 16 '18 at 17:38
  • A `std::vector` has to use memory from the heap for its elements since the size of the vector can change at run time. – R Sahu Feb 16 '18 at 17:39
  • now, suppose, I declare hello within the function create(), that variable is in memory stack, but its 8 elements are stored in the heap? am I right? – cesarknbv Feb 16 '18 at 17:40
  • other question. Suppose, for some reason I declare hello as static, and allocate/construct as in the code. Now suppose, that hello is a vector with millions of int elements, and after some time Im gonna need that memory for other purposes. I can I correctly destroy and deallocate that vector without waiting for the program to end. Can I call hello.clear and then hello.~vector1Int() for that purpose? – cesarknbv Feb 16 '18 at 17:44
  • 1
    @cesarknbv, you can use `hello.clear();` but not `hello.~vector();`. Caling the destructor explicitly for that object is not only unnecessary but also it leads to undefined behavior. – R Sahu Feb 16 '18 at 17:47
  • Im currently not needing to destroy it, but then, what is the purpose of having a destructor for vector as a public member function.... if as you said (and hundreds of other posts said), it not recomended given the undefined behavior? – cesarknbv Feb 16 '18 at 17:50
  • That indicates to me that you need understand the fundamentals of using classes. It will be helpful for you to go through the "classes" section of [a good text book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – R Sahu Feb 16 '18 at 17:54
  • you are more than right. I was trying to do all my work with C-like pointer programming but it was pointed out to me that I better learn classes and go to c++. Im liking it, but still, have much to learn about it. Anyway, thanks for the answer. Im gonna keep reading about it. thanks alot. – cesarknbv Feb 16 '18 at 17:57
  • _"The elements of hello always live in heap memory."_ This indicates to me that you need to understand the fundamentals of C++ as an abstract description of a computer program. – Lightness Races in Orbit Feb 16 '18 at 18:05
  • @RSahu _"A std::vector has to use memory from the heap for its elements since the size of the vector can change at run time."_ Simply not true. It could use a pre-allocated block of memory from somewhere else (up to a maximum size). Indeed, this may very well be the case if you're using a pool allocator, which is not uncommon. Besides that, assuming use of the default allocator, C++ doesn't specify how or where the free store should be implemented. – Lightness Races in Orbit Feb 16 '18 at 18:07
  • @LightnessRacesinOrbit, I am guessing that you are being particular about dynamically allocated memory vs heap memory. – R Sahu Feb 16 '18 at 18:13
  • @RSahu: I am being particular about _the truth_ if that's what you mean – Lightness Races in Orbit Feb 16 '18 at 18:15
  • @LightnessRacesinOrbit, Falling back on my Indian upbringing - *the truth* is one but we all see it differently :) – R Sahu Feb 16 '18 at 18:19
  • @RSahu: In this case, it is written in black and white by the International Standards Organisation. – Lightness Races in Orbit Feb 16 '18 at 18:36
  • @LightnessRacesinOrbit, That the standard does but they cannot remove the use of the term heap memory to refer to dynamically allocated memory from existing literature and the vernacular. – R Sahu Feb 16 '18 at 18:42
  • That is not what is being pointed out to you right now. What @LightnessRacesinOrbit is saying is that `std::vector` may not use dynamic memory at all. – giusti Feb 16 '18 at 18:43
  • @RSahu: The vernacular is only so when people continually insist on using improper terminology. You have the power to help us _stop_ that problem getting worse. I invite you to join us! – Lightness Races in Orbit Feb 16 '18 at 19:16
1

Possibly neither. The precise terms are

  • static storage: for data that exists as long as the process exists;
  • automatic storage: for data that is allocated and fred as the process enters/exits different scopes;
  • dynamic storage: for data that must be explicitly requested and exists until it is explicitly fred.

Usually automatic memory lives in the stack and dynamic storage lives in the heap. But the compiler is completely free to implement all those storage types in whatever way they want, as long as it respects the rules for lifespan.

So:

static vector1Int hello;

is in the file scope and creates an object of type vector1Int in static storage.

And this

hello = vector1Int(8,12);

will cause std::vector to create room for at least 8 integers. We can usually assume that this will be taken from dynamic storage. However, this is not a rule. For instance, you could easily make std::vector use static or automatic memory by implementing your own allocator (not general purpose memory allocator, but STL allocator).

When your program reaches the end of the main function, the destructor of std::vector will be called for hello, and any dynamic memory that hello had requested will be given back to the memory manager.

The memory for the object hello itself is not fred because it is static. Instead, it is given back to the OS together with anything else the process used when the process terminates.

Now, if hello had been declared as a local variable of create, then the destructor would be called at the end of that function. In that case, hello would have been allocated at automatic storage, and would be fred at the end of create.

giusti
  • 3,156
  • 3
  • 29
  • 44