LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-25-2006, 06:46 PM   #1
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Rep: Reputation: 50
[C++] Classes and dynamic memory


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?

Regards,
 
Old 12-25-2006, 07:27 PM   #2
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
This is wrong for C++. This is even wrong for C. Use std::vector instead of arrays. push_back() will do what you want.
 
Old 12-25-2006, 08:20 PM   #3
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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.
 
Old 12-25-2006, 08:37 PM   #4
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Original Poster
Rep: Reputation: 50
Thanks for your fast replies. I will check std::vector out right away.
 
Old 12-25-2006, 11:39 PM   #5
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Original Poster
Rep: Reputation: 50
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:

std::vector<MyClass> my_instance;
my_instance.push_back(MyClass);

but it results in the error:

error: expected constructor, destructor, or type conversion before ‘.’ token

Although, it works if I put the declaration and push_back in a function, but I need it to be globally. :/

Any ideas?
 
Old 12-26-2006, 01:08 AM   #6
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
the declaration should work globally but not the push_back call. this will need to be inside another function.
 
Old 12-28-2006, 07:10 PM   #7
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Original Poster
Rep: Reputation: 50
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?
 
Old 12-28-2006, 07:32 PM   #8
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
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!
 
Old 12-29-2006, 12:49 PM   #9
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Original Poster
Rep: Reputation: 50
Yes I have tested. And I end up with

551: error: expected primary-expression before ‘;’ token

This error message was brought to you by:
Code:
550: animations.reserve(st+1);
551: animations.at(st) = MyClass;

Last edited by Ephracis; 12-29-2006 at 12:51 PM.
 
Old 12-29-2006, 01:11 PM   #10
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
dont really know sorry. is MyClass an object? does it help if you put & in front of MyClass?
 
Old 12-29-2006, 01:51 PM   #11
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Original Poster
Rep: Reputation: 50
Quote:
Originally Posted by nadroj
dont really know sorry. is MyClass an object? does it help if you put & in front of MyClass?
My class is a class, and I want a vector of instances.
 
Old 12-29-2006, 03:20 PM   #12
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
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.
 
Old 12-29-2006, 03:51 PM   #13
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
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.
 
Old 12-29-2006, 04:43 PM   #14
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
tuxdev I can't believe I actually wrote that. Every day I learn n new things and forget n^2, I think it's a age thing.
 
Old 12-29-2006, 04:45 PM   #15
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
As long as n=1, you're safe. :-)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic Memory Allocation jvff Programming 1 09-05-2005 05:01 AM
dynamic memory allocation failure guam Programming 4 04-13-2005 09:16 AM
G++ memory layout of the classes shibdas Programming 4 09-20-2004 07:17 AM
pointers and dynamic memory allocation deveraux83 Programming 2 01-24-2004 10:35 AM
C++ dynamic classes tristan_vdv Programming 1 01-04-2002 04:30 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:14 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration