pointer value

Started by
4 comments, last by AgPF6 9 years, 8 months ago

Hi

like below my app crashes:

float * pin28Value;
*pin28Value = 0.5;
XPLMSetDatavf(pnlBri, pin28Value, 10, 1);

pin28Value needs to be float*. Now I wonder what I'm doing wrong and

how to write the above better and or shorter. The very same but at another

place in my code works!

Thanks in advance

* XPLMSetDatavf
*
* Write part or all of a single precision floating point array dataref. The
* values passed by inValues are written into the dataref starting at
* inOffset. Up to inCount values are written; however if the values would
* write "off the end" of the dataref array, then fewer values are written.
*
* Note: the semantics of array datarefs are entirely implemented by the
* plugin (or X-Plane) that provides the dataref, not the SDK itself; the
* above description is how these datarefs are intended to work, but a rogue
* plugin may have different behavior.
*
*/
XPLM_API void XPLMSetDatavf(
XPLMDataRef inDataRef,
float * inValues,
int inoffset,
int inCount);

Advertisement

You are invoking undefined behavior (you are writing to an uninitialized pointer) and that will either crash or screw up a random part of your program (very bad because it might remain undetected for a very long time and when it starts crashing it will be days to track down the issue).

For this situation the solution would probably be:

float pin28Value = 0.5f;
XPLMSetDatavf(pnlBri, &pin28Value, 10, 1);

Well I tought & to take the address but not the value. Good thanks.

float * pin28Value; //create a pointer to some address

*pin28Value = 0.5; //write 0.5 to the location in memory that the pointer is pointing to

XPLMSetDatavf(pnlBri, pin28Value, 10, 1); //russian roulette

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

You are invoking undefined behavior (you are writing to an uninitialized pointer) and that will either crash or screw up a random part of your program (very bad because it might remain undetected for a very long time and when it starts crashing it will be days to track down the issue).

For this situation the solution would probably be:


float pin28Value = 0.5f;
XPLMSetDatavf(pnlBri, &pin28Value, 10, 1);

Bitmaster's solution is probably what you are looking for, but I want to show you another way to "solve" your problem, that can give another way of seeing what is happening:

Just want to warn that you should NOT do what I am showing now, but it tells why your code crashed.

This was your code:


float * pin28Value;
*pin28Value = 0.5;
XPLMSetDatavf(pnlBri, pin28Value, 10, 1);

If you just add one line:


float * pin28Value;
pin28Value = malloc(sizeof(float));
*pin28Value = 0.5;
XPLMSetDatavf(pnlBri, pin28Value, 10, 1);

I am not saying this is the way you should solve this problem, because it definitely isn't, but it should give a big hint why your code crashed.

You are creating a pointer to an object of size float, but you are never creating memory to store the actual float. When you dereference the pointer you are accessing memory that is not available to you. Becareful also that the memory you create stays within scope for the entire duration that the function you are calling requires it or you will get an access violation.

These are difficult to track down.

This topic is closed to new replies.

Advertisement