Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
03-07-2011, 08:07 PM
|
#1
|
LQ Newbie
Registered: Jan 2011
Distribution: Gentoo, Slackware, Arch
Posts: 27
Rep:
|
C/C++ dynamic string array?
Can you create a dynamic string array and using calloc(),realloc()?
Like:
Code:
string* x;
x = (string) calloc(x,sizeof(string));
Last edited by Thesniperofdeath; 03-07-2011 at 08:45 PM.
|
|
|
03-07-2011, 08:54 PM
|
#2
|
LQ Guru
Registered: Apr 2005
Location: /dev/null
Posts: 5,818
|
I know for in C, it would be like so:
Code:
string *string1;
array=(string *) malloc(size*sizeof(string));
I know that is using malloc(), but should be almost the same for you. C++ will normally cast for you:
Code:
string *string1;
string1=new string[size]
Josh
|
|
|
03-07-2011, 09:00 PM
|
#3
|
LQ Newbie
Registered: Jan 2011
Distribution: Gentoo, Slackware, Arch
Posts: 27
Original Poster
Rep:
|
Quote:
Originally Posted by corp769
I know that is using malloc(), but should be almost the same for you. C++ will normally cast for you:
Code:
string *string1;
string1=new string[size]
|
How would you resize then?.thanks.
|
|
|
03-07-2011, 09:21 PM
|
#4
|
LQ Guru
Registered: Apr 2005
Location: /dev/null
Posts: 5,818
|
Check this link out:
http://www.cplusplus.com/forum/general/11111/
I got to get going, I'm about to get off of work. I will return once I get home.
Josh
|
|
1 members found this post helpful.
|
03-07-2011, 09:48 PM
|
#5
|
Senior Member
Registered: Dec 2008
Posts: 4,732
|
But C++'s string is already re-sizable:
Code:
#include <iostream>
using namespace std;
int main ()
{
string s;
s.append ("abc");
s.append ("def");
s.append ("ghi");
cout << s << "\n";
}
|
|
2 members found this post helpful.
|
03-07-2011, 11:09 PM
|
#6
|
LQ Guru
Registered: Apr 2005
Location: /dev/null
Posts: 5,818
|
Quote:
Originally Posted by Anisha Kaul
But C++'s string is already re-sizable:
Code:
#include <iostream>
using namespace std;
int main ()
{
string s;
s.append ("abc");
s.append ("def");
s.append ("ghi");
cout << s << "\n";
}
|
Thanks Anisha, I keep thinking of C instead of C++ 
|
|
|
03-07-2011, 11:27 PM
|
#7
|
LQ Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Rep: 
|
Hi -
Code:
// Wrong!
string* x;
x = (string) calloc(x,sizeof(string));
Code:
/* Correct for C */
char *s = (char *)malloc (4);
strcpy (s, "ABC");
s = realloc (s, 7);
strcpy (s, "ABCDEF");
Code:
// Correct for C++
string s = "ABC";
s.append ("DEF");
|
|
2 members found this post helpful.
|
03-08-2011, 04:20 PM
|
#8
|
Member
Registered: Jan 2011
Location: the right coast of the US
Distribution: Ubuntu 10.04
Posts: 80
Rep:
|
Quote:
Originally Posted by paulsm4
Hi -
Code:
// Wrong!
string* x;
x = (string) calloc(x,sizeof(string));
Code:
/* Correct for C */
char *s = (char *)malloc (4);
strcpy (s, "ABC");
s = realloc (s, 7);
strcpy (s, "ABCDEF");
Code:
// Correct for C++
string s = "ABC";
s.append ("DEF");
|
I think this would be a a C++ version that is closer to the C version.
Code:
// Correct for C++
string s = "ABC";
s = "ABCDEF"; // the string object will adjust its internal memory as needed
|
|
|
03-08-2011, 05:28 PM
|
#9
|
Senior Member
Registered: Jan 2003
Posts: 2,786
|
Quote:
Originally Posted by Thesniperofdeath
How would you resize then?
|
Anisha has already pointed out that C++ strings can change size on-the-fly. However, I assume your question is not about the length of the individual strings, but the number of strings in the array itself.
The way I normally do it is to create a vector whenever I need an array. My approach is a little different from corp769. Instead of:
Code:
string *string1;
string1=new string[size]
I would probably do something like:
Code:
std::vector<string> stringArray;
...
stringArray.push_back("ABCDEF");
stringArray.push_back("GHIJKL");
...
stringArray.erase(stringArray.begin());
Of course, the erase() method can be used to remove any member of the array that you want as long as it's given the right arguments; you're not limited to erasing only the first element.
And you can access the individual strings just like a traditional array:
Code:
std::cout << stringArray[2] << std::endl;
EDIT:
I didn't read the link corp769 gave. So if this was covered in it, my mistake.
Last edited by Dark_Helmet; 03-08-2011 at 05:33 PM.
|
|
1 members found this post helpful.
|
03-09-2011, 03:06 AM
|
#10
|
Member
Registered: May 2010
Posts: 136
Rep:
|
Hello. Just a little observation, if I may, even though it might be somewhat off-topic. Unless one really knows what one is doing and decides that it is absolutely necessary, it is not a good idea to use C standard library memory allocation functions (*alloc()) in C++ programs. For instance, unlike new, *alloc() functions do not call object constructors and unlike delete, free() does not call object destructors.
Last edited by posixculprit; 03-09-2011 at 03:07 AM.
|
|
2 members found this post helpful.
|
All times are GMT -5. The time now is 03:02 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|