0

I want to create objects of a class with user defined size at run time. Like when user enter 5 it creates 5 objects. I tried this

student s[num];

for(int i=0;i<num;i++)
{
    student s[i]; // also used student s[i]=new student();
}

but it says student s[num] expression must have constant value. So how to do this?

6
  • 9
    std::vector<student> s(num); Commented Oct 28, 2016 at 14:19
  • more like ArrayList. Commented Oct 28, 2016 at 14:27
  • vector<student> s(num); says incomplete type not allowed. Commented Oct 28, 2016 at 14:32
  • #include <vector> Tricky Commented Oct 28, 2016 at 14:33
  • That's in my answer, the include is vector Commented Oct 28, 2016 at 14:34

2 Answers 2

3

A std::vector was made for just this purpose. (basically a dynamically allocated array) It has a constructor taking the number of elements to pre-allocate and construct:

#include <vector>
std::vector<student> students(num);

You can loop over it as a normal array:

for(unsigned int i = 0; i < students.size(); ++i)
{
  //students[i]..
}

Or, if you have access to C++11 use a range-based loop:

for(auto const& student : students)
{
  //student..
}

Or, it's iterators:

for(std::vector<student>::iterator itr = students.begin(); itr != students.end(); ++itr)
{
  //access with *itr..
}
Sign up to request clarification or add additional context in comments.

10 Comments

Perhaps use students.at(i) instead of students[i], in order to get run-time bounds checking.
@AaronMcDaid we're using i < students.size() so [i] must be valid.
I know, but I still just think nobody should ever use [] on vectors. It's a bad habit to allow it. If I was teaching C++, I would never even mention it's existence! :-)
@AaronMcDaid How exactly? bounds checking just adds pointless overhead.
I don't understand your objection. Given a choice between always using [] and always using .at(), then former will definitely introduce undefined behaviour into every non-trivial codebase. I will never believe that anybody, even the smartest person, can avoid all incorrect usage of []. So the overhead is definitely not "pointless" in general
|
0

Alternatively, you can dynamically allocate and deallocate. If you new[] don't forget to delete[]!

#include <iostream>



struct MyObj {
    float a;
    float b;
};


int main() {

    int num = 10;

    MyObj* objs = new MyObj[num];

    for(int i = 0; i < num; i++) {
        objs[i].a = i;   
        objs[i].b = i*2;
    }

    std::cout << objs[3].a << " * 2 = " << objs[3].b << std::endl;

    delete [] objs;

    return 0;
}

Test

2 Comments

should delete all object at last or delete them as soon as they out of use??? or is there any garbage collector in C++ like C#.net???
There is no garbage collection. If you don't delete an object you will introduce memory leaks. You should delete the object whenever you are done with it. Whenever makes logical sense to ensure you don't lose a block of memory by re-assigning its pointer before deleting it.

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.