0
votes

If a subroutine allocates a new buffer, how can the caller of that function pass a pointer to get the address?

Every declaration combination I've tried doesn't work, i.e. after calling the subroutine, the pointer passed does not get set to the address of the buffer created in the subroutine.

int main ()
{
   float *fMyBuffer;

   // Pointer Update
   readFloatData ( fMyBuffer ); // &fMyBuffer  *fMyBuffer 

   // Deallocate Memory
   delete [] fMyBuffer;
}


void readFloatData(float *&fBuffer)  //*fBuffer   &fBuffer
{
  // Create New Buffer
  float *fData;
  fData = new float [1000];

  // Pass our buffer Address to user's pointer
  fBuffer = fData;
}
3
If a reference to a float is a float*, then a reference to a float * is? - SJuan76
@Sjuan Usually an incredibly bad idea? :-) - Voo
Very simple: 1) function signature: void readFloatData(float **fBuffer) {...}, 2) function call: readFloatData (&fMyBuffer );, 3) Delete all the (now extraneous) stuff in "readFloatData():. - FoggyDay
The only thing I can see wrong with the code you posted is that you're calling a function before it is declared. - Ben Voigt
Thanks, should have mentioned, that for clarity I've ommited some of the details, like the function prototype declaration section. Also, while its customary that the function return the handle, my actual function needs to return 2 pointer addresses so that's why I wanted to do it as a parameter. Although, I bet there's a way to return multiple pointers (using vectors/unique_ptrs) however, I'm not very familiar with those types. - ben

3 Answers

2
votes

This is usually done by passing a double pointer, or the real C++ way is to use std::unique_ptr or std::vector:

vector:

int main(int argc, char** argv)
{
   std::vector<float> fMyBuffer = readFloatData ();
   return 0;
}


std::vector<float> readFloatData()
{
  // Create New Buffer
  std::vector<float> fData(1000);

  // Populate vector...

  return fData;
}

unique_ptr:

int main(int argc, char** argv)
{
   std::unique_ptr<float[]> fMyBuffer = readFloatData (); // Note the [] is MUST else the wrong deleter will be used
   return 0;
}


std::unique_ptr<float[]> readFloatData()
{
  // Create New Buffer
  std::unique_ptr<float[]> fData(new float[1000]);

  // Populate data...

  return fData;
}

double pointer:

int main(int argc, char** argv)
{
   float* fMyBuffer = nullptr;
   readFloatData(&fMyBuffer);
   delete[] fMyBuffer;
   return 0;
}


void readFloatData(float** fData)
{
  if (fData)
  {
      // Create New Buffer
      *fData = new float[1000];

     // Populate data...
   }
}

And as Konrad Rudolph said, a reference to pointer is possible too:

int main(int argc, char** argv)
{
   float* fMyBuffer = nullptr;
   readFloatData(fMyBuffer);
   delete[] fMyBuffer;
   return 0;
}


void readFloatData(float*& fData)
{
   // Create New Buffer
   fData = new float[1000];
}

Note that you can also pass in references to vectors or unique_ptrs, e.g:

void readFloatData(std::unique_ptr<float[]>& fData)
void readFloatData(std::vector<float>& fData)
1
votes

Use a double level pointer?

float **fBuffer would work just fine.

1
votes

Your code actually works as-is (apart from the fact that you need to declare readFloatData before using it). The question is whether this is really a good idea – why not return the new value instead? This is what return values are for, after all:

float* readFloatData()
{
  // Create New Buffer
  float *fData = new float [1000];

  // Pass our buffer Address to user's pointer
  return fData;
}

int main() {
  float* fMyBuffer = readFloatData();
  // …
  delete [] fMyBuffer;
}

That said, raw pointers shouldn’t own memory anyway. Use a std::unique_ptr or similar instead. For arrays, use std::vector<float> instead of a pointer-to-array.