Sign in to follow this  
Quinnie

C++ New And Allocation

Recommended Posts

Hey, maybe a dumb question but I'm having a hard time with this. I have a class called A declared like this: class B; class A { public: A(); B *Children; }; The constructor does the following: this->Children = new B[50]; Then my main function looks like this: int main() { A Test; Test.Children[23].membervariable = 5; } This however does not work, somehow when the constructor is returned Children gets deallocted or something... Any help is appreciated! [Edited by - Quinnie on March 9, 2007 6:49:30 PM]

Share this post


Link to post
Share on other sites
1.) Well if I set this in the constructor
this->Children[30].membervalue = 7;

then in the main function this value does not exist anymore....

2.) it says {Length=50}

Share this post


Link to post
Share on other sites
Quote:
Original post by Quinnie
1.) Well if I set this in the constructor
this->Children[30].membervalue = 7;

then in the main function this value does not exist anymore....


Fine. Then, use your debugger to determine when this->Children[30].membervalue changes (because, it's 7 before the return, and something else after, so it has to change sometime). This is done using a variable watch.

Share this post


Link to post
Share on other sites
Well after i set it to 7 it is 7 in the watch
then i step to return; it is still 7... then after return; it goes back to main and then poof it changes to 0..

Share this post


Link to post
Share on other sites
Could you try without stepping? Let the program run and interrupt itself when it executes an operation that changes the value? "When the constructor returns" is a pretty vague piece of information when the debugger would be able to pinpoint the exact machine code instruction (and corresponding source) where this happens.

Aside from that, your problem is very strange. Reasons why this could happen are:

1° You're not looking at the same object: maybe a copy constructor intervened, or maybe you're just looking at something else.

2° The array was redirected to somewhere else, by outside action.

3° The array contents were overwritten, by outside action.

So, it's necessary to pinpoint the exact instruction which causes the change (and which of the three options actually happens).

Share this post


Link to post
Share on other sites
Hmm not sure how to set such a breakpoint... I'm using visual studio .net 2003 when trying it gives me an error:

"The breakpoint is not valid. Data breakpoints are not supported in the Common Language Runtime."

Share this post


Link to post
Share on other sites
I decided to post some more code, this code failes with this error:
"Unhandled exception at 0x7c9105f8 in test.exe: 0xC0000005: Access violation reading location 0x00000010."

The actual error happens at this line:
InstanceB = new B[20];

Heres the source:

class B
{
public:
int MemberValue;
};

class A
{
public:
A()
{
InstanceB = new B[20];
InstanceB[2].MemberValue = 9;
}

B* InstanceB;
};

int main()
{
A InstanceA;

int c = InstanceA.InstanceB[2].MemberValue;

return 0;
}




I must be doing something wrong I just cant see what it is. Any help would be appreciated!

Share this post


Link to post
Share on other sites
Hold up, you're using the CLR (Common Language Runtime)? You should probably turn that off, unless you atually want to use .Net stuff. You can do that in the project properties (Configuration Support -> General). That may have something to do with it, maybe not. I doubt it, but it's still best not to have it on if you aren't using it.

Share this post


Link to post
Share on other sites
Hey, the program I'm making does actually use de clr however turning it off or on doesn't really change anything.... I'm still having the error and I would really like to know what is causing it.

To give another example:
int main()
{
int *TestVar = new int[20];

return 0;
}

Again at the new statement I get an error:
"Unhandled exception at 0x7c9105f8 in test.exe: 0xC0000005: Access violation reading location 0x00000010."

The error appears in de malloc.c file.

Any help would be greatly appreciated!

Share this post


Link to post
Share on other sites
Ummm... this seems to be kind of weird to me but maybe its not... I changed the Runtime Library from "Single-threaded Debug (/MLd)" debug to "Multi-threaded DLL (/MD)" and everthing works like a charm.

Are you supposed to set it to "Multi-threaded DLL (/MD)" when you want to use new or malloc statements?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this