View previous topic :: View next topic |
Author |
Message |
aaaaamartin
Joined: 17 Apr 2005 Posts: 39 Location: Germany Stuttgart
|
return pointer to structure |
Posted: Mon Jan 08, 2007 6:29 pm |
|
|
Hello,
can anyone please give a complete example on how to correctly return
a pointer to struct.
I want to do something like this:
Code: | typedef struct {
int x;
int y;
} a_struct;
pointer * test (void) {
a_struct *pointer;
pointer -> x += 1;
pointer -> y -= 1;
return pointer
}
void main (void) {
recieved_pointer = test ();
printf ("x: %u // y: %u", recieved_pointer->x, recieved_pointer->y);
while (1);
}
|
Thanks Martin |
|
|
kamputty Guest
|
Re: return pointer to structure |
Posted: Mon Jan 08, 2007 8:46 pm |
|
|
Martin,
Some pointers (pun intended!)
In the following method
Code: |
pointer * test (void) {
a_struct *pointer;
pointer -> x += 1;
pointer -> y -= 1;
return pointer
}
|
Look what you are doing. You are allocating/defining pointer in line #3
Code: |
#1:pointer * test (void) {
#2:
#3: a_struct *pointer;
#4:
#5: pointer -> x += 1;
#6: pointer -> y -= 1;
#7:
#8: return pointer
}
|
thats cool, and you're setting it etc. It's scope is WITHIN the method. So after you return the pointer, the scope changes, and the data can be overwritten. You have defined a LOCAL variable and then returning the address to it. I'm sure CCS will reuse that memory for other variables.
So you can either define the variable out of this scope, as in a global variable, or malloc the pointer
Code: |
a_struct *pointer=(a_struct *)malloc(sizeof(a_struct));
|
Make sense? Think about it, you're allocating the memory for the pointer with the method, and once the method leaves, even though you return the pointer, the memory can be use for something else. With the malloc way, you allocate the memory, but you are also responsible in free()ing the memory too.
food for thought...hope it helps...
~Kam (^8* |
|
|
aaaaamartin
Joined: 17 Apr 2005 Posts: 39 Location: Germany Stuttgart
|
|
Posted: Tue Jan 09, 2007 9:59 am |
|
|
I'm thinking,
however I can't even use malloc since
Code: | #include <stdlibm.h>
int * iptr;
iptr=malloc(10); |
gives me an error "Expecting a ("
right before right before the '='
I have PCH 3.235
Martin |
|
|
kamputty Guest
|
|
Posted: Tue Jan 09, 2007 8:23 pm |
|
|
Martin,
Can you compile this:
Code: |
#include <stdlibm.h>
void main()
{
int *iPtr;
iPtr=malloc(10);
iPtr=(int*)malloc(10);
iPtr=(int*)malloc(10*sizeof(int));
}
|
I've got v3.234 and it compiles just fine...
~Kam (^8*[/code] |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Wed Jan 10, 2007 2:56 am |
|
|
I wouldn't recommend using malloc because of the possible memory fragmentation that can occur, leading to instable applications and very hard to find errors. General rule: don't use malloc() in embedded applications.
If you want to return a structure from a function then use either a pointer to a global allocated variable: Code: | typedef struct {
int x;
int y;
} a_struct;
a_struct my_struct; // allocate global variable
a_struct* test(void)
{
a_struct *pointer;
pointer = &my_struct; // initialize pointer to the _global_ allocated memory
pointer->x += 1;
pointer->y -= 1;
return pointer;
}
void main(void)
{
a_struct *received_pointer;
received_pointer = test();
printf ("x: %u // y: %u", received_pointer->x, received_pointer->y);
while (1);
}
|
Or instead of returning a pointer to the struct, you are in C allowed to return the struct itself. This method has the advantage that you don't have to use a global variable but can allocate the struct on the local heap.
Code: | typedef struct {
int x;
int y;
} a_struct;
a_struct test(void) // Note that we are not returning a pointer anymore
{
a_struct my_struct; // allocate a _local_ variable
my_struct.x += 1;
my_struct.y -= 1;
return my_struct;
}
void main(void)
{
a_struct received_struct;
received_struct = test();
printf ("x: %u // y: %u", received_struct.x, received_struct.y);
while (1);
} |
|
|
|
|