Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


pointer value


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 FGFS   Members   -  Reputation: 214

Like
0Likes
Like

Posted 16 August 2014 - 01:39 AM

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

 



Sponsor:

#2 BitMaster   Crossbones+   -  Reputation: 4276

Like
4Likes
Like

Posted 16 August 2014 - 01:43 AM

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


#3 FGFS   Members   -  Reputation: 214

Like
0Likes
Like

Posted 16 August 2014 - 02:30 AM

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



#4 Khatharr   Crossbones+   -  Reputation: 3031

Like
2Likes
Like

Posted 16 August 2014 - 03:24 AM

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.

#5 aregee   Members   -  Reputation: 1026

Like
1Likes
Like

Posted 16 August 2014 - 07:17 AM

 

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.


Edited by aregee, 16 August 2014 - 07:18 AM.


#6 AgPF6   Members   -  Reputation: 165

Like
2Likes
Like

Posted 16 August 2014 - 08:49 AM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS