?
Solved

returning an array from a function

Posted on 2013-11-20
4
Medium Priority
?
369 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

WordPress Tutorial 3: Plugins, Themes, and Widgets

The three most common changes you will make to your website involve the look (themes), the functionality (plugins), and modular elements (widgets).

In this article we will briefly define each again, and give you directions on how to install them.

Question has a verified solution.

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

Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Suggested Courses
Course of the Month10 days, 14 hours left to enroll

770 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