• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Alessandro

C++ pointer to struct [edited]

11 posts in this topic

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

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

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

[code]struct myObjects::myObjectLib *myObject;[/code]

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
0

Share this post


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

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

Share this post


Link to post
Share on other sites
You may want to consider using a std::vector instead.
0

Share this post


Link to post
Share on other sites
[quote name='Molle85' timestamp='1317846057' post='4869523']
[code]struct myObjects::myObjectLib *myObject = new myObjects::myObjectLib[MAX_OBJECTS];[/code]

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

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] ?
0

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.
[/quote]

They seem to be extremely complicated to use. Am I wrong?
0

Share this post


Link to post
Share on other sites
[quote name='Oberon_Command' timestamp='1317846418' post='4869526']
You also don't need the 'struct' keyword when you're declaring instances of structures in C++.
[/quote]

Right, thanks!
0

Share this post


Link to post
Share on other sites
[quote name='Alessandro' timestamp='1317847250' post='4869536']
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] ?
[/quote]
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='Alessandro' timestamp='1317847283' post='4869537']
[quote name='SiCrane' timestamp='1317846206' post='4869525']
You may want to consider using a std::vector instead.
[/quote]
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.
0

Share this post


Link to post
Share on other sites
[quote name='Alessandro' timestamp='1317847283' post='4869537']
[quote name='SiCrane' timestamp='1317846206' post='4869525']
You may want to consider using a std::vector instead.
[/quote]

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

yes, you're wrong.

[code]
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[i]);
if(vec[i] == 17)
printf("I didn't put this number in the vector!");
}
[/code]

output:
[quote]
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.
0

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!
0

Share this post


Link to post
Share on other sites
Though I am for using vectors, you still need to learn about memory allocations and deallocations. C++ has so many pitfalls that you need to be aware of. Improper usage of memory can turn your app inside out and you won't know why, and it's hell of a bitch to track down.
1

Share this post


Link to post
Share on other sites
the vector class is pretty cool if u have to change the size of memory at runtime.. consider the following example

[code]
t_tile **tile;

void makeMap (int rows, int cols) {

*tile = (t_tile**)malloc(sizeof(t_tile*rows));
for (int i = 0; i < rows; i++) {
tile[i] = (t_tile*)malloc(sizeof(t_tile*cols));
}

}
[/code]

So, now we have enough memory for a 2d array which we can access easily through tile[i][j]..
What if we want to change the map size on runtime AND we don't want to delete our old memory because we still need? (Consider a map editor)
We would have to do the following

[code]

void resizeMap (int rows, int cols) {

// allocate temp 2d array of the same size as the old map
t_tile **temp;

*temp = (t_tile**)malloc(sizeof(t_tile)*cols);
for (int i = 0; i < old_rows; i++) {
temp[i] = (t_tile*)malloc(sizeof(t_tile)*rows);
}

// copy the old value into temp
for (int i = 0; i < old_rows; i++) {
for (int j = 0; j < old_cols; j++) {
temp[i][j].dataset = tile[i][j].dataset;
}
}

// recalculate memory for tiles
*tile = (t_tile**)realloc(NULL, sizeof(t_tile)*rows);
for (int i = 0; i < rows; i++) {
tile[i] = (t_tile*)realloc(NULL, sizeof(t_tile)*cols);
}

// copy temp into new reallocated memory
for (int i = 0; i < old_rows; i++) {
for (int j = 0; j < old_cols; j++) {
if (
tile[i][j].dataset = temp[i][j].dataset;
}
}

for (int i = 0; i < old_rows; i++) {
free(*temp[i]);
}

free(temp);

old_rows = rows;
old_cols = cols;

}

[/code]

pretty much messy code.. btw, if there are any mistakes.. don't blame me.. its 1:10 AM here, i wrote that out of my memory after watching three apple dev videos about __weak and __autoreleased objects ^^

under the bottom-line, we see that this is a lot of code.. the vector class takes ALL this away from us!!! we can add and remove objects as we want and it is pretty efficient as well..

Only thing why i don't use it pretty often is...

it leaks when u store a pointer to a class in the vector.. so don't use

class_name *object with a STL vector.. only use class_name object

also keep in mind that it will always call the constructor and the destrunctor if u add or remove objects.. this can especially be very tricky if u store openGL textures in one memory locations accessed by a lot of objects and u delete this memory by calling glDeleteTexture in a destructor.. if one object gets removed from the vector list, all objects loose their reference..

i go to sleep now, good night :-)
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0