views:

875

answers:

4

This problem involved me not knowing enough of C++. I am trying to access a specific value that I had placed in the Heap, but I'm unsure of how to access it. In my problem, I had placed a value in a heap from a data member function in an object, and I am trying to access it in another data member function. Problem is I do not know how, and I had searched examples online, but none were what I needed as they were all in int main() and were not specifically what I needed.

In the first data member function, I declare the value I want to be sent to the Heap; Here's an example of what my first data member function.

void Grid::HeapValues()
{
    //Initializing Variable
    value = 2; //The type is already declared

    //Pointers point a type towards the Heap
    int* pValue = new int;

    //Initialize  an a value of in the Heap
    *pValue = value;
}

And in data member function This is what want:

void Grid::AccessHeap()
{
    //Extracting heap:
    int heap_value = *pValue; //*pValue does not exist in this function
    cout << heap_value; //Delays the value 2, which is found
                        //in the first data member function
}

I feel foolish for asking, but I am unable to find the answers and do not how. Does anyone know how to access a value from the heap in a simple way? And I would need it to be able to access in more then two data member function.

+11  A: 

pValue needs to be a member-variable of the class Grid.

class Grid
{
  private: int* pValue;
  public: void HeapValues();
          void AccessHeap();
};

Now the member-variable pValue is accessible from any member-function of Grid.

abelenky
Don't forget to change HeapValues() so that is doesn't declare pValue locally: int* pValue = new int;
jmucchiello
+6  A: 

Don't forget to delete your pointer in the destructor when you are done. For more information visit:

Maxfrank
Better off by using smart pointers. In this case auto_ptr should suffice
David Rodríguez - dribeas
+2  A: 

Like Aaron said you can make the value a member of your Grid class. In this case though there is no need for it to be a pointer to an int.

class Grid
{
private:

    int value;

public:

    void HeapValue();
    void AccessHeap();
};

The value will be stored as part of the object wherever it is instanciated. You can make it on the stack or the heap, it doesn't matter. For simple values like the built in types and Objects that will be owned by the instance of the class it is unnecessary to allocate them using new. This way you don't need to worry about cleaning up with the delete operator in the Grid destructor, just make sure you dispose of the owning Grid instance properly ;-)

Of coarse there are exceptions to this that you will learn as you delve more into C++, but for your example the above will be fine.

Matt
A: 

Why do you want it on the heap? If you add it as part of the class then it will be in the same place the class is, possibly on the stack or in global memory. Perhaps you want to have a variable size to your integer pointer? In that case, then you need to be sure to deallocate the memory when you are done with it.

The problem with stuff on the heap is finding it. There is no accessing it by name, unless you add a mechanism for that. Somehow you need to communicate the location to whatever code needs to access it. In this case, it looks like you only need access within the Grid class, so it is easy. Just make it a member variable like Aaron indicates. You might end up with something like:

class Grid
{
protected:
    int* pVals;
public:
    Grid() pVals(NULL) { }
    ~Grid() { delete [] pVals; }
    void HeapValues() {
        pVals = new int[getHeapValuesSize()];
        pVals[0] = 1; // ...
    }
    void AccessHeap() {
        cout << pVals[0]; // ...
    }

(On a side note, you appear to be using the phrase "data member function" when you mean "member function". "Data member" refers to member data of a class, like pVals, but I'm not sure what "data member function" would mean.)

Mark Santesson