Solved

returning an array from a function

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

How many times have you wanted to quickly do the same thing to a list but found yourself typing it again and again? I first figured out a small time saver with the up arrow to recall the last command but that can only get you so far if you have a bi…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
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 and use conditional statements in the C programming language.

747 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now