Sign in to follow this  
DJHoy

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 this post


Link to post
Share on other sites
Quote:
Original post by ToohrVyk
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?


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).

[edit]
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 this post


Link to post
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 this post


Link to post
Share on other sites
Zahlman:

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

snk_kid:

Quote:
Original post by snk_kid
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++.


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_kid
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.


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.

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