Solved

returning an array from a function

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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

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…
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
The goal of this video is to provide viewers with basic examples to understand and use structures 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