Visual C++ 6 odd problem (new operator)

Recommended Posts

I'm having a very odd problem with Visual Studio 6 debugging my code (aside from the normal visual C++ bugs... )... basically, I'm allocating some vertex and edge lists on the fly, and for whatever reason, in my face/edge list copy constructor, it cannot allocate an array - here's the code (for part of the copy constructor for the face list object):
#define int32 unsigned long

typedef struct SFaceList {
int32 *edges;
int32 nEdges;

SFaceList(const SFaceList &p) {
//... snipped ...
if (p.edges) {
nEdges=p.nEdges;
edges=new int32[nEdges*3]; // <--- fails here
memcpy(edges, p.edges, nEdges*3*sizeof(int32));
}
//... snipped ...
}
};

For whatever reason, the new operator above always returns NULL - I'm not obliterating my memory allocation stack (the stack is set at ~1MB reserved, and the current memory usage at the point of failure is only around 180K)... I am doing the same type of allocation in other parts of my code, which is working fine, but for whatever reason, it's failing in this copy constructor - has anyone else experienced the same / similar problems? Anyone know how to get around this mess? Thanks much!

Share on other sites
What is the value of nedges prior to the call?
Does it work if you replace nedges by a correct, and large enough, value, in the call to new?

Share on other sites
Quote:
 Original post by ToohrVykWhat is the value of nedges prior to the call?Does it work if you replace nedges by a correct, and large enough, value, in the call to new?

nEdges is set at 294 when it calls... I've also tried a static allocation with "edges=new int32[884]" and it still gives the same result - edges=0x0000000 (NULL).

I should also mention that this happens on the first call globally to the copy constructor... all the previous calls to instantiate an object of this type were all SFaceList() (which doesn't allocate any memory internally), and then I allocate the memory from whereever I am (which does work).

I even tried it with allocating a new object of SFaceList normally (i.e. =new SFaceList()) and writing an = assignment operator method - same problem (SFaceList &operator = (const SFaceList& p))

Share on other sites
Are you sure that 'p.edges' gets set properly?

Share on other sites
Quote:
 Original post by DJHoy#define int32 unsigned long typedef struct SFaceList { int32 *edges; int32 nEdges;SFaceList(const SFaceList &p) { //... snipped ... if (p.edges) { nEdges=p.nEdges; edges=new int32[nEdges*3]; // <--- fails here memcpy(edges, p.edges, nEdges*3*sizeof(int32)); } //... snipped ...}};

That is some crazy code, don't use define's for type aliases thats what typedef is for, and typedef'ing structures like that is redudndant in c++.

I suggest you don't use C style dynamic arrays use C++ dynamic arrays std::vector, then you don't have to explicitly define a copy constructor & assigmenet operator the default implicitly defined will do that correctly as std::vector will do the wright copying/assignement for you e.g.

#include <vector>struct SFaceList {  typedef unsigned long int32;  typedef std::vector<int32> edge_list;    edge_list edges;  SFaceList(edge_list::size_type n = 0, int32 def_val = 0)  : edges(n, def_val) {}  /* no need to explicitly define copy & assignement ops, implicitly defined by compiler is appropriate and is better than hand written one. */    };

Share on other sites
Zahlman:

Yes, p.nEdges is set correctly (as well as p.edges)

snk_kid:

Quote:
 Original post by snk_kidThat is some crazy code, don't use define's for type aliases thats what typedef is for, and typedef'ing structures like that is redudndant in c++.

Good idea... I flip back and forth between my usage of #define and typedef when it comes to things like this (whatever's more convienient at the time...)... I switched things back to "typedef unsigned long int32;" (same problem though...)

Quote:
 Original post by snk_kidI suggest you don't use C style dynamic arrays use C++ dynamic arrays std::vector, then you don't have to explicitly define a copy constructor & assigmenet operator the default implicitly defined will do that correctly as std::vector will do the wright copying/assignement for you e.g.

I could, but for as simple of a thing as I'm doing, I don't want to bother with the overhead of STL pieces... besides, I've done things like this is the past, and the code above is the only time I've encountered a problem...

Thanks both for trying to help.

Create an account

Register a new account

• Forum Statistics

• Total Topics
628385
• Total Posts
2982391

• 10
• 9
• 15
• 24
• 11