Dynamically declaring an array within a function

C

Chris Haynes

Hello all,

I have a structure:

typedef struct UVstruct {
float u, v;
} uv;

Inside a function (A) i declare a pointer to an instance of this
structure:

UVstruct *pUV;

then from within the same function (A) i call another function (B)
that takes as one of its parameters the pointer declared above.

From within functionB i dynamically declare an array (pUVlocations is
the name of the passed pointer pUV within B):

pUVlocations = new UVstruct[number];

Still in B, I fill pUVlocations with some data. At the end of the
function pUVlocations contains all the data it should, and i do not
free pUVlocations.

However when the program has passed back to A, when I try to access
pUV it contains rubbish.

What am I doing wrong? Why does pUV not contain the data it was given
within B?

Many thanks,

Chris.
 
D

Donovan Rebbechi

Hello all,

I have a structure:

typedef struct UVstruct {
float u, v;
} uv;

Inside a function (A) i declare a pointer to an instance of this
structure:

First, when posting, could you always post a minimal example that demonstrates
the problem ? Most of us are good at reading C++ but not so good at reading your
verbose and imprecise rambling.
UVstruct *pUV;

then from within the same function (A) i call another function (B)
that takes as one of its parameters the pointer declared above.

You really should right the actual code down. The above is ambiguous and
verbose.
From within functionB i dynamically declare an array (pUVlocations is
the name of the passed pointer pUV within B):

pUVlocations = new UVstruct[number];

Look, if you do this for example:

void B( UVstruct * x )
{
// creates a pointer to some memory and assigns that pointer to
// the identifier "x".
x = new UVstruct[number];
}

you have a memory leak. The reason is that you can't modify x (it's not passed
by reference). So when I do this:

void A ()
{
UVstruct* b;
cout << (void*)b << endl;
B(b); // pass the pointer "by value".
cout << (void*)b << endl; // same output as above,


What you need to do instead is something like this:

void B ( UVstruct ** x, int n )
{
*x = new UVstruct [n];
...
}

or better, use vector or some other standard container

Cheers,
 
R

Rob Williscroft

Chris Haynes wrote in
Hello all,

I have a structure:

Did you cut-n-paste this from some C code, the typedef is
unnessasery and you dont even use it.
typedef struct UVstruct {
float u, v;
} uv;

struct UVstruct {
float u, v;
};

Would do just fine.

Inside a function (A) i declare a pointer to an instance of this
structure:

UVstruct *pUV;

then from within the same function (A) i call another function (B)
that takes as one of its parameters the pointer declared above.

From within functionB i dynamically declare an array (pUVlocations is
the name of the passed pointer pUV within B):

change B from say:

void B( UVstruct * pUVlocations );

To:

void B( UVstruct * & pUVlocations );

pUVlocations = new UVstruct[number];

This will now assign to the pointer you passed in, not a copy of
it.
Still in B, I fill pUVlocations with some data. At the end of the
function pUVlocations contains all the data it should, and i do not
free pUVlocations.

However when the program has passed back to A, when I try to access
pUV it contains rubbish.

What am I doing wrong? Why does pUV not contain the data it was given
within B?

Because you changed the value of a *copy* not the original.

void does_nothing( int i )
{
/* this just changes the value of the *copy* that was passed
*/
i = 2;
}

int main()
{
int j = 3;
does_nothing( j ); /* passes a copy of j */

// j == 3 still.
}

I'd suggest you change B so that it returns the pointer, since
logicaly B() is creating the array.

UVStruct *B( /* whatever */)
{
UVStruct * pUVlocations = new UVstruct[number];
/* init ... */
return pUVlocations;
}

void A()
{
UVStruct *b = B();
}

Also consider using std::vector<> instread of trying to create
and manage your own array's.

#include <vector>

struct UVStruct {/*...*/};

either:

void B( std::vector< UVstruct > &uv_vect /* ,... */ )
{
uv_vector.resize( number );
/* init ... */
}

HTH.

Rob.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,417
Latest member
DarrenGaun

Latest Threads

Top