LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 03-07-2011, 08:07 PM   #1
Thesniperofdeath
LQ Newbie
 
Registered: Jan 2011
Distribution: Gentoo, Slackware, Arch
Posts: 27

Rep: Reputation: 1
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.
 
Old 03-07-2011, 08:54 PM   #2
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
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
 
Old 03-07-2011, 09:00 PM   #3
Thesniperofdeath
LQ Newbie
 
Registered: Jan 2011
Distribution: Gentoo, Slackware, Arch
Posts: 27

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by corp769 View Post
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.
 
Old 03-07-2011, 09:21 PM   #4
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
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.
Old 03-07-2011, 09:48 PM   #5
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,732
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
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.
Old 03-07-2011, 11:09 PM   #6
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
Quote:
Originally Posted by Anisha Kaul View Post
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++
 
Old 03-07-2011, 11:27 PM   #7
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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.
Old 03-08-2011, 04:20 PM   #8
z1p
Member
 
Registered: Jan 2011
Location: the right coast of the US
Distribution: Ubuntu 10.04
Posts: 80

Rep: Reputation: 23
Quote:
Originally Posted by paulsm4 View Post
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
 
Old 03-08-2011, 05:28 PM   #9
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
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.
Old 03-09-2011, 03:06 AM   #10
posixculprit
Member
 
Registered: May 2010
Posts: 136

Rep: Reputation: 42
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.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
c++ dynamic array Fredstar Programming 16 03-19-2009 08:26 PM
Dynamic Array Sizes JMJ_coder Programming 8 03-05-2008 07:33 PM
java test if string in string array is null. exodist Programming 3 02-21-2004 01:39 PM
More dynamic array problems PTBmilo Programming 5 03-14-2003 12:51 AM
dynamic array in class help PTBmilo Programming 6 03-09-2003 02:35 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:02 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration