Array Creation at run-time in c++/Linux

Apr 4, 2010 at 4:19am
Dear Friends,

I have a text file from which i read a number of names with their lengths at the run-time. Now i want to created a char array having the length and name as already read from the text file at the run-time. There is no compilation involved. Every thing is happening at the run-time. I tried using STL like map along with malloc but i am unable to name an array at run-time. I can keep some type of mapping with previously created arrays , but that would be very inefficient implementation.

Please help.

Regards,
Raghuvendra Kumar
Apr 4, 2010 at 5:57am
C++ has refined allocation new memory. Instead of malloc, please you the keyword new.

1
2
3
4
5
6
7
int read_length;
...
... // here you somehow determined the length
...
char *name = new char [length];
...
delete [] name;


or did i misunderstand anything?

Maikel
Apr 4, 2010 at 6:19am
I'm not sure I understand the OP's problem either.

Can you elaborate a little more, rash? Maybe give a small example?
Apr 4, 2010 at 10:29am
Sure. Well suppose we have a text file that contains names like student = 10, RollNo =3 , Age=3 etc. Now i want to write a program in c++ that can read that name and value pair and create an array respectively ...like

char student [10] = {0,};
char RollNo [3]={0,};
char Age [3]= {0,};

Now the problem is that i dont want to recompile the code. I mean the binary should read these values & create respective array variables and can use them as the logic of the program.Similarly by just changing the content of the text file we can have array name and size as per our requirement and that too without recompiling the code.

Actually i am writing a software tool for IVR appliations and this approach simplifies the coding for the tool users.
Apr 4, 2010 at 4:15pm
You could use a vector for each array, and then put those vectors in a map with a string key:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
map< string, vector<char> >  data;
typedef pair<string, vector<char> > datpair;
typedef map<string, vector<char> >::iterator datiter;

while( reading_the_file )
{
    string fieldname = GetNameFromFile();
    vector<char> fielddata = GetArrayFromFile();

    if( data.insert( datpair(fieldname,fielddata) ).second == false )
    {
        // an entry with the given 'fieldname' already existed
    }
}

// then to get a specific array
datiter i = data.find("students");  // note:  case sensitive

if(i == data.end())
{
    // "students" entry not found
}
else
{
    vector<char>& studentarray = i->second;

    // studentarray is now your student array
}
Apr 5, 2010 at 2:24pm
Yup.Agreed, But the problem is to how declare vector at run-time. I mean i dont want to recompile the code. just read the name from the text file and create an array by that name at run-time. I believe we need to declare vector and compile and then use it.

I used to work on a toolkit on windows, where i have a facility , whereby i could declare the global array at runtime and was also able to fetch the global array based on user inputs. For example, if i pass "SUB" and "_ARRAY" , at runtime i was able to declare SUB_ARRAY at runtime and by passing "SUB" and "_ARRAY", was able to
fetch SUB_ARRAY index values.
Function that was there for runtime declaration was:
glb_dec(<ArrayName>,<LowerIndex>,<UpperIndex>);
And for fetching index value was:
glb_getx(<ArrayName>,<Index>);

I have migrated that application on Linux in C/C++. Now i am finding it very difficult to develop the similar functionality. It used to ease application development and maintainance to a large extent.

Apr 5, 2010 at 6:30pm
disch's solution looks good to me.

One question, what data type(s) need to be stored in the array?
Apr 6, 2010 at 2:31pm
char or say string will do it for me. can u explain GetArrayFromFile() more.

vector<char>& studentarray = i->second; studentarray is again predeclared ...problem is that i can not have predeclared variable name. It should be runtime. This is because i will never now how many array variable i should make..its depened on the content of the text file.


Apr 6, 2010 at 4:18pm
closed account (S6k9GNh0)
? Vector elements do not require names. An array itself is simple a series of data held in sections of a certain size. A vector is the same thing except it allows simple resizing, addition, and deletion of the elements in the array.

GetArrayFromFile() is an example function. It doesn't actually exist and you have to implement it yourself. Same with GetNameFromFile().

And I fail to see what you are trying to do. There's no reason you need to identify your variables by the file names. Just make a vector of a struct that contains the name and data, and then iterate through the vector to find the filename you are wanting.
Apr 6, 2010 at 6:44pm
Well c++ is a totally different language. I'm not sure why it matters what the name of the vector object is or how that functionality makes your program any better.
Topic archived. No new replies allowed.