generating arrays in a function

hey, i’m trying to pass an array to a function, which will set its length and fill its contents (with info coming from a text file).

here’s a little demo of what i’m doing:

  
  
  
string * myString;  
	  
generateStringArray(myString);  
  
void testApp::generateStringArray(string * stringArray){  
  
	stringArray = new string[2];   
	  
	stringArray[0] = "hello world";  
	stringArray[1] = "gruss";  
	  
}  
  
  

when i print the contents of the array from within the function, it works, but when i print them in the main loop, i get all sorts of gobbledygook from my computer’s memory. any ideas about what’s wrong with my syntax? i know i should be moving to vectors in order to solve this problem, but i stubbornly want to get this working first.

thanks!

jeremy

can you post the whole code ? testApp.h and testApp.cpp –

thanks!
-z

ah, no worries see the issue - I think you are making assumptions about pointers passed into functions. you can always just print out a pointer to see where in memory it points to…

check it:

  
  
#include <iostream>  
#include <string>  
  
using namespace std;  
  
void generateStringArray(string * stringArray);  
  
int main(){  
  
	string * myString;  
	printf("before call %i \n", myString);  
	generateStringArray(myString);  
	printf("after call %i \n", myString);  
  
	return 0;  
}  
  
  
void generateStringArray(string * stringArray){  
  
   printf("inside call, pre allocate = %i\n", stringArray);  
   stringArray = new string[2];  
   printf("inside call, post allocate = %i\n", stringArray);  
   stringArray[0] = "hello world";  
   stringArray[1] = "gruss";  
     
}   
  

produces:

  
  
before call -858993460  
inside call, pre allocate = -858993460  
inside call, post allocate = 1432016  
after call -858993460   
  

so the “after call” is wrong – we don’t get that new value back. this is because of pass by copy or pass by reference issues, it’s made trickier w/ a pointer but it’s the same problem.

alternatively:

  
  
  
string *  generateStringArray();  
  
int main(){  
  
	string * myString;  
	printf("before call %i \n", myString);  
	myString = generateStringArray();  
	printf("after call %i \n", myString);  
  
	return 0;  
}  
  
  
string * generateStringArray(){  
  
   string * stringArray = new string[2];  
   printf("inside call, post allocate = %i\n", stringArray);  
   stringArray[0] = "hello world";  
   stringArray[1] = "gruss";  
     
}   
  
  

produces:

  
  
before call -858993460  
inside call, post allocate = 1432016  
after call 1432028  
  

we get what we’d expect…

hope that helps!
zach

ok, i’ll have to look into this pass by copy vs. pass by reference thing.
unfortunately, when i tried to print the contents of the array generated by generateStringArray(), my program crashed. shouldn’t those last two values be the same?

cout << myString[0] << endl;

thanks!

jeremy

oops. i was missing the return statement. so nix that last question.