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. 2 2106
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/
Chris Haynes wrote in news:ba******** *************** ***@posting.goo gle.com: 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.resiz e( number );
/* init ... */
}
HTH.
Rob.
-- http://www.victim-prime.dsl.pipex.com/ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 clicked on, will open up the
full view in a CSS layer, as opposed to a pop-up window let's say. I had
this before, but with one layer for each image, and as you might imagine,
the markup was way too bloated. So can I just change the css layers...
|
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 enough to convert a favorite PHP
script to work on an ASP.NET site.
I'm experimenting with simple example scripts that will help me learn
how to implement each "piece" of the puzzle.
|
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");
}
|
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 have the following event handler functions defined and need to assign
them to table elements (<TD>) created dynamically in another function.
function handleMouseOver(elt, cssClass, strURL)
|
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 CheckBoxes with values returned from the array. So far all
I have are CheckBoxes without values across my screen.
Also, how would I place a "<br />" between all these CheckBoxes so that they
| |
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 form on which the users can click on a button to
add text boxes dynamically. That all works without a hitch. The problem
comes in trying to verify the information in the boxes.
Below is a very abbreviated example of the code I have in the form:
|
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 can not use the row 0, and the column 0.
|
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 multiple dynamic rows with chechboxs, input box for units of service, description of the service and each row has its own dropdown list of unit fees that apply.
|
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
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
| |
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |