Sign in to follow this  
adam17

finicky new operator

Recommended Posts

adam17    227
ive been stressing over getting one of my dynamic arrays to function properly and everything compiles fine, it just crashes during runtime though. anyway here is what im trying to do. inside of the
quadnode
struct is a pointer of type GLfloat defined as:
GLfloat *vert_array
. in the constructor it is set to
vert_array = NULL;
void quadtree::add(quadnode *n, GLfloat *arr)
{
	int pos = n->numFace;

	n->numFace++;

	n->vert_array = new GLfloat[9];  //the error is always on this line

	/*for(int i=0; i<9; i++)
	{
		n->vert_array[pos*9+i] = arr[i];
	}*/
}

any ideas as to what could be causing this problem? thanks alot!

Share this post


Link to post
Share on other sites
darookie    1441
Have you tried to break into that location if the app crashes and taking a look at the variables? My guess would be that either the quadnode pointer is invalid or you are allocating new nodes over and over (due to a logical error elsewhere in your code) and the system simply runs out of memory.

EDIT: SiCrane must be some kind of bot [smile]

Share this post


Link to post
Share on other sites
adam17    227
eh damn, im sorry i made a mistake in my source post. i forgot to uncomment the for loop.

EDIT:

void quadtree::add(quadnode *n, GLfloat *arr)
{
int pos = n->numFace;

n->numFace++;

n->vert_array = new GLfloat[9];

for(int i=0; i<9; i++)
{
n->vert_array[pos*9+i] = arr[i];
}
}



what happens is the new operator does not allocate another 9 GLfloats. if i do a std::cout of the size, all i get is a size of 1. i do this (just so the math is right too)

std::cout << "sizeof(vert_array) => " << sizeof(vert_array)/sizeof(GLfloat) << std::endl;


it always outputs 1

Share this post


Link to post
Share on other sites
adam17    227
Quote:
Original post by RDragon1
How about using a std::vector for your dynamic array?


im using it for a vertex array in opengl. although i guess i could use a std::vector and then once its built, do a
GLfloat *vert_array[vector.size()];


EDIT: i forgot, you cant define dynamic arrays like that. they need a constant for the size when defining the array.

Share this post


Link to post
Share on other sites
darookie    1441
Quote:
Original post by adam17
std::cout << "sizeof(vert_array) => " << sizeof(vert_array)/sizeof(GLfloat) << std::endl;


it always outputs 1

This is not surprising at all - the sizeof operator doesn't work on dynamic memory. It is evaluated at compile time and thus cannot return the size of a dynamically allocated array. It will simply divide the size of the pointer by the size of the float, which happens to be one on machines wherepointers are the same size as a GLfloat (e.g. 32 bit).


Share this post


Link to post
Share on other sites
d00fus    328
If pos is anything other than 0 you are accessing outside the array bounds. When you say

Quote:

what happens is the new operator does not allocate another 9 GLfloats.


it sounds like you think it will *append* space for 9 floats - this is not the case. You will have to keep track of the size of the array yourself and grow it in size when necessary, basically the job a vector will do for you.

Share this post


Link to post
Share on other sites
Shannon Barber    1681
I use std::vector<float> for my vertext arrays. You can use &*my_vector.begin() to get the pointer and my_vector.size() for the number of floats in it.

.resize will make the vector a particular size
.push_back will append elements to it.

You can access elements using my_vector[index] just like a normal array.

Share this post


Link to post
Share on other sites
Aryabhatta    186
Your problem could be with this line of code:

n->vert_array[pos*9+i] = arr[i];

what values does pos*9 + i take?

vert_array is an array of GLFloats. The size of the array is 9.
You might be accessing memory locations way beyond the bounds of the array.

Also you are sure it is the new Glfloat.. line which is causing the problem?

Did you try turning optimizations off and debugging this on a debug version?

Share this post


Link to post
Share on other sites
adam17    227
Quote:
Original post by Aryabhatta
Your problem could be with this line of code:

n->vert_array[pos*9+i] = arr[i];

what values does pos*9 + i take?

vert_array is an array of GLFloats. The size of the array is 9.
You might be accessing memory locations way beyond the bounds of the array.

Also you are sure it is the new Glfloat.. line which is causing the problem?

Did you try turning optimizations off and debugging this on a debug version?


pos is the number of faces, and helps me with finding the last elements of the array. when its multiplied by 9 (3 vertices with 3 values (x, y, z)) it gives the proper offset with pos. i is just an iterator.

arr is always an array with 9 GLfloats in it.

when i run it pos is correct.

i also have all optimizations off.

i guess im going to try and do everything in std::vector and then when its done ill pass everything in a dynamic array and then into the vertex array.

Share this post


Link to post
Share on other sites
Zahlman    1682
Quote:
Original post by adam17
arr is always an array with 9 GLfloats in it.


Yes, and so is n->vert_array. Scroll back up and reread d00fus' post. The 'new' does not expand an existing array allocation; it replaces it (rather, it causes the pointer to point at a new allocation, and the old allocation is forgotten about. So not only do you still have 9 elements, you also have a memory leak.) If pos is anything greater than 0, then you create an array index >= 9, and use it for a 9-element array - boom.

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