Sign in to follow this  
Ainokea

Converting C memory management to C++

Recommended Posts

Ainokea    435
I was reading through some old C code for a game a few days ago and I am confused about how to convert c code to c++ in areas where they do things like this:
memset (p->texture.texName, '\0', sizeof (p->texture.texName));

p->verts = malloc (sizeof (vec3_t) * p->numVertex);

//And though they don't declare the pointer as an array they access it like one:
//Accesing it:
	fscanf (s, "%d", &index);
	fscanf (s, "%f %f %f", 
		&p->faces[index].normal[0], 
		&p->faces[index].normal[2],
		&p->faces[index].normal[1]); 

	p->faces[index].normal[2] = -p->faces[index].normal[2];

//Declaring it:
triangle_t *faces;

Im trying to figure out how you would convert that to something like new and delete, etc..

Share this post


Link to post
Share on other sites
jyk    2094
Basically you can 'new' an object:

Object* object = new Object;

Or an array of objects:

Object* objects = new Object[numObjects];

To delete a single object:

delete object;

To delete an array:

delete [] objects;

Share this post


Link to post
Share on other sites
Oluseyi    2103
p->verts = malloc (sizeof (vec3_t) * p->numVertex);

becomes

p->verts = new vec3_t[p->numVertex];

The key is to realize that whatever is being queried for its size (sizeof(X)) is a type and thus becomes new X, and its multiplicand in the C version is the dimension.

The rest has nothing to do with memory allocation. Learn your pointers, though; you seem a little fuzzy on them.

Share this post


Link to post
Share on other sites
Telastyn    3777
You don't? I mean vec3_t won't have a constructor, so it won't really serve a purpose.

I've always used plain jane C when fiddling with normal arrays, and don't know if C++ will even let you new for an variably sized array nicely.

The C++ style if it works would be something like

vec3_t p->verts[p->numVetrex]=new vec3_t;


And since this will require a contiguous memory block, container classes are right out.... C does have it's uses.

Share this post


Link to post
Share on other sites
Zahlman    1682

//memset (p->texture.texName, '\0', sizeof (p->texture.texName));

// This 'name' is presumably a char* or char[] holding "string data", so you'd
// probably want to replace the field with a std::string - which then wouldn't
// need this initialization at all. However, if you went that route, it would
// have an effect on the (de)serialization process... Anyway, memset() should
// work as is in C++.

//p->verts = malloc (sizeof (vec3_t) * p->numVertex);

p->verts = new vec3_t[p->numVertex]; // as indicated.

// Of course, you might consider using a std::vector (which would keep track
// of its length for you). Also, this looks like initialization-type code,
// so you may be able to move it into a ctor and avoid the p-> business.

//And though they don't declare the pointer as an array they access it like one:
//Accesing it:
// fscanf (s, "%d", &index);
// fscanf (s, "%f %f %f",
// &p->faces[index].normal[0],
// &p->faces[index].normal[2],
// &p->faces[index].normal[1]);
// p->faces[index].normal[2] = -p->faces[index].normal[2];

// Which, 'faces'? Doesn't matter. This is array-name/pointer equivalence.
// Note array-*name*. :)
// Anyway, in C++ style, with s being a std::stringstream:
s >> index;
s >> p->faces[index].normal[0] >> p->faces[index].normal[2] >> p->faces[index].normal[1];
p->faces[index].normal[2] = -p->faces[index].normal[2];
// Again, see about getting rid of the p->. You might even put this part into
// a constructor for the 'face' objects. (Or create a reference to 'normal' to
// avoid the redundancy.)

//Declaring it:
triangle_t *faces;
// Still the same.

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