Jump to content
  • Advertisement
Sign in to follow this  
Alessandro

C++ pointer to struct [edited]

This topic is 2574 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

Hi, I have the following code that I use to declare a static number of structs and fill it with data:


struct myObjects::myObjectLib myObject[MAX_OBJECTS];
for (int i=0; i<MAX_OBJECTS; i++)
{
myObject.x=...
myObject.y=...
myObject.z=...
}


I'd like to define structs dinamically and so I used the following instruction:

struct myObjects::myObjectLib *myObject;

How can I initialize myObject struct so that later I can fill it up with values like in the static example?

Thanks again for your help

Share this post


Link to post
Share on other sites
Advertisement
struct myObjects::myObjectLib *myObject = new myObjects::myObjectLib[MAX_OBJECTS];

You need to learn about memory allocation, you have to free the memory yourself via 'delete'

Share this post


Link to post
Share on other sites

struct myObjects::myObjectLib *myObject = new myObjects::myObjectLib[MAX_OBJECTS];

You need to learn about memory allocation, you have to free the memory yourself via 'delete'


Mmmhhhh... why using the pointer (*myObject)? Why also the MAX_OBJECTS?

I was thinking instead that the following shoud work:

myObject = new myObjects::myObjectLib;

Everytime I call it, shouldn't it create a new struct, so that I'd have a myObject[0],myObject[1],myObject[n] ?

Share this post


Link to post
Share on other sites

I was thinking instead that the following shoud work:
myObject = new myObjects::myObjectLib;
Everytime I call it, shouldn't it create a new struct, so that I'd have a myObject[0],myObject[1],myObject[n] ?

No that will not work. new myObjects::myObjectLib gives you a pointer to a newly created myObjectLib object. If you don't keep track of the other objects they are lost, and you have a memory leak.


[quote name='SiCrane' timestamp='1317846206' post='4869525']
You may want to consider using a std::vector instead.

They seem to be extremely complicated to use. Am I wrong?
[/quote]
Yes you are wrong. std::vector is much easier to work with. You can declare the vector like this: std::vector<myObjects::myObjectLib> myObject;
To add a new element in the vector you can do something like myObject.push_back(myObjects::myObjectLib());
You can access the elements like in a normal array, myObject[0] being the first element in the vector.

Share this post


Link to post
Share on other sites

[quote name='SiCrane' timestamp='1317846206' post='4869525']
You may want to consider using a std::vector instead.


They seem to be extremely complicated to use. Am I wrong?
[/quote]

yes, you're wrong.


std::vector<int> vec;
vec.push_back(5);
vec.push_back(6);
vec.push_bacK(3);

for(unsignd int i = 0; i < vec.size(); i++)
{
printf("%d\n", vec);
if(vec == 17)
printf("I didn't put this number in the vector!");
}


output:

5
6
3
[/quote]

Allocates space whenever it needs it, frees space it doesn't need, calls the destructor of every object that's inside the vector (in the case of classes/structs).

See http://cplusplus.com/reference/stl/vector/ for more info.

Share this post


Link to post
Share on other sites
Shame on me. Followed your suggestions and examples about vectors and they work perfectly. Thanks again for your continued help!

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!