Solved

returning an array from a function

Posted on 2013-11-20
4
365 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Over the last ten+ years I have seen Linux configuration tools come and go. In the early days there was the tried-and-true, all-powerful linuxconf that many thought would remain the one and only Linux configuration tool until the end of times. Well,…
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…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

809 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