I have created a class that contains a simple character array, and I'm trying to create an array of that class, initializing all the member character arrays upon declaration. The problem is, I need to be able to destroy the array and re-create it later, since my array sizes are causing overflow errors if I simply create a new array for each use. To be specific, my class is called city, and contains among other things a character array that contains the city's name. I already have the constructor that will initialize the cityname that's passed to it, that part is working fine.
My program reads in multiple text files, one at a time, and parses them to extract info from them for insertion into a SQL database. Each file contains records from 1 state, but parsing multiple files means that each file might have records for a new state. My array of citynames should at any time contain all the cities for the current state, and should be destroyed at the end of the file to free up the memory. Upon opening the next file, I need to create a new array with the city names for that state. Essentially, I need the equivalent of the following statement (which doesn't work for obvious reasons, but I wish it did)
city* citylist=new city[]={"city1","city2","city3"...};
The following code successfully creates a citylist:
city citylist[]={"city1","city2","city3"...};
But I can't destroy an array made like that.
Any ideas?
-Moedaddy
PaulWendt
March 25th, 2003, 04:13 PM
Can you use std::vector<> or are you limited to C?
If you can use C++, then I strongly recommend you use
std::vector<> instead of trying to worry about all of this memory
management yourself. std::vector<> guarantees that its
memory is stored contiguously ... just like a C-style array so even
if you DO have to maintain compatability with legacy C functions,
you won't be up a creek.
--Paul
Gabriel Fleseriu
March 25th, 2003, 04:14 PM
Try preferring the use of the standard library to hand crafted solution. Your problem seems to be the perfect candidate for
std::vector<std::string> ...
Paul McKenzie
March 25th, 2003, 04:14 PM
Use std::vector and std::string. There is no need for the headaches of C-style char arrays, pointers and operator new.
You can copy vectors and strings with no need for new or delete. The vector can dynamically grow, etc. and clean up after themselves when they go out of scope, automatically.
I suggest you get a good C++ book that discusses these classes, such as "The C++ Standard Library" by Nicolai Josuttis.
Regards,
Paul McKenzie
JamesSchumacher
March 25th, 2003, 05:41 PM
good ideas as far as time constraints and development resources go.
But if you want to learn how these things work, it is a good idea to ask questions like this. Pick up a good book on C++ and it will teach you arrays and 'C' style character strings (which are just null terminated arrays.)
moedaddy
March 26th, 2003, 03:40 PM
That's all good and well, but if I'm reading your article correctly Gabriel, the only way for me to simulate directly initializing the vector the same way I could with a C-style array is to use an intermediary array. This defeats the whole purpose, as I will have to create a huge intermediary array which I can't destroy later. The only alternative for me is to push_back the elements into the vector. That really isn't possible, my arrays for each state contain every city in that state. For states like Idaho, that will only be a few hundred cities, but that is still going to increase the length of my program significantly. My example included 4 dummy cities, but in reality I'm talking about thousands of cities here. So, unless I'm mis-understanding something, I'm stuck with the same brick wall.
-Moedaddy
PaulWendt
March 26th, 2003, 03:46 PM
Originally posted by moedaddy
That's all good and well, but if I'm reading your article correctly Gabriel, the only way for me to simulate directly initializing the vector the same way I could with a C-style array is to use an intermediary array. This defeats the whole purpose, as I will have to create a huge intermediary array which I can't destroy later. The only alternative for me is to push_back the elements into the vector. That really isn't possible, my arrays for each state contain every city in that state. For states like Idaho, that will only be a few hundred cities, but that is still going to increase the length of my program significantly. My example included 4 dummy cities, but in reality I'm talking about thousands of cities here. So, unless I'm mis-understanding something, I'm stuck with the same brick wall.
-Moedaddy
You talk about creating and destroying arrays of strings ... and
then you talk about having them all known at compile-time.
So, which is it? If you know everything at compile time, there's no
need for you to create or destroy anything. If you don't know
these strings until run-time, then std::vector is a perfectly fine
solution.
Why don't you tell us exactly what you're trying to do? I get the
feeling that you're mixing up two unrelated concepts in an
attempt to do what you want. Why not tell us what you want and
we can help?
It sounds almost like you want to load a list of cities, given a
state. I'd say you would probably want to put these cities in a
file ... maybe each state would have its own file and you would
put each city on its own line [that'd make it really easy to read in].
Then, at run-time, once you know the proper state, you get every
line [city] in the file and put each one into your vector.
If I don't understand what you're trying to accomplish, please
enlighten us :)
--Paul
moedaddy
March 26th, 2003, 04:03 PM
The problem is, I have the initializer lists for the 50 arrays coded already, so that I can use them as such:
city WAcitylist[]={"city1","city2","city3"...};
city OR citylist[]={"city1","city2","city3"...};
...
I'm trying to avoid having 50 files bundled in with my executable. Granted, that solution will work, but this exe will be processing so much data that using that solution (which was what I was using before I hard coded the info into arrays) caused the exe to take hours to run. I know all the city names at compile time, I've got them hard-coded in, just waiting to be put into an array or a vector. All I'm looking for is a way to dynamically declare an array that is initialized upon declaration. If I have to declare the array first and then initialize it 1 value at a time, it will take too long.
You're exactly right - I do want to load a list of cities given a state. I have come up with two solutions so far: Storing the cities in files (too slow) and storing the cities in arrays (takes too much memory). I don't know if it's possible to initialize a dynamic array upon declaration or not, that's all I'm trying to find out.
-Moedaddy
Paul McKenzie
March 26th, 2003, 04:14 PM
Originally posted by moedaddy
That's all good and well, but if I'm reading your article correctly Gabriel, the only way for me to simulate directly initializing the vector the same way I could with a C-style array is to use an intermediary array. This defeats the whole purpose, as I will have to create a huge intermediary array which I can't destroy later.
The only alternative for me is to push_back the elements into the vector. That really isn't possible, my arrays for each state contain every city in that state. Use back_inserter
int main()
{
std::vector<std::string> S;
char *city[] = {"abc", "123", "def", "fiodfjio" };
std::copy( city, city + 4, std::back_inserter(S) );
for (int i = 0; i < 4; i++ )
std::cout << S[i] << " ";
}
One extra line of code is all you need to initialize the vector. Also, are these cities stored in a database or in a file? If it isn't a file / database, your program will be loaded with static strings, making your final executable much larger than necessary. If they are stored in a database / file, why are you attempting to initialize the cities at compile time?
Regards,
Paul McKenzie
Paul McKenzie
March 26th, 2003, 04:21 PM
Originally posted by moedaddy
The problem is, I have the initializer lists for the 50 arrays coded already, so that I can use them as such:
city WAcitylist[]={"city1","city2","city3"...};
city OR citylist[]={"city1","city2","city3"...};
...
I'm trying to avoid having 50 files bundled in with my executable. Granted, that solution will work, but this exe will be processing so much data that using that solution (which was what I was using before I hard coded the info into arrays) caused the exe to take hours to run. Why not read all the cities at the beginning? Why do you need to access these files again after you've initially read the cities in?
Regards,
Paul McKenzie
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.