Jump to content
  • Advertisement
Sign in to follow this  
jpmonette

[C++] Dynamic Memory for my Player List

This topic is 4137 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello. I'm actually developing a MMORPG, and the server is sending to the client the informations about users connecting to his map. Each time someone join, I need to add a new row in the memory. Example structure : struct character { unsigned int id; SDL_Surface* image; SDL_Rect rectangle[6]; }; So each time there's a new player, I need to allocate new memory space to add a new row: ID | IMAGE | RECTANGLE[6] ############################# 1 | IMAGE | Rectangle 2 | IMAGE_2 | Rectangle_2 3 | IMAGE_3 | Rectangle_3 <---- new information And when someone leave the map, I need to remove the entry in the memory and it might be a little complicate if the user is in the middle. I don't really know how I can do that in C++... If I use: variable = new int ;, i need to delete the content and recreate each time to expand the size. I don't know if i'm clear, I guess I need to practice my English a bit... Thanks.

Share this post


Link to post
Share on other sites
Advertisement
Use the C++ standard library. It contains a series of containers designed for exactly this kind of thing, which can be quickly interchanged.

Since you have an ID field, I'm assuming you're going to look characters up by this ID, which I'm assuming won't change. std::map would be the obvious choice in this case, since it's designed to map keys (such as your ID) to values (such as the rest of your character data).

Unless character needs to know it's own ID (which could very well be the case), I would something along the lines of:


#include <map> //for std::map
#include <cassert> //for assert

struct character_data {
SDL_Surface* image;
SDL_Rect rectangles[6];
};

int main() {
std::map< unsigned int , character_data > characters;
characters[4].image = SDL_CreateSurface(...); //With std::map, operator[] will always return an entry, which may involve creating a new one.

// Explicitly searching for only existing entries:
std::map< unsigned int , character_data >::iterator character;

// Since #42 doesn't exist, this will evaluate to true:
character = characters.find(42);
assert(character == characters.end());

// Since #4 exists, we can access it's members:
character = characters.find(4);
assert(character != characters.end());
SDL_Surface* character_4_image = character->image;
}





A random link into the MSDN documentation to get you started on finding out more about this and other standard library components: std::map::insert

Obligitory google link:

C++ container tutorials

Share this post


Link to post
Share on other sites
i would use the sizeof() and malloc() methods. do a search for these on microsofts documentation and hopefully they will help you. Good luck with the game.

Share this post


Link to post
Share on other sites
Referring myself to this example:


// accessing mapped values
#include <iostream>
#include <map>
#include <string>
using namespace std;

int main ()
{
map<char,string> mymap;
map<char,string>::iterator it;

mymap['a']="an element";
mymap['b']="another element";
mymap['c']=mymap['b'];

cout << "mymap['a'] is " << mymap['a'] << endl;
cout << "mymap['b'] is " << mymap['b'] << endl;
cout << "mymap['c'] is " << mymap['c'] << endl;
cout << "mymap['d'] is " << mymap['d'] << endl;

cout << "mymap now contains " << (int) mymap.size() << " elements." << endl;

return 0;
}



I can use some char for my keys? :o Sounds nice...!

Thanks, I'll look at this!

[Edited by - jpmonette on March 23, 2007 11:18:54 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by jpmonette
I can use some char for my keys?


Yes, as well as std::string, and anything else that's copy constructable and comparable (or for which an explicit comparison functor can be written). Very useful :-).

Share this post


Link to post
Share on other sites
Quote:
Original post by Sneftel
Moved to For Beginners (bad sign when it's an MMORPG post...)


Well, I'm not that noob... I do not plan to do the best game out there, it's more a simple prototype, with the basic engine...

Thanks anyway lol

Share this post


Link to post
Share on other sites
Quote:
Original post by Payton
heres a little code that I hope can help explain a little. If you look in main, you can see that i created a dynamic object of the soldier struct . In the getPref function, i created 2 dynamic strings, for the soldiers name and his weapon. I figure you can make it out the rest of the program for yourself since your already starting to code a simple mmorpg. After you use what you need, notice i deleted before the program ended. Make sure you delete them in the correct order to avoid a memory leak. If you have any questions about it just pm me.
*** Source Snippet Removed ***


1) No no no. delete != delete[]. Both these lines invoke undefined behavior, by calling delete (without the []) on an array allocated with new[]:

delete newSoldier->name;
delete newSoldier->weapon;

For classes, this will usually result in improper destructor calling. But anything, including nuclear missiles being launched at a cow ranch in Alaska, is possible even under a fully compliant C++ compiler.

2) No no no. Such lines should be implemented in a class destructor -- that of newSoldier's class. This fundamental and powerful technique is used by the C++ standard library throughout, and is known as RAII.

3) No no no. It's clear that the OP knows how to use new[], and knows that such allocations need to be cleaned up afterwards. He either dislikes the repetitive aspect of it, shuns the sillyness and it's associated performance penalty, or both, rightfully so.

4) std::string is probably (definately to my mind) preferable instead of (char*), given that it implements the RAII already mentioned in #2, requiring no extra effort on your part as you need to as is. You can load directly to them with std::getline( cin , newSoldier->name ) and such.

Share this post


Link to post
Share on other sites
Quote:
Original post by jpmonette
Quote:
Original post by Sneftel
Moved to For Beginners (bad sign when it's an MMORPG post...)


Well, I'm not that noob... I do not plan to do the best game out there, it's more a simple prototype, with the basic engine...

Thanks anyway lol


You would do well not to *call* it an MMORPG, in that case. Keep your mind free of those ideas. In the same way that a beginning music student, correctly starting off with some small composition (maybe a 12-bar jazz improvisation or something, I dunno) doesn't try to call it an opera.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!