CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

return pointer to structure

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
aaaaamartin



Joined: 17 Apr 2005
Posts: 39
Location: Germany Stuttgart

View user's profile Send private message

return pointer to structure
PostPosted: Mon Jan 08, 2007 6:29 pm     Reply with quote

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
PostPosted: Mon Jan 08, 2007 8:46 pm     Reply with quote

Martin,

Some pointers (pun intended!) Very Happy

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

View user's profile Send private message

PostPosted: Tue Jan 09, 2007 9:59 am     Reply with quote

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







PostPosted: Tue Jan 09, 2007 8:23 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Jan 10, 2007 2:56 am     Reply with quote

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);   
}
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group