0

I'm new to C++, and I want to know if this valid.

So I want to create an array of strings, but I wont know the size of the array I need until a value is passed in to a function within the class I'm working in. So, can I do this:

string sList[];

void init(unsigned int size=1)
{
    sList = new string[size];

 }

I'm sorry if this is a dumb question, but I'm a Java guy new to C++.

EDIT: This is an assignment that involves me writing an array wrapper class. If I could use vector<>, trust me, I would.

5
  • This is valid, but do you have a reason not to use std::vector<> instead of flat arrays? Commented Apr 22, 2013 at 22:19
  • Unfortunately, yes. I have to write a wrapper class for arrays, and the function above is basically the constructor. Commented Apr 22, 2013 at 22:21
  • @user1795374: No, you definitely do not have to write a wrapper class yourself in C++. Whoever is telling you this is either lying to you or wants to make you unemployable in the C++ job market. Commented Apr 22, 2013 at 22:22
  • When I said I have to, I mean it's an assignment. Commented Apr 22, 2013 at 22:23
  • Why would a constructor be called init? There's absolutely positively no reason for that being an assignment requirement, and that will quickly make you unemployable. Commented Apr 23, 2013 at 0:11

3 Answers 3

1

This is correct, although string sList[] should be string *sList. Don't forget the delete [] sList at the end.

As many others say, you can use std::vector if you want, but getting some practice with arrays is an excellent way to learn how the memory management works and I encourage you to explore it further.

Sign up to request clarification or add additional context in comments.

5 Comments

Or use unique_ptr< string[] > and forget about delete[]. Though I also like vector better.
well, as I was saying, it is better to start old school until you get a good understanding of how it works, so I would advice to stay away from smart pointers as well, at least until you know how to code one yourself.
“it is better to start old school” – no, that is a myth. It’s wrong. Education works better when you start top-down rather than working bottom up.
Well, i am not qualified and motivated enough to sparkle a debate on education now. My point was just, I see a lot of students sorting arrays using object.sort() because it is there, without even wondering what are the different ways to sort an array and which one would you choose in which situation. I think telling the OP pointers are outdated and he shouldn't use them when he is showing interest is bad advice, feel free to disagree.
@EdS. OP: "but I wont know the size of the array I need until a value is passed in to a function" = the size of the array is only known at run-time.
1

A new-expression (such as new string[size]) returns a pointer to a dynamically allocated object. In this case, it returns a pointer to the first string object in the dynamically allocated array. So to make this work, sList should be a pointer:

string* sList;

It is important to remember that you must always delete/delete[] and object that has been created with new/new[]. So you must at some point delete[] sList. If you don't do this, the memory allocated for the array of strings will “never” be deallocated.

However, you'll be in a much better situation if you use a std::vector<std::string> instead, rather than doing your own dynamic allocation.

1 Comment

Could I do this in my destructor for the class that this is inside of?
1

The right way to do this in C++:

#include <string>
#include <vector>

std::vector<std::string> sList;

void init(unsigned int size = 1)
{
    sList.resize(size);
}

int main()
{
    init(25);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.