473,408 Members | 2,009 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Dynamically declaring an array within a function

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.
Jul 22 '05 #1
2 2079
In article <ba**************************@posting.google.com >, Chris Haynes wrote:
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,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
Jul 22 '05 #2
Chris Haynes wrote in news:ba**************************@posting.google.c om:
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.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: TheKeith | last post by:
I don't know much about javascript, so take it easy on me. Is there a way to dynamically change a CSS layers dimensions on the fly. Here is what I'm doing. I have a bunch of thumbnails that when...
29
by: Friday | last post by:
Sorry if this is the wrong group. I tried to find the one I thought would be most relevant. I'm an old PHP guy, who knows little about asp and NOTHING about asp.net, but need to learn at least...
8
by: Falc2199 | last post by:
Hi, Does anyone know how to make this work? var sectionId = 5; repeat_section_sectionId(); function repeat_section_5(){ alert("firing"); }
3
by: N. Demos | last post by:
How do you dynamically assign a function to an element's event with specific parameters? I know that the code is different for MSIE and Mozilla, and need to know how to do this for both. I...
2
by: Jeff Johnson | last post by:
Hi, Does anyone know how I would code a dynamically generated CheckBoxList within a PlaceHolder? I have a function that returns an array, I then want to loop throuth the array and create...
2
by: Sethos | last post by:
I am sure that this has been covered, hashed, and rehashed, but a search on the group did not produce the answer, so forgive me if this seems like a "newbie" type question... Besically, I have a...
2
by: xhunga | last post by:
I have try a new version of my work. I have put the sizes of the matrix into the matrix. A = number of rows A = number of columns The first element of the matrix is A instead of A. You...
4
by: assgar | last post by:
Hi I am stuck on a problem. I use 3 scripts(form, function and process). Development on win2003 server. Final server will be linux Apache,Mysql and PHP is being used. The form displays...
5
by: miladhatam | last post by:
how can i declare a varible dynamically like this : for (i=1 ; i<10;i++){ int ("j" + i) = i ; //declaring j1 - j9 } ofcourse this code is wrong and it is an algorithm
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.