Error on pointer math

Started by
16 comments, last by Zahlman 17 years, 4 months ago
I have a crash on this code and can't figure out the math for the pointer offsets.

float *ptr;
Noise3DTexPtr = (float*) malloc(NOISE3DTEXSIZE * NOISE3DTEXSIZE * NOISE3DTEXSIZE * 4);

for (f = 0, inc = 0; f < numOctaves; ++f, frequency *= 2, ++inc, amp *= 0.5)
{
	SetNoiseFrequency(frequency);
	ptr = Noise3DTexPtr;
	ni[0] = ni[1] = ni[2] = 0;

	inci = 1.0 / (NOISE3DTEXSIZE / frequency);
	for (i = 0; i < NOISE3DTEXSIZE; ++i, ni[0] += inci)
	{
	    incj = 1.0 / (NOISE3DTEXSIZE / frequency);
		for (j = 0; j < NOISE3DTEXSIZE; ++j, ni[1] += incj)
		{
			inck = 1.0 / (NOISE3DTEXSIZE / frequency);
			for (k = 0; k < NOISE3DTEXSIZE; ++k, ni[2] += inck, ptr += 4)
				//error on the line below is where it crashes 
*(ptr + inc) = float((((noise3(ni) + 1.0) * amp) * 128.0));

			}
		}
	}






Advertisement
just to make sure we're on the same page. The array you have malloc'd appears to be a 3-dimensional array of something that is 4 bytes in size. Is this what you intended?

If that's the case, an x,y,z iteration would look like this:

for ( int i = 0; i < NOISE3DTEXSIZE; ++i ){    for ( int j = 0; j < NOISE3DTEXSIZE; ++j )    {        for ( int k = 0; k < NOISE3DTEXSIZE; ++k )        {            //i think this is correct...            int offset = (i * NOISE3DTEXSIZE * NOISE3DTEXSIZE) + (j * NOISE3DTEXSIZE) + k;            Noise3DTexPtr *cur = Noise3DTexPtr[offset];        }    }}


-me
This is for making a noise texture and was using unsigned byte for types but now I want to use floats and figured I only need to change the pointers and get rid of the type cast to unsigned char and leave it as a float. But I am not following the math and thinking I am not allocating enough memory so sizeof(float) * noise3dtexsize * noise3dtexsize * noise3dtextsize * 4 is probably correct now that I think about it...
Quote:Original post by MARS_999
float(/* stuff */)

As far as I'm aware, that ain't valid C. Which begs the question which language are you using and why aren't you using it to its full potential?

Σnigma
Quote:Original post by Enigma
Quote:Original post by MARS_999
float(/* stuff */)

As far as I'm aware, that ain't valid C. Which begs the question which language are you using and why aren't you using it to its full potential?

Σnigma


It actually is, it's the same as (float)/*some stuff*/.
(float) x //is C casting

float(x) // is C++ casting I think it uses a constructor like method

static_cast<float>(x) is C++ also

In C++ it is. I can't find any reference to it being valid in any standard dialect of C though, and it doesn't compile as C under any of my compilers. Either the OP wants to be coding in C and thus that line is an error or he is coding in C++ and there are far superior ways of coding what he wants that using malloc (Boost.MultiArray or at least a std::vector implementation with bounds checking in debug spring to mind).

EDIT: That was in reply to TrueTom

Σnigma
Not too familiar with noise functions, but the first thing I thought of is: Why are you doing all preincrements rather than postincrements? (This might cause unexpected behaviour in your loops, i.e. never starting at zero and fooling the less-than conditionals.)
Quote:Original post by spartanx
Why are you doing all preincrements rather than postincrements? (This might cause unexpected behaviour in your loops, i.e. never starting at zero and fooling the less-than conditionals.)

No, it does not.
for (int i=0; i<10; ++i)

Will count from 0 to 9.
Quote:Original post by spartanx
Not too familiar with noise functions, but the first thing I thought of is: Why are you doing all preincrements rather than postincrements? (This might cause unexpected behaviour in your loops, i.e. never starting at zero and fooling the less-than conditionals.)

A for loop of the form

for (initialisation; condition; expression) statement;

is equivalent to:
{	initialisation;	while (condition)	{		statement;		expression;	}}
Since expression is executed as a separate statement at the end of the loop preincrement and postincrement will have identical effect (although in C++ preincrement may be more efficient for user-defined types). There will be no "unexpected behaviour" or "fooling the less-than conditionals".

Σnigma

This topic is closed to new replies.

Advertisement