Stuart
Stuart

Reputation: 1773

How do I set a pointer to Null at run-time (C++)

Right now I have a pointer set to a row in my 2D array. I want that pointer to stop pointing to that row, but I will be using the pointer later for something else. I just want to know how to unset the pointer after it is initialized and has pointed to a row.

double* tempRow;
tempRow = (double*) malloc(size * sizeof(double));
   ...
tempRow = NULL;

doesn't unlink the tempRow variable from the array row. Why not?

I wonder if I should be using C then instead. Will there be overhead when using a vector?

Upvotes: 0

Views: 602

Answers (5)

Michael Anderson
Michael Anderson

Reputation: 73490

While you have written will set tempRow to NULL, it wont release the memory you have allocated. For that you need

free(tempRow);
tempRow = NULL;

However if you're using C++ as the tags suggest, you'd be better off using C++ new/delete

double* tempRow;
tempRow = new double[size];
   ...
delete [] tempRow;
tempRow = NULL;

you can even use the STL to handle your memory allocation for you.

std::vector<double> tempRow(size);
// You can access the data, in a similar way to an array
tempRow[5] = tempRow[4]+tempRow[3];

// If you really need to access the underlying pointer, (To pass to another 
// function for example) you can do this. Note that the pointer will be valid
// until the vector is destroyed or modified in certain ways that can cause the
// vector to reallocate its memory. So you can't use this to pass data to a 
// function that destroys or takes ownership of the passed in pointer.

fn_requiring_pointer( &temp[0] );

// The memory used in tempRow will get cleaned up automatically when the 
// object goes out of scope
//
// If I really need to free up the memory used in it early I can use a swap 
// hack. (iirc tempRow.clear() isn't guaranteed to release the memory)
std::vector<double>().swap(tempRow); // Unneeded in most cases.

Also trying to reuse the tempRow pointer for something unrelated is probably not necessary. Just create a new pointer with a different name. Reusing a variable form multiple different unrelated purposes can make code very hard to understand later.

Upvotes: 11

Chubsdad
Chubsdad

Reputation: 25497

Doesn't seem to work?

That's the worst complaint and a solution providers's nightmare.

Do you mean you get a compilation error?

If yes, did you include <cstdio>? and using namespace std;

Upvotes: 4

paxdiablo
paxdiablo

Reputation: 881453

Doesn't work in what way? The normal way to "unset" a pointer in C++ is with:

tempRow = 0;

but what you have should be fine, assuming you've included the correct headers or otherwise have the correct definition for NULL.

As an aside, you should first call free() on that memory before losing the pointer, otherwise you'l have a memory leak (and this is assuming you have a good reason to use C-style malloc/free instead of the more kosher C++ new/delete).

Upvotes: 2

dreamlax
dreamlax

Reputation: 95335

I'm new at C++ as well, but a while ago, someone told me that using std::vector is a much safer approach to handling arrays of data.

  • Automatic re-allocation when adding more elements.
  • Iterators for use with stuff from #include <algorithm>.
  • Bounds-protection with .at(index) element access.
  • No messy pointer-tracking required.
  • C-array style access with operator[].
  • RAII.

You would declare a vector like this:

std::vector<double> tempRow(size);

tempRow[0] = 3.00;
tempRow[1] = 1.00;

// no need to use delete[] or free(), it will destruct itself
// and relinquish its resources automatically.

Upvotes: 6

codaddict
codaddict

Reputation: 455020

The example you've shown should work.
Also if you've not freed the memory before making temRow NULL, you are leaking memory.

double* tempRow;
tempRow = (double*) malloc(size * sizeof(double));
   ...
free(tempRow);  // free the memory.
tempRow = NULL; // reset the pointer.
   ...
tempRow = &some_other_double_var; // reuse the pointer.

Upvotes: 4

Related Questions