2

I'm trying to use an array in C++ that changes in size. For some reason the size does not change, it only ever holds 1 string. The difficult part is that the user cannot input the number of courses they are going to add, instead the addCourse function is called until the user stops. A vector cannot be used (this is for a school assignment, and a resizing array is required). I'm stuck as to why the array only seems to hold one string, I would think it to hold the equivalent of numCourses strings. How would I go about resizing to hold multiple strings after each call to the function?

void Student::addCourse(string* courseName)
{
    int x;
    numCourses += 1;//increments number of courses

    string newCourse = *courseName;

    string* newCourses = new string[numCourses];//temporary array

    for(x=0; x<numCourses - 1; x++)//fills temp array with the values of the old
    {
        newCourses[x] = courses[x];
    }

    newCourses[numCourses - 1] = newCourse;//adds extra value

    delete[] courses;//removes original array

    courses = newCourses;//sets the new course list
}

Edit: For those asking why a vector cannot be used because the point of the assignment is to actively avoid memory leak using the heap. Using an array like this forces intentional delete of stored values.

21
  • 3
    std::vector<std::string>... Commented Oct 27, 2013 at 21:35
  • 4
    "A vector cannot be used (this is for a school assignment [...]" You should go to suggest your teacher to learn C++. That exercise and style is the way to learn C, not the way to learn good C++ Commented Oct 27, 2013 at 21:39
  • 1
    There is nothing (functionally) wrong with the code you've presented: ideone.com/khAjuD. Why do you think that it doesn't resize the array? Commented Oct 27, 2013 at 21:39
  • 3
    @BartoszKP one of the reasons because there are a lot of bad "C++ programmers" is that people still keep teaching C++ as if it was C. C++ should be teached as if it was C++. I'm pretty sure that no one python tutorial starts with pointers, stack vs heap, and how python tuples are implemented. It starts with the natural and user-friendly syntax of python. That should be the same for C++ teaching/learning. Commented Oct 27, 2013 at 21:50
  • 2
    Visual Studio shows only one element in the debugger for dynamic arrays (the debugger has no direct way to determine how much elements are in that array). And for Visual Studio this isn't even an array but in the first case only a pointer to a string. Commented Oct 27, 2013 at 22:35

2 Answers 2

1

The comment should have answered your question: there is no way for the debugger to know that a pointer to a string is pointed to an array, nor does it know its bounds, because no such information is kept at runtime (a std::vector will show its whole contents in the debugger, in contrast).

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

Comments

0

Your method prototype should read:

void Student::addCourse(const string& courseName);

If you don't want to have a memory leak, declare a pointer to courses in your class:

private:
    string* courses;

Allocate space for an array of strings in your constructor:

Student::Student() 
{
    courses = new String[5];
}

Then deallocate in the destructor: Student::~Student() { delete[] courses; }

This gives you room for up to 5 courses. If you need more you need to adjust the size of the array of strings at run time:

void Student::ExtendArray()
{
    delete[] courses;
    courses = new String[10];
}

Note this code is not exception safe, but will give you the basic idea.

1 Comment

You can delete your own comments by clicking on the "x" symbol that appears at the end of your comment when you hover over your comment.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.