Jump to content
  • Advertisement
Sign in to follow this  
MARS_999

Error on pointer math

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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));

			}
		}
	}






Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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*/.

Share this post


Link to post
Share on other sites
(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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!