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.
6 replies to this topic
#1
Posted 26 July 2011 - 06:46 PM
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
Posted 27 July 2011 - 03:08 AM
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.
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.
#3
Posted 27 July 2011 - 03:28 PM
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
Posted 27 July 2011 - 04:05 PM
A std::vector<std::string>, or a pointer to that would be how I'd approach it.
#5
Posted 31 July 2011 - 10:32 AM
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
Posted 05 August 2011 - 01:09 PM
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
Posted 06 August 2011 - 08:10 AM
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.
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


Sign In
Create Account


Back to top









