Jump to content

Returning an array of strings

- - - - -

  • Please log in to reply
6 replies to this topic

#1
bbqroast

bbqroast

    Codecall Addict

  • Members
  • PipPipPipPipPipPipPip
  • 548 posts
  • Location:/etc/passwd
I am trying to return an array of strings from a function I am writing, basically it takes a string 'name' and then opens that file, it then reads the file outputs it and returns a array containing each line.

It does this fine except for the returning part, I understand I have to return a pointer? But i'm not really sure.


Thanks in advance, bbqroast.

This thread sat unsent for 3-5 hours.
Please, write clearly with proper structure. Double spacing makes the text feel un-jointed, Capitalizing Every Word Means People Stop Before Every Word Sub-Consciously Which Is A Pain In The Backside, and use code tags! (The right most styling box).

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Is this in C or C++?

For C, you'll have to malloc an array of appropriate size to a pointer and return the pointer. In C++, you would do better to return a std::vector of std::string.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
bbqroast

bbqroast

    Codecall Addict

  • Members
  • PipPipPipPipPipPipPip
  • 548 posts
  • Location:/etc/passwd
C++
Please, write clearly with proper structure. Double spacing makes the text feel un-jointed, Capitalizing Every Word Means People Stop Before Every Word Sub-Consciously Which Is A Pain In The Backside, and use code tags! (The right most styling box).

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
A std::vector<std::string>, or a pointer to that would be how I'd approach it.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,719 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
Make sure you don't return a pointer to a local std::vector. That'd deallocate when the function exits and then the caller would get stuck with a pointer to a block of memory that contains who knows what.

#include <vector>
#include <string>

// This is okay
std::vector<std::string> * foo()
{
    std::vector<std::string> *ptr = new std::vector<std::string>();

    // do stuff

    return ptr;
}

// This is also okay - note it doesn't return a pointer
std::vector<std::string> foo()
{
    std::vector<std::string> obj;    // not allocated on the heap

    // Copy constructor called, life is good
    return obj;
}

// This is also okay but it requires the caller to pass in their own vector
void foo(std::vector<std::string>& obj)
{
    obj.clear();
    // or whatever you need to do
}

// THIS IS NOT OKAY
// Never return a pointer to local data.
std::vector<std::string> * foo()
{
    std::vector<std::string> local_obj;

    // Copy constructor never called
    return &local_obj;
}

sudo rm -rf /

#6
bbqroast

bbqroast

    Codecall Addict

  • Members
  • PipPipPipPipPipPipPip
  • 548 posts
  • Location:/etc/passwd
Ahh I get it now :)
Please, write clearly with proper structure. Double spacing makes the text feel un-jointed, Capitalizing Every Word Means People Stop Before Every Word Sub-Consciously Which Is A Pain In The Backside, and use code tags! (The right most styling box).

#7
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,719 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
Just an FYI - never return a pointer to any function-local variable, as it'll get deallocated as soon as the function exits and you'll be left with a pointer to who-knows-what.

The only exception is if you're returning a pointer to a static variable declared inside a function, in which case it's permanent, like a global variable, but accessible only inside the function, like a global variable. Don't use these - C was designed far before multithreading, and those kind of static variables are inherently unsafe.

char *foo(int a)
{
    char str[32];
    sprintf(str, "%d", a);
    
    /* DO NOT DO THIS - Returning pointer to local variable */
    return str;
}


char *foo(int a)
{
    char *str = malloc(32 * sizeof(char));
    
    sprintf(str, "%d", a);
    
    /* OK - Allocated on the heap */
    return str;
} 


char *foo(int a)
{
    static char str[32];
    sprintf(str, "%d", a);
    
    /* OK - variable str is statically allocated and will
    never die for the duration of the program. */
    return str;
}

sudo rm -rf /




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users