int arrayLength = 0;
struct myStruct
{
int a;
int b;
string foo;
};
list<myStruct> myList;
myStruct *myArray; //Watch this space
int myArrayLength = 0;
void myFunc();
//Start doing stuff
int main()
{
myFunc(); //Run that function below to build "myArray"
for (int i = 0; i < myArrayLength; i++) cout << myArray[i].foo << endl; //Prints a few values then segfaults!!
}
void myFunc()
{
ifstream myFile('./somefile'); //We don't know how many lines are in the file yet
while (!myFile.eof())
{
string tmp;
getline(myFile, tmp);
myStruct x;
x.a = 10; //Just any value
x.b = 6; //Some other value
x.foo = tmp;
myList.push_back(x); //Add to stack
myArrayLength++; //Keep track of how many records we get
}
/*
So now we have a list that has data about our file
*/
myStruct bar[myArrayLength]; //Declare a new array of correct size
list<myStruct>::iterator i;
int z = 0;
for (i = myList.begin(); i != myList.end(); i++)
{
bar[z] = (*i); //Copy to the array
z++;
}
myArray = bar; //This is where it seems to go wrong!!!
/*
Print out all of "myArray" just to see that it really is there at this point in execution
*/
for (int i = 0; i < myArrayLength; i++) cout << myArray[i].foo << endl; //This does work
};
Basically I'm trying to optimize some code that uses a stl::list by moving the contents back out of the list into an array before starting the heavy work (arrays are faster, but I'm not sure by how much since I can't get it working).
I know the segfault must have something to do with the scope of that pointer but how can I work around that?
d11wtq wrote:Is this something to do with the fact I used a global pointer then assigned it to something that was only declared in the scope of the function?
You point myArray to an address of a variable that exists only in myFunc... Thus once you're outside that function, myArray points to something bogus..
(probably there are some compiler options to make variables live longer than their scope... i think microsoft used to provide such compilers :p but i'm not really convinced that it's a good thing to have)
d11wtq wrote:Is this something to do with the fact I used a global pointer then assigned it to something that was only declared in the scope of the function?
You point myArray to an address of a variable that exists only in myFunc... Thus once you're outside that function, myArray points to something bogus..
(probably there are some compiler options to make variables live longer than their scope... i think microsoft used to provide such compilers :p but i'm not really convinced that it's a good thing to have)
Thanks.
So what would be the normal way of creating an array at runtime, for the fact you don't know how big it will be in advance? The stl::list itself is a big help and my program runs nicely without an array but it's taking a long time to parse large scripts. From what I've read linked lists are slow because to access data they itreate across all previous items first. That's why I'm trying to convert it into an array before I start doing an heavy processing with it.
Basically the array goes into a loop lots and lots of times in order to generate some output.
Can I increase the size (efficiently and accurately) of an array "myStruct foo[1]" at runtime. I guess malloc() will work but I've never really understood how you know precisely how much space to allocated with malloc This weekend has been a steep learning curve for me
d11wtq wrote:Is this something to do with the fact I used a global pointer then assigned it to something that was only declared in the scope of the function?
You point myArray to an address of a variable that exists only in myFunc... Thus once you're outside that function, myArray points to something bogus..
(probably there are some compiler options to make variables live longer than their scope... i think microsoft used to provide such compilers :p but i'm not really convinced that it's a good thing to have)
d11wtq wrote:Is this something to do with the fact I used a global pointer then assigned it to something that was only declared in the scope of the function?
You point myArray to an address of a variable that exists only in myFunc... Thus once you're outside that function, myArray points to something bogus..
(probably there are some compiler options to make variables live longer than their scope... i think microsoft used to provide such compilers :p but i'm not really convinced that it's a good thing to have)
Thanks.
So what would be the normal way of creating an array at runtime, for the fact you don't know how big it will be in advance? The stl::list itself is a big help and my program runs nicely without an array but it's taking a long time to parse large scripts. From what I've read linked lists are slow because to access data they itreate across all previous items first. That's why I'm trying to convert it into an array before I start doing an heavy processing with it.
Basically the array goes into a loop lots and lots of times in order to generate some output.
Can I increase the size (efficiently and accurately) of an array "myStruct foo[1]" at runtime. I guess malloc() will work but I've never really understood how you know precisely how much space to allocated with malloc This weekend has been a steep learning curve for me
STL would likely be the way to go, because direct memory managment is how VERY hard to find bugs are created
The STL vector is the way to go for fast random access...it doesn't implement anything fancy, it's simply a container class for a linear array.
You are correct in your thinking about linked lists, they are slow for things like random access, because of the way they are implemented. Basically structures with previous and next pointers. Making them awesome for random insertion/deletion (which are constant in time) but slow as melting polar ice caps for indexing.
Anyways, use a vector if you want dynamic array allocation...
Thanks Hockey. Yeah I was using a STL vector and I gathered that the reason for them being slower was due to the double-linking/iteration. Since i'm always looping over the list in a linear fashion due to the nature of the way the app works maybe a basic array wouldn't be any faster in any case.
As for compiler options I believe GNU's g++ compiler/linker (linux of course) offers some scope options (I've heard this from 3 people now) but I haven't looked into it.... it has to be a BAD idea in any case.... I'd have lots of identifier changing to do
d11wtq wrote:Thanks Hockey. Yeah I was using a STL vector and I gathered that the reason for them being slower was due to the double-linking/iteration. Since i'm always looping over the list in a linear fashion due to the nature of the way the app works maybe a basic array wouldn't be any faster in any case.
As for compiler options I believe GNU's g++ compiler/linker (linux of course) offers some scope options (I've heard this from 3 people now) but I haven't looked into it.... it has to be a BAD idea in any case.... I'd have lots of identifier changing to do
I suppose technically it's possible, but seems very illogical and almost completely useless
All my development is strictly Windows, so perhaps I'm keeping a biased view here...
There may very well be reasons on *nix machines to keep a local variable "alive" outside of it's scope, in which case, why not make it explicit using static?
Eg: with the visual c++ 6 compiler the following code is perfectly acceptable, notice that i is still defined in the second loop.
With visual c++ 8 you explicitely have to disable a check for conformance to get the code compiled.