ale
ale

Reputation: 11830

Can this code be improved by returning a pointer to a struct?

I have the following code that gets the MAC address of an interface:

static struct ifreq get_mac( int socket_desc, char *if_name ) {                                                                                                                                                                         
      struct ifreq if_mac;                                                                                                                                                                                                                
      memset( &if_mac, 0, sizeof( struct ifreq ) );                                                                                                                                                                                       
      strncpy( if_mac.ifr_name, if_name, IFNAMSIZ - 1 );                                                                                                                                                                                  
      ...                                                                                                                                                                                                                                  
      return if_mac;                                                                                                                                                                                                                      
}

My C experience is limited to what I did in college. I roughly get pointers but I understand that returning large structs by value is a bad idea because you can run out of stack space (stackoverflow!). How can I change the above code to return a pointer to if_mac instead ? It's just a bit confusing because there's a struct and an 'address of' operator :S.

Upvotes: 2

Views: 184

Answers (3)

ecatmur
ecatmur

Reputation: 157424

You'd do better to take the struct by pointer as an out-parameter, thus deferring the decision and responsibility for dynamic memory management (if any) to the caller:

static void get_mac( int socket_desc, char *if_name, struct ifreq *if_mac ) { 
    ...
}

However, given that most compilers will optimise large struct return to an out-parameter call anyway, there's not much point. Do you know how large the struct if_mac is?

Upvotes: 2

Benny
Benny

Reputation: 4321

Give a pointer to the expected structure in parameter:

static int ifreq get_mac(int socket_desc, char *if_name, struct ifreq **if_mac)
{
      if (NULL == (*ifmac = malloc(sizeof (struct ifreq))) {
          return -1;                                        
      }                                                                                                                                                                                                                                                                                                                            
      memset(*if_mac, 0, sizeof(struct ifreq));                                                                                                                                                                                       
      strncpy((*if_mac)->ifr_name, if_name, IFNAMSIZ - 1);                                                                                                                                                                                  
      ...                                                                                                                                                                                                                                  
      return 0;                                                                                                                                                                                                                      
}

If the caller has already allocated the space for the structure, the function prototype becomes:

static int ifreq get_mac(int socket_desc, char *if_name, struct ifreq *if_mac)

Upvotes: 3

Kraken
Kraken

Reputation: 24233

Dynamically allocate memory (malloc) to if_mac and return the pointer Or Pass the pointer to the function itself, creating one before the function call and apply changes.

Dont return anything in second case. That may help.

Upvotes: 0

Related Questions