timiz
timiz

Reputation: 59

Can a pointer be placed on heap memory (C++)?

I wrote some code in which I attempted to create a pointer on the Free store(Heap memory). I think it's impossible but I tried regardless.

The code below is basically creating a vector of pointers on the heap and then making those pointers in the vector point to some ints on the heap. I want to know if those pointers in the vector called vec are on the heap? I also wanted to know what is the proper way to free up space allocated when using a vector like this. The last delete statement was crashing the program so I commented it out. I don't know if there is a memory leak.

    vector<int*> *vec = new vector<int*>();
    vec->push_back(new int(1));
    vec->push_back(new int(2));
    vec->push_back(new int(3));
    cout << (*vec)[0] << " " << (*(*(vec))[0]) << endl;
    cout << (*vec)[1] << " " << (*(*(vec))[1]) << endl;
    cout << (*vec)[2] << " " << (*(*(vec))[2]) << endl;
    delete (*vec)[0];
    delete (*vec)[1];
    delete (*vec)[2];
    //delete [] vec;

Upvotes: 3

Views: 2308

Answers (4)

molbdnilo
molbdnilo

Reputation: 66459

The most important thing to understand about pointers is that there is nothing special about pointers.

Like all other vector elements, the elements of vec are located in the free store, since that’s where a vector keeps them.

If you want to manually create an int* in the free store, you would say new int*, which will of course return the address of that pointer, so for instance

int** pointer = new int*(nullptr);

However, there is very little point in doing this in practice — I don't think I’ve seen anyone use it in my twenty-odd years of C++.

Upvotes: 2

scohe001
scohe001

Reputation: 15456

If you want to allocate memory for an integer, you can say new int;, as you've done here.

The new keyword will accept any (non-void) type. So if you want to allocate for a pointer (which would be of type int*), you can just say new int*;

A (heavily contrived) example may look like:

int thing = 7;
int **ptr = new int*(&thing);
std::cout << "**ptr = " << **ptr << std::endl;

Notice that since new will return an address pointing to whatever type we allocated (in this case int*), we'll need to assign it to an int** variable.


All this being said, you're very unlikely to ever need to allocate a raw new int*. More than likely, you'd run into something similar if you're were dealing with dynamically allocated multi-dimensional arrays.

But in any of these situations, I'd strongly suggest using more modern memory management methods with Smart Pointers.

Upvotes: 0

Rokas Višinskas
Rokas Višinskas

Reputation: 543

The c++ way of creating a pointer on the heap is as follows: int** pointerToPointer = new int*;

The type int** can be read as a pointer to a pointer. Then we use new to allocate a piece of data of type int* (int pointer) on the heap.

Upvotes: 0

Michael Chourdakis
Michael Chourdakis

Reputation: 11178

Any variable can be created in heap, including a pointer. That said, at the C++ level you don't control where exactly the memory is created.

Upvotes: 7

Related Questions