Pointers, Functions and XCode messages
-
Hi all; I am relearning C after about 20 years. (I had a done a course after which never wrote a line of C code to earn beer money). I would appreciate any sort of help with my current quandary - a function returning a pointer and returning structure variables.
#include typedef struct
{
float Centre[3];
float Radius;
} Sphere;Sphere *MakeSphere(float *centre, float *radius);
void PrintSphereDetails(Sphere *s);int main(int argc, const char * argv[])
{float c1\[3\] = {10.0,20.0,30.0}; float r1 = 10.0; Sphere \*s1 = MakeSphere(&c1\[0\], &r1); PrintSphereDetails(s1); return 0;
}
Sphere *MakeSphere(float *centre, float *radius)
{Sphere s; s.Centre\[0\] = \*centre++; s.Centre\[1\] = \*centre++; s.Centre\[2\] = \*centre; s.Radius = \*radius; return &s;
}
void PrintSphereDetails(Sphere *s)
{printf("------------------------------------------------\\n"); printf("Centre: (%6.2f, %6.2f, %6.2f)\\n", s->Centre\[0\], s->Centre\[1\], s->Centre\[2\] ); printf("Radius: %6.2f\\n\\n", s->Radius);
}
I am working in XCode and the code works, but I get a warning "Address of stack memory associated with local variable 's' returned." for the code line return &s in function MakeSphere. That is exactly what, I think, I need so what is XCode telling me? Another problem, which XCode solved for me has to do with the way to get the values of the structure variables in function PrintSphereDetails. Initially I had them as s.Centre[0] but XCode flagged these and recommended doing s->Centre[0]. What is the difference? Regards Eric
The author's comes from a long line of evolved fish.
-
Hi all; I am relearning C after about 20 years. (I had a done a course after which never wrote a line of C code to earn beer money). I would appreciate any sort of help with my current quandary - a function returning a pointer and returning structure variables.
#include typedef struct
{
float Centre[3];
float Radius;
} Sphere;Sphere *MakeSphere(float *centre, float *radius);
void PrintSphereDetails(Sphere *s);int main(int argc, const char * argv[])
{float c1\[3\] = {10.0,20.0,30.0}; float r1 = 10.0; Sphere \*s1 = MakeSphere(&c1\[0\], &r1); PrintSphereDetails(s1); return 0;
}
Sphere *MakeSphere(float *centre, float *radius)
{Sphere s; s.Centre\[0\] = \*centre++; s.Centre\[1\] = \*centre++; s.Centre\[2\] = \*centre; s.Radius = \*radius; return &s;
}
void PrintSphereDetails(Sphere *s)
{printf("------------------------------------------------\\n"); printf("Centre: (%6.2f, %6.2f, %6.2f)\\n", s->Centre\[0\], s->Centre\[1\], s->Centre\[2\] ); printf("Radius: %6.2f\\n\\n", s->Radius);
}
I am working in XCode and the code works, but I get a warning "Address of stack memory associated with local variable 's' returned." for the code line return &s in function MakeSphere. That is exactly what, I think, I need so what is XCode telling me? Another problem, which XCode solved for me has to do with the way to get the values of the structure variables in function PrintSphereDetails. Initially I had them as s.Centre[0] but XCode flagged these and recommended doing s->Centre[0]. What is the difference? Regards Eric
The author's comes from a long line of evolved fish.
ericgahn wrote:
Sphere s;
That line of code is creating the instance on the stack. The stack is only valid within the method and there is no assurance that it will continue to be valid. You need to allocate it on the heap - look at the malloc method and associated other methods. And remember to delete it when you are done with it.
-
Hi all; I am relearning C after about 20 years. (I had a done a course after which never wrote a line of C code to earn beer money). I would appreciate any sort of help with my current quandary - a function returning a pointer and returning structure variables.
#include typedef struct
{
float Centre[3];
float Radius;
} Sphere;Sphere *MakeSphere(float *centre, float *radius);
void PrintSphereDetails(Sphere *s);int main(int argc, const char * argv[])
{float c1\[3\] = {10.0,20.0,30.0}; float r1 = 10.0; Sphere \*s1 = MakeSphere(&c1\[0\], &r1); PrintSphereDetails(s1); return 0;
}
Sphere *MakeSphere(float *centre, float *radius)
{Sphere s; s.Centre\[0\] = \*centre++; s.Centre\[1\] = \*centre++; s.Centre\[2\] = \*centre; s.Radius = \*radius; return &s;
}
void PrintSphereDetails(Sphere *s)
{printf("------------------------------------------------\\n"); printf("Centre: (%6.2f, %6.2f, %6.2f)\\n", s->Centre\[0\], s->Centre\[1\], s->Centre\[2\] ); printf("Radius: %6.2f\\n\\n", s->Radius);
}
I am working in XCode and the code works, but I get a warning "Address of stack memory associated with local variable 's' returned." for the code line return &s in function MakeSphere. That is exactly what, I think, I need so what is XCode telling me? Another problem, which XCode solved for me has to do with the way to get the values of the structure variables in function PrintSphereDetails. Initially I had them as s.Centre[0] but XCode flagged these and recommended doing s->Centre[0]. What is the difference? Regards Eric
The author's comes from a long line of evolved fish.
-
ericgahn wrote:
Sphere s;
That line of code is creating the instance on the stack. The stack is only valid within the method and there is no assurance that it will continue to be valid. You need to allocate it on the heap - look at the malloc method and associated other methods. And remember to delete it when you are done with it.
-
As another option, you could change your function this way
void MakeSphere(float *centre, float *radius, Sphere *);
and initialize the struct allocated by the caller.
Veni, vidi, vici.
-
CPallini wrote:
void MakeSphere(float *centre, float *radius, Sphere *);
So if I understand correctly a Sphere 'instance' would be created in Main() and then a pointer to this instance passed to the function?