ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have a class (MyClass) in which I have an array of N elements of type MyItem.
MyClass has a public method called MyClass::addItem() which would allocate memory and create a new instance of MyItem. I use the private variable numPos to keep track of how many instances of MyItem I have within the array.
psuedo code:
Code:
class MyItem {
...
};
class MyClass {
private:
int numPos;
MyItem *items;
public:
void addItem();
};
MyClass::addItem() {
items[numPos] = new MyItem();
}
I hope you understood the above, cause here comes the question:
the function addItem allocates memory when called. Will this code do it or will I have to call malloc too?
Just to clarify what tuxdev has said, memory is being allocated in two different ways. First the allocation of memory for your new object myItem which is achieved by the new keyword. The other is the storage of the memory location of the new object into the member variable item. item has been declared as a pointer to a MyItem object and in addItem you are using it as an array of a pointer to MyItem objects.
How many elements are in this array? Answer one.
How many do you want? Unknown, so you require a data structure that will dynamically resize an array.
So as tuxdev said the use of std::vector will solve your problem because it will dynamically allocate sufficient storage to hold the details that you require.
std::vector did the trick, although I tried to use it in another case, where I have a globally declared instance of a class and I tried to declare a bunch of those using vector:
Is there a way I can modify a certain index in a vector? I have a file that I parse and I want to insert number from a row at a certain position, but the problem is that the file may specify the position 10 before 1.
So I figured that I should use vector::reserve() since it will grow the vector if it's needed. But then, how will I set vector::at(N) to a value? Can I just do something like vector::at(N) = MyClass?
have you tested this? looking at the documentation (here http://www.cppreference.com/cppvector/at.html), it says it returns a reference (pointer) to the object at that index. with that reference you should be able to reassign it to something else of the same type. sorry i havent done any programming in months and almost forget it!
Have you overloaded the equal operator? otherwise you are trying to reset a reference which is not allowed. There are many more questions I could ask but I think it maybe better to see your code.
dmail: Operator = is provided by the compiler if you don't declare it.
If you want an instance, you have to use MyClass(), not just MyClass. I don't think a vector is really supposed to be used that way. It sounds more like how a map is used.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.