sizeof(float)/sizeof(float[0])
-
my array is declared as following:
float fVertexes[6];
I'm inserting this values;fVertexes[0] = 1.0; fVertexes[1] = 0.0; fVertexes[2] = -1.0; fVertexes[3] = 1.0; fVertexes[4] = -1.0; fVertexes[5] = -1.0;
I'm passing this guy to a function, like this:primitive.Shape(GL_TRIANGLES, fVertexes, 1,1,1);
Again, why it returns 1 if I don't create this guy dynamically?That is an example of the decay I mentioned in my post - when you pass the array, it becomes a pointer in the called function. The function that has the pointer has no "internal" or automatic way of knowing how much the data points to. That is why whenever you pass an array of data to a function, you usually have to tell the function how much valid data is in the array (or have a special element in the array to indicate the end of valid data). Peace!
-=- James
If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
DeleteFXPFiles & CheckFavorites (Please rate this post!) -
The math is working: 4/4=1. The problem looks like
dVertexes
is not a true array. It is an atomic type (double
) or a pointer. Note that usingsizeof(...)
in this way does not work correctly with pointers to arrays, even if they are pointers that have decayed from a true array. Peace!-=- James
If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
DeleteFXPFiles & CheckFavorites (Please rate this post!)ok, so how can I discover the array size? I don't want to use vectors....
-
ok, so how can I discover the array size? I don't want to use vectors....
You have to pass the size to the called function to let it know how much valid data exists. Peace!
-=- James
If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
DeleteFXPFiles & CheckFavorites (Please rate this post!) -
my array is declared as following:
float fVertexes[6];
I'm inserting this values;fVertexes[0] = 1.0; fVertexes[1] = 0.0; fVertexes[2] = -1.0; fVertexes[3] = 1.0; fVertexes[4] = -1.0; fVertexes[5] = -1.0;
I'm passing this guy to a function, like this:primitive.Shape(GL_TRIANGLES, fVertexes, 1,1,1);
Again, why it returns 1 if I don't create this guy dynamically? -
ok, so how can I discover the array size? I don't want to use vectors....
Depending on how often you are passing these arrays around and how frequently the size changes, you might want to declare a struct with the array and the size as members, then pass that to your functions.
typdef struct _double_array
{
double array[9];
int nCount;
} DOUBLE_ARRAY, *LPDOUBLE_ARRAY;You can then pass the stuct
'DOUBLE_ARRAY'
or a pointer to the struct'LPDOUBLE_ARRAY'
. -
Hi, Just modify
Shape
function by addingINT nCount
after pointer to array of floats. Then inShape
function, calculate array size asnCount*sizeof(float)
.nCount
should specify the size of the array. RegardsI made this: I've passed the size of the array(like you've said) and divided by float[0] and works fine
-
You have to pass the size to the called function to let it know how much valid data exists. Peace!
-=- James
If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
DeleteFXPFiles & CheckFavorites (Please rate this post!)I've made this: primitive.Shape(GL_TRIANGLES, fVertexes, sizeof(fVertexes), 0,1,1); And in the function I'm divided the size of array by the size of first element in the array. Works fine, thanks for help
-
Depending on how often you are passing these arrays around and how frequently the size changes, you might want to declare a struct with the array and the size as members, then pass that to your functions.
typdef struct _double_array
{
double array[9];
int nCount;
} DOUBLE_ARRAY, *LPDOUBLE_ARRAY;You can then pass the stuct
'DOUBLE_ARRAY'
or a pointer to the struct'LPDOUBLE_ARRAY'
.Thanks for the tip. It's a good idea
-
Alex Cutovoi wrote:
Why it returns 1?
4/4 = 1 4 = size of a pointer is dVertexes allocated dynamically ? if so, you can't use that sizeof trick to get its size. -c
I made some modifications and works fine. Thanks for the support
-
Thanks for the tip. It's a good idea
No problem. If you look into the windows api's, you will find that many of the functions are declared like this, you either have to set up a struct or you get one back. My current project calls on the need of dealing with several arrays all relating to the same info, so instead of passing all the arrays and sizes to each function, I simply pass a pointer to a struct.
-
Hi fellows I have a single question. I have a float array that have 6 elements all of them initialized with values. I need to find out the size of this array. Let's called my float array of theArray. I use this commom formula: sizeof(theArray)/sizeof(theArray[0]). My results are strange, because it returns 1. I use this code to see the values:
char cTemp[50]; sprintf(cTemp, "%d %d %d", sizeof(dVertexes), sizeof(dVertexes[0]), sizeof(dVertexes)/sizeof(dVertexes[0])); MessageBox(NULL, cTemp, "", MB_OK);
In the first and in the second values it returns "4", and in the third value it returns "1". Why it returns 1? Did this operation returns the correct size of array(in my case "6")? thanks for the supportIf the array is allocated on the heap, sizeof behaves differently than it does if it was on the stack.
int i = 0; float stackArray[5]; i = sizeof(stackArray) / sizeof(stackArray[0]); // i is 5 // sizeof(stackArray) will return (5 * sizeof(float)) // sizeof(stackArray[0]) will return sizeof(float) float* heapArray = new float[5]; i = sizeof(heapArray) / sizeof(heapArray[0]); // i is 1 // sizeof(heapArray) will return sizeof(float*) // sizeof(heapArray[0]) will return sizeof(float) // Interesting Note (forget bounds errors here) int myFunction(float* myArray) { return sizeof(myArray) / sizeof(myArray[0]); } // above function will return heap results regardless of where // the float array is actually allocated (that is, always 1)
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac