0

I don't understand the way this is working. Im tring to create an array of strings from a list of strings. I count the number of strings in the list and then want to create an array of these strings. I was doing some testing and came up with this code:

string *newOrder;

int numNodes;

numNodes = alphaTree.numNodes();

newOrder = new string [numNodes];

newOrder[0] = "This is";
newOrder[1] = "a test";
newOrder[2] = "To see";
newOrder[3] = "If this";
newOrder[4] = "will work";

The results are that newOrder acts like it is a single string array having the vaule "This is". What am I doing wrong?

4
  • 1
    Please use std::vector. All use of new, new[], delete, delete[], ~T(), malloc, or free should be avoided except in a class that does nothing else but call them (for example, when writing a custom smart pointer or typesafe union). Commented Sep 28, 2014 at 4:07
  • Too new to programing. I don't know what a smart pointer or typesafe union is, but I'd like to know your reasoning for not using the code you listed above. Commented Sep 29, 2014 at 4:59
  • 1
    The point is only advanced things such as smart pointers and typesafe unions should call those things directly. Using a vector prevents leak of memory and can protect against buffer overruns. Commented Oct 1, 2014 at 23:02
  • 1
    Voting to close as the code is correct but the problem was misunderstanding of the debugger view. Commented Oct 2, 2014 at 1:05

2 Answers 2

1

Check if numNodes = alphaTree.numNodes(); is returning desired size.

The following is a correct piece of code, allocates for 5 strings, and assigns.

newOrder = new string [5];
newOrder[0] = "This is";
newOrder[1] = "a test";
newOrder[2] = "To see";
newOrder[3] = "If this";
newOrder[4] = "will work";

If you execute the following statement:

cout << newOrder[2] << endl;

This will print: To see

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

6 Comments

it returns 5. Using the debugger in VS I can see the end result after all the strings have been assigned and all it shows is "This is". It has index numbers next to each letter when I expand it in the locals window.
Ahh yes. I guess since newOrder is a pointer it will only show the first element in the array that is pointing to. Thanks! Alow my self... to confuse...my self....
@user3600424 If the problem is the debugger, then reword your question that it is a debugger display issue, not a programming issue. Right now, your question sounds like a programming issue.
Perhaps my comments were worded poorly. This was a programming issue.
@user3600424 So how is the answer you accepted different than the code you posted in your question? I see no difference, except that we don't see a couple of variable declarations, and that numNodes is used instead of 5.
|
0
using std::string;
using std::vector;

// from an initializer_list
vector<string> newOrder1 = {
    "This is",
    "a test",
    "To see",
    "If this",
    "will work",
};

// as a sequence of appends
// often used in a loop if an iterator is not applicable
vector<string> newOrder2;
newOrder2.push_back("This is");
newOrder2.push_back("a test");
newOrder2.push_back("To see");
newOrder2.push_back("If this");
newOrder2.push_back("will work");

// from an iterator-pair over any standards-compliant container
vector<string> newOrder3(alphaTree.begin(), alphaTree.end());

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.