Solved

returning an array from a function

Posted on 2013-11-20
4
367 Views
Last Modified: 2013-11-20
I want to provide an API that returns an array
of structre items.  As one of the structure member is a
pointer, I assume the caller of the function would allocate
the memory for it so I don't have to worry about allocating the memory.
Even if I do, then I will not be able to free it.  So, i'm thinking the
way I am doing is the usual way.

Below is my get_values( ) API and a test_get_values() showing the calling function.

The output seems correct, but I wanted to make sure I am doing it in the correct way.

Also, if I want the API to be using pointers to the structure rather than structures
like get_values(my_struct *values[10], int size) then how would the calling
function would change.
typedef struct
{
    char *name;
    char *address;
    int id;
} my_struct_t;

void get_values(my_struct_t values[], int size)
{
    int i = 0;
    for(;i < size; i++ )
    {
      strcpy(values[i]->name, "somename");
      strcpy(values[i]->address,"someaddress");
      values[i]->id = i * 5000;
    }
}


void test_get_values()
{
  int i = 0, j = 0;

  my_struct_t values[10];
  
  for(;i < 10; i++)
  {
      values[i].name = malloc(20);
      values[i].address = malloc(20);
      values[i].id = 0;
  }
  get_values(values, 10);

  for(; j < ret_size; j++ )
  {
        printf("TEST RESULT: %s - %s - %d\n", values[j].name, values[j].address, values[j].id);
  }

  for(i = 0; i < 10; i++ )
  {
      free(values[i].name);
      free(values[i].address);

  }

}

int main()
{
    test_get_values();
}

Open in new window

0
Comment
Question by:ambuli
  • 2
  • 2
4 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 39663758
If you know that "somename" and "someaddress" will always be less than 20 characters, your way would work, otherwise, I'd either let get_values allocate values[ i ].name and values[ i ].address, or pass their lengths to get_values

But if you do know that "somename" and "someaddress" will always be less than 20 characters, then it may be better to declare them as char  name[20] and char  address[20]
0
 

Author Comment

by:ambuli
ID: 39663776
Thank you ozo.  In your opinion which would be better passing the length or asking the caller to free the memory once done?  Also, can you please answer my second question.
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 39663805
If you know that the lengths of name and address will always be less than 20, I'd declare them as
      char name[20];
      char address[20];
and then they will be freed whenever the struct is freed,
otherwise, I'd let  get_values allocate the space, and ask the caller to free the memory once done

BTW, values[ i ]-> should be values[ i ].
If you want to use -> you might do
#include <stdio.h>
#include <string.h>
#include <stdlib.h> 
typedef struct
{
  //    char *name;
  //    char *address;
      char name[20];
      char address[20];
    int id;
} my_struct_t;

void get_values(my_struct_t *values[], int size)
{
    int i = 0;
    for(;i < size; i++ )
    {
      strncpy(values[i]->name, "somename",sizeof(values[i]->name));
      strncpy(values[i]->address,"someaddress",sizeof(values[i]->address);
      values[i]->id = i * 5000;
    }
}

#define ret_size 10
void test_get_values()
{
  int i = 0, j = 0;

  my_struct_t *values[ret_size];
  
  for(;i < ret_size; i++)
  {
      values[i] = malloc(sizeof(my_struct_t));
    //      values[i].name = malloc(20);
    //      values[i].address = malloc(20);
      values[i]->id = 0;
  }

  get_values(values, ret_size);

  for(; j < ret_size; j++ )
  {
        printf("TEST RESULT: %s - %s - %d\n", values[j]->name, values[j]->address, values[j]->id);
  }

  for(i = 0; i < ret_size; i++ )
  {
    free(values[i]);
    //      free(values[i].name);
    //      free(values[i].address);

  }

}

int main()
{
    test_get_values();
}
   

Open in new window

0
 

Author Comment

by:ambuli
ID: 39663941
Thank you!!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question