Declare string array in heap
-
Hi Can anyone plz tell me how to declare a string array in heap? I also dont want to specify its size at the time of declaration. Thanks
We Believe in Excellence www.aqueelmirza.cjb.net
-
Hi Can anyone plz tell me how to declare a string array in heap? I also dont want to specify its size at the time of declaration. Thanks
We Believe in Excellence www.aqueelmirza.cjb.net
std::vectorstd::string string_array; At least that is how it is done in C++. Of course that is not really a heap only version, because for that you would have to know the size of the array and the maximum length of any string placed in the array. But for your question it is close enough to qualify as a resonable answer. INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra
-
Hi Can anyone plz tell me how to declare a string array in heap? I also dont want to specify its size at the time of declaration. Thanks
We Believe in Excellence www.aqueelmirza.cjb.net
Of course it depends on what you choose as a suitable string. For instance C-like strings are simply (NULL-terminated) character arrays, hence you can do something like:
char ** myStringArray;
myStringArray = new char * [3];
myStringArray[0] = "hello world";
myStringArray[1] = new char[10];
memset(myStringArray[1], '*',9);
myStringArray[1][9]='\0';
myStringArray[2] = strdup("Hi");
//Clean-up
delete [] myStringArray[1];
free (myStringArray[2]);
delete [] myStringArray;On the other hand, MFC CString or std::string are objects controlling themselves the inner character buffer, therefore you can do, for instance:
CString * myStringArray;
myStringArray = new CString[2];
myStringArray[0]="Hello World";
myStringArray[0] += "!";// note CString dynamically controls its buffer
myStringArray[1]=CString('*',9);// Clean-Up
delete [] myStringArray;Hope that helps :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.