C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

.net memory allocation question

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
laniik
Guest





PostPosted: Fri Mar 11, 2005 11:30 am    Post subject: .net memory allocation question Reply with quote



Hi, i have a program that runs fine on vc6. I am trying to port it to
..net 2003, but i am having some memory problems.

here is the code that is causing me grief:

struct Point3f
{
float x,y,z;
};

--- snip
int numLines=5;
Point3f* pOuts = new Point3f[numLines];
---

causes a crash because new cannot allocate any more memory.
i am not allocating any memory with the new or malloc operator before
this line.

also note:
Point3f pOuts[10000]; //or 5 for that matter

does not cause a crash. what is the difference between these two
operators. i cannot use the latter because the size that i have to
malloc changes dynamically.

Thanks for the help!

oliver


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ben Hutchings
Guest





PostPosted: Fri Mar 11, 2005 3:15 pm    Post subject: Re: .net memory allocation question Reply with quote



laniik wrote:
Quote:
Hi, i have a program that runs fine on vc6. I am trying to port it to
.net 2003, but i am having some memory problems.

here is the code that is causing me grief:

struct Point3f
{
float x,y,z;
};

--- snip
int numLines=5;
Point3f* pOuts = new Point3f[numLines];
---

causes a crash because new cannot allocate any more memory.
snip


No, the crash is due to the code you didn't post. You may be quite
certain that that other code is correct, but you're almost certainly
wrong. (It *is* possible that the other code triggers a bug in the
implementation, but this doesn't happen very often.)

Have your rebuilt the entire program using VC++ 7.1 (VS.NET 2003)
or are you using libraries that were compiled using VC++ 6?

Ben.

--
Ben Hutchings
Having problems with C++ templates? Your questions may be answered by
<http://womble.decadentplace.org.uk/c++/template-faq.html>.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
laniik
Guest





PostPosted: Sun Mar 13, 2005 12:36 pm    Post subject: Re: .net memory allocation question Reply with quote



yes. i am aware that the code i posted is correct as i have done
similar things many times. also my whole program is compiled using vc
..net 2003.

i thought it might be something wrong with the heap/freestore settings
as memory allocation works only at compile time.

the other alternative is that there is somthing that i could be doing
in the code before that that could be damaging the memory so that it
cannot allocate. is this possible?

the only memory related thing that I am doing in my code is this:

struct Cluster
{
Point3f* points;
};

int k=4;
Cluster* cluster = new Cluster[k];


i am not however, allocating the points array before i do the
allocation above. could this be causing a problem? as it know how big
a Cluster is?

The other confusing thing is that it works just find in VC6.0. Which
leads me to believe that its not somthing in the code.

Thanks a lot. Im trying to learn how all this allocation stuff works.

Oliver


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Ben Hutchings
Guest





PostPosted: Mon Mar 14, 2005 2:15 pm    Post subject: Re: .net memory allocation question Reply with quote

laniik wrote:
Quote:
yes. i am aware that the code i posted is correct as i have done
similar things many times. also my whole program is compiled using vc
.net 2003.

i thought it might be something wrong with the heap/freestore settings
as memory allocation works only at compile time.

the other alternative is that there is somthing that i could be doing
in the code before that that could be damaging the memory so that it
cannot allocate. is this possible?

That is possible. A bug elsewhere may result in writing to memory
referenced by an uninitialised or otherwise invalid pointer, and so
corrupting the heap.

Quote:
the only memory related thing that I am doing in my code is this:

struct Cluster
{
Point3f* points;
};

int k=4;
Cluster* cluster = new Cluster[k];


i am not however, allocating the points array before i do the
allocation above. could this be causing a problem? as it know how big
a Cluster is?

That should not be a problem. However, reading the uninitialised
"points" member of any of the newly-allocated Cluster objects results
in undefined behaviour. In practice it is likely to be harmless
unless and until you attempt to dereference it, e.g. evaluating
cluster[0].points[0] is likely to crash the program.

Quote:
The other confusing thing is that it works just find in VC6.0. Which
leads me to believe that its not somthing in the code.

It is entirely possible and regrettably common for bugs that lead to
"undefined behaviour" to be harmless in some circumstances but
devastating in others.

Quote:
Thanks a lot. Im trying to learn how all this allocation stuff works.

--
Ben Hutchings
Having problems with C++ templates? Your questions may be answered by
<http://womble.decadentplace.org.uk/c++/template-faq.html>.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ismail Pazarbasi
Guest





PostPosted: Mon Mar 14, 2005 11:55 pm    Post subject: Re: .net memory allocation question Reply with quote

did you see anything on Output window? You may see something like "Heap
address 0xbaadf00d blah blah..."
there might also be a problem with the stack which would cause such a
failure. check out your stack allocations as well.

Ismail


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Junker
Guest





PostPosted: Tue Mar 15, 2005 8:38 pm    Post subject: Re: .net memory allocation question Reply with quote

"laniik" <laniik (AT) yahoo (DOT) com> wrote

Quote:
yes. i am aware that the code i posted is correct as i have done
similar things many times. also my whole program is compiled using vc
.net 2003.

i thought it might be something wrong with the heap/freestore settings
as memory allocation works only at compile time.

the other alternative is that there is somthing that i could be doing
in the code before that that could be damaging the memory so that it
cannot allocate. is this possible?

the only memory related thing that I am doing in my code is this:

struct Cluster
{
Point3f* points;
};

int k=4;
Cluster* cluster = new Cluster[k];


i am not however, allocating the points array before i do the
allocation above. could this be causing a problem? as it know how big
a Cluster is?

The other confusing thing is that it works just find in VC6.0. Which
leads me to believe that its not somthing in the code.

Thanks a lot. Im trying to learn how all this allocation stuff works.

Oliver

If you are having memory allocation issues under Windows, and using
the VC compilers, I'd recommend having a look at the debug CRT
functions. They are quite good for tracking down memory corruption
issues, although they will slow down execution of your program (as the
heap is scanned for corruption).

You should be able to find information about these functions on MSDN
or the web, and the function to start looking at is the
_CrtCheckMemory() function.

If you call this function it will check the heap for corruption and
fire an assert if the heap has been corrupted.

Hope this helps,

Dan.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ron Ruble
Guest





PostPosted: Wed Mar 23, 2005 10:43 pm    Post subject: Re: .net memory allocation question Reply with quote

Ben Hutchings wrote:
Quote:
laniik wrote:

<snip>
Quote:
The other confusing thing is that it works just find in VC6.0. Which
leads me to believe that its not somthing in the code.


It is entirely possible and regrettably common for bugs that lead to
"undefined behaviour" to be harmless in some circumstances but
devastating in others.

Particularly porting from VC 6 to VC 7. That's
the first upgrade in a -long- time that changed
any of the longstanding non-compliant behavior
in VC.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.