finicky new operator

This topic is 4636 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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;
}*/
}


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

Share on other sites
What kind of error message do you get? You may have heap corruption.

Share on other sites
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 on other sites
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;	}}

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 on other sites
Quote:
 Original post by RDragon1How 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 on other sites
Quote:
 Original post by adam17std::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 on other sites
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 on other sites
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 on other sites
Your problem could be with this line of code:

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

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?

1. 1
2. 2
frob
16
3. 3
4. 4
5. 5

• 13
• 13
• 61
• 14
• 15
• Forum Statistics

• Total Topics
632125
• Total Posts
3004252

×