bizarre - instance of class non-dynamic?
I have a weird problem here which I hope someone can explain.
I have a class declared like this:
struct fruit
{
double apple;
double grapefruit;
double salad;
};
I create a global instance of the class in one of my .cpp files like so:
fruit frt;
Then, I call a procedure in the .cpp which sets the starting values for apple, grapefruit, and salad, before any of the other procedures/functions in the .cpp which refer to frt are called, using:
frt.apple = 5.0;
frt.grapefruit = 2.0;
Now, everything works fine when I setup frt this way (I refer to 'frt.apple' using a '.' of course). BUT....As soon as I try to dynamically allocate frt, I start to get unexpected values in the apple & grapefruit members:
fruit *frt = new fruit;
fruit->apple = 5.0;
fruit->grapefruit = 2.0;
Does anyone have an explanation for why there are unexpected values in the fruit->apple and fruit->grapefruit members when I refer to these values in other procedures (now using '->'). I know that the members will have junk in them when the instance of the class is created (when the memory is first allocated), but I thought assigning starting values like I have done, before the members are used/referenced would fix that.
Please explain!
Paulcoz.
Edited by - paulcoz on January 21, 2001 8:41:08 PM
quote: Original post by paulcoz
Does anyone have an explanation for why there are unexpected values in the fruit->apple and fruit->grapefruit members when I refer to these values in other procedures (now using ''->''). I
How are you handing the fruit object to the other procedures? If frt is still supposed to be global it looks like the one you are creating ( has the same name ) is local to that function and the global one is still not initialized.
-------
Andrew
Just to clarify:
struct ''fruit'' is defined at the top of the .cpp.
fruit *frt = new fruit; is global - it is declared outside all of the other procedures.
When I refer to frt->apple in the other procedures, I am referring to the global instance (eg. there is no additional local instance).
Regards,
Paulcoz.
struct ''fruit'' is defined at the top of the .cpp.
fruit *frt = new fruit; is global - it is declared outside all of the other procedures.
When I refer to frt->apple in the other procedures, I am referring to the global instance (eg. there is no additional local instance).
Regards,
Paulcoz.
Ok. I did up a little test program. I don''t know if this is exactly what you are doing but this produces the right output. You could try tracing with your debugger to see where your global is getting changed.
-------
Andrew
struct fruit{ double a; double b; double c;};// Global definedfruit *g_f = new fruit;// Test global varvoid test(){ cout << "f.a = " << g_f->a << endl; cout << "f.b = " << g_f->b << endl;}//Init Global Varvoid init(){ g_f->a = 6743; g_f->b = 64.50;}int main(){ init(); test(); return 0;}
-------
Andrew
Your code looks just like mine - the only difference is, my class contains another class:
struct green
{
double greenapple;
double grape;
};
struct fruit
{
green g;
};
fruit *frt = new fruit;
frt->g.greenapple = 5.2;
That doesn't make any difference though, does it?
Paulcoz.
Edited by - paulcoz on January 22, 2001 1:11:57 AM
struct green
{
double greenapple;
double grape;
};
struct fruit
{
green g;
};
fruit *frt = new fruit;
frt->g.greenapple = 5.2;
That doesn't make any difference though, does it?
Paulcoz.
Edited by - paulcoz on January 22, 2001 1:11:57 AM
heum i dont know much of declaring a struct this way but me i do this this way:
typedef struct tag_green
{
double greenapple;
double grape;
}green;
typedef struct tag_fruit
{
green g;
}fruit;
fruit *frt = new fruit;
frt->g.greenapple = 5.2;
it seems that if you dont typedef the struct you will have to declare the variables like that:
struct fruit *frt = new struct fruit;
and same thing for green in the struct fruit
typedef struct tag_green
{
double greenapple;
double grape;
}green;
typedef struct tag_fruit
{
green g;
}fruit;
fruit *frt = new fruit;
frt->g.greenapple = 5.2;
it seems that if you dont typedef the struct you will have to declare the variables like that:
struct fruit *frt = new struct fruit;
and same thing for green in the struct fruit
cyberg- cyberg_coder@hotmail.com- http://members.xoom.com/cybergsoft
January 22, 2001 01:48 AM
cyberg, you only need to do that for c., with c++ you do not need to typedef the struct
quote: Original post by paulcoz
frt->g.greenapple = 5.2;
That doesn''t make any difference though, does it?
It shouldn''t make any difference. I''d suggest two things. First make sure that you are displaying the correct data and two trace through with the debugger and see where changes to the global occur.
-------
Andrew
And in future to avoid this kind of problem, learn how to use constructors
Fruit::Fruit(){apple = 5.0;grapefruit = 2.0;}
O.k, this is starting to get very annoying.
I am dynamically allocating memory for another class now, and I am still getting strange values in the members, even when I make a point of assigning valid values to them beforehand. I have also tried to clear the memory (to get rid of junk) using ZeroMemory, immediately after allocation. Still no luck.
I don't suppose anyone has any other ideas on what might be causing this? Once I have used 'new' to dynamically allocate memory, is there anything else I should do to make sure the allocated memory is blank before I use it? What is the most efficient/common command to do this?
Thanks again,
Paulcoz.
Edited by - paulcoz on January 27, 2001 1:05:59 AM
I am dynamically allocating memory for another class now, and I am still getting strange values in the members, even when I make a point of assigning valid values to them beforehand. I have also tried to clear the memory (to get rid of junk) using ZeroMemory, immediately after allocation. Still no luck.
I don't suppose anyone has any other ideas on what might be causing this? Once I have used 'new' to dynamically allocate memory, is there anything else I should do to make sure the allocated memory is blank before I use it? What is the most efficient/common command to do this?
Thanks again,
Paulcoz.
Edited by - paulcoz on January 27, 2001 1:05:59 AM
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement