1

I have the following scenario in the C (!!! not C++ !!!) code:

#include <stdlib.h>

struct point
{
    double *x, *y;
};

void point_Construct(struct point *p)
{
    p->x = (double*)malloc(sizeof(double));
    p->y = (double*)malloc(sizeof(double));
}

struct point3D
{
    double *ux, *uy, *uz;
};

void point3D_Construct(struct point3D *uP, double *x, double *y, double *z)
{
    uP->ux = x; //assigning pointers to pointers
    uP->uy = y;
    uP->uz = z;
}

void point3D_Compute(struct point3D *uP)
{
    double cx, cy, cz;

    //the following 3 lines do not work...
    //i.e.,  *uP->ux contains the right value but after assigning this value
    //to the cx variable, the cx holds some unreasonable value...
    cx = *uP->ux; //assigning values to which the pointers points to local variables
    cy = *uP->uy;
    cz = *uP->uz;

    cz = cx + cy; //using values

    //... other code...
}

static struct point  instPoint;  //create structures
static struct point3D instPoint3D;

static double mx, my, mz; //declare global variables

int main(void)
{

    mx = 1.0; //assigning values to static variables
    my = .0;
    mz = 24.5;

    point_Construct(&instPoint); //alloc memory for struct point

    //assigning values to the place in memory where pointers of the point struct point
    *instPoint.x = mx;
    *instPoint.y = my;

    //inicialize pointers of the point3D struct to memory addresses 
    //pointed by the pointers of the point struct and
    //to the address of the mz static global variable
    point3D_Construct(&instPoint3D, instPoint.x, instPoint.y, &mz);


    point3D_Compute(&instPoint3D); //using all the values

    //...other code...
}

The code compiles without any issue. The problem is within the point3D_Compute function. I can see in debugger that values to which the pointers point are correct. After assigning this values to the local double variables, these variables contain some garbage values instead of the correct ones...

I've already tried the following methods but no one of them is working:

cx = *up->ux;

or

cx = *(up->ux);

or

cx = up->ux[0];

What am I missing?

Thank you in advance for any help...

The code compiles without any issue. The problem is within the point3D_Compute function. I can see in debugger that values to which the pointers point are correct. After assigning this values to the local double variables, these variables contain some garbage values instead of the correct ones...

I've already tried the following methods but no one of them is working:

cx = *up->ux;

or

cx = *(up->ux);

or

cx = up->ux[0];

What am I missing?

Thank you in advance for any help...

Jirka
  • 91
  • 1
  • 6
  • Why do you store pointers to double in `struct point` ? This is overly complicated and less efficient and more error prone than storing directly `double`s in the `point struct`. – Jabberwocky Mar 26 '14 at 16:02
  • Correct your code, it doesn't compile. – Jabberwocky Mar 26 '14 at 16:06
  • [Please don't cast the return value of `malloc()` in C](http://stackoverflow.com/a/605858/28169). – unwind Mar 26 '14 at 16:09
  • In fact it is more complex code and I've dropped all unnecessary parts... – Jirka Mar 26 '14 at 16:09
  • @Jirka: even if your pointer stuff is weird, after some corrections, your code compiles... and works as expected. – Jabberwocky Mar 26 '14 at 16:13
  • Maybe I have to appoint that it has to be compilled with C90 – Jirka Mar 26 '14 at 16:22
  • I've corrected the code - I missed the semicolons after the struct declarations... – Jirka Mar 26 '14 at 16:23
  • Try using printf rather than debugger to look at the values –  Mar 26 '14 at 16:25
  • NOw I have tried to compile it in Visual Studio - and it works as expected... BUt when I compile and run the code in Keil for ARM then the problem persists... – Jirka Mar 26 '14 at 16:27
  • @unwind If I try to compille without the casts I get the following error: try.c(8): error: #513: a value of type "int" cannot be assigned to an entity of type "double *" – Jirka Mar 26 '14 at 16:30
  • @Jirka Then you're headers are broken, it seems you're missing a prototype for `malloc()`. It does not return `int`, which your compiler seems to be thinking. – unwind Mar 26 '14 at 16:33
  • I've tried calloc with the same result... – Jirka Mar 26 '14 at 17:07
  • OK, to avoid using (m, c)alloc functions I've tried to create static arrays and assing their addresses to the pointers. Again, values that pointers point to are OK but it is not possible to assign them to the local variables within the function :-( Result is again the same... I've added the volatile specifier to the declarations of the variables but it changed nothing... – Jirka Mar 26 '14 at 17:20

2 Answers2

0

try *(up)->ux; which is the value that up pointer holds and selecting ux field out of that value

Pandrei
  • 4,843
  • 3
  • 27
  • 44
  • Thank you, Pandrei. Tried your suggestion with and the result is without any change... The value in ux is 2.121995791459e-314 – Jirka Mar 26 '14 at 17:09
0

Even I do not understand why it is not working by the standard way, I've found the working solution:

void point3D_Compute(struct point3D *uP)
{
    double cx, cy, cz;
    double *pCx, *pCy;

    pCx = &cx;
    pCy = &cy;    

    pCx = uP->ux; 
    pCy = uP->uy;

    cz = cx + cy; //it is OK now...

    //... other code...
}
Jirka
  • 91
  • 1
  • 6
  • If someone is able to explain this (strange?) behaviour maybe it could help in simillar cases... However thank to all for the effort... – Jirka Mar 26 '14 at 18:06
  • Update: I've found that if I declare cx, cy, cz globaly, everything is working well even in original version. Maybe the problem resides in some far jumps in complilled code because it is called within an interrupt routine, perhaps there is some bug in optimization of the Keil ARM compiller... I have changed it to the level 0 and everything is working even with the variables declared locally within the Compute function. So you can experiment with this settings if you have same problem. Good luck :-) – Jirka Mar 27 '14 at 07:55
  • this looks like a compiler bug. – Pandrei Mar 27 '14 at 08:20