• 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
captacha

C++

6 posts in this topic

I'm using a dynamiclly allocated array in my code. If I have 4 elements in my array and I put [CODE]
Array[4] = new Object[5]
[/CODE] will the first four elements remain intact?
0

Share this post


Link to post
Share on other sites
Yes, that won't modify Array[0/1/2/3]... but...
Can you post some real, compilable C++ code? That code could be doing a lot of different things... it's probably not doing what you think it's doing -- it only makes sense if Array is an "array of arrays of objects", and you're setting the array at index 4 to be a new array of 5 objects. Edited by Hodgman
1

Share this post


Link to post
Share on other sites
If you have something like
[code]
Object *Array[4];

Array[4] = new Object[5];
[/code]

You would be using memory outside of Array (Array[4] is one past the end).

If you have
[code]
std::vector< Object * > Array(4, 0);
Array[4] = new Object[5];
[/code]

You would again be using memory outside the array.

Otherwise if you have the first and do assign to a Array[X] where X is in bounds, all the other Array elements remain unchanged.
1

Share this post


Link to post
Share on other sites
[quote name='Hodgman' timestamp='1342329609' post='4959190']
Yes, that won't modify Array[0/1/2/3]... but...
Can you post some real, compilable C++ code? That code could be doing a lot of different things... it's probably not doing what you think it's doing -- it only makes sense if Array is an "array of arrays of objects", and you're setting the array at index 4 to be a new array of 5 objects.
[/quote]
I just made the example up off the top of my head, so that's why I wasn't following 0-indexing rules. Here's some code I'm using that was I was thinking about[CODE]
void Player::addToList(Object* object)
{
colListSize++;
colList[colListSize-1] = new Object[colListSize];
colList[colListSize-1] = object;
}
[/CODE]

colList is a Dynamic array of Pointers to class Object. It holds all the data I need for managing collisions. colListSize contains the size of the array. It starts off at 0 and increments every time the function is called. What it's supposed to do is add a pointer to the object to the end of the array. I haven't tested it out yet, so I don't know whether or not it works.
0

Share this post


Link to post
Share on other sites
Okay, so you'd like to resize the array to make room for a new [tt]Object*[/tt]. In C++, this is done using [url=http://www.cplusplus.com/reference/stl/vector/]std::vector[/url] instead of raw arrays:
[code]
std::vector<Object*> colList;

void Player::addToList( Object* object ) {
colList.push_back( object );
}

int Player::getColListSize() const {
return colList.size();
}
[/code]

Using raw arrays, you need to reallocate enough space to hold N+1 [tt]Object*[/tt]s, and move those [tt]Object*[/tt]s from the old array into the new array, and destroy the old array:
[code]
Object* newArr = new Object*[N+1];
for ( int idx = 0; idx < N; ++idx )
newArr[idx] = colList[idx];
delete[] colList;
colList = newArr;
[/code]


Most implementations of [tt]std::vector[/tt] do the same as above behind the scenes. Edited by fastcall22
1

Share this post


Link to post
Share on other sites
[quote name='fastcall22' timestamp='1342338572' post='4959210']
Okay, so you'd like to resize the array to make room for a new [tt]Object*[/tt]. In C++, this is done using [url="http://www.cplusplus.com/reference/stl/vector/"]std::vector[/url] instead of raw arrays:
[code]
std::vector<Object*> colList;

void Player::addToList( Object* object ) {
colList.push_back( object );
}

int Player::getColListSize() const {
return colList.size();
}
[/code]

Using raw arrays, you need to reallocate enough space to hold N+1 [tt]Object*[/tt]s, and move those [tt]Object*[/tt]s from the old array into the new array, and destroy the old array:
[code]
Object* newArr = new Object*[N+1];
for ( int idx = 0; idx < N; ++idx )
newArr[idx] = colList[idx];
delete[] colList;
colList = newArr;
[/code]


Most implementations of [tt]std::vector[/tt] do the same as above behind the scenes.
[/quote]
So you're saying I should use a vector instead of manual memory allocation. vectors gave me a bunch of weird errors in my last project with subscript errors or something. I figured this would be easier. Oh Well.
0

Share this post


Link to post
Share on other sites
[quote name='Captacha' timestamp='1342332556' post='4959196']
[quote name='Hodgman' timestamp='1342329609' post='4959190']
Yes, that won't modify Array[0/1/2/3]... but...
Can you post some real, compilable C++ code? That code could be doing a lot of different things... it's probably not doing what you think it's doing -- it only makes sense if Array is an "array of arrays of objects", and you're setting the array at index 4 to be a new array of 5 objects.
[/quote]
I just made the example up off the top of my head, so that's why I wasn't following 0-indexing rules. Here's some code I'm using that was I was thinking about[CODE]
void Player::addToList(Object* object)
{
colListSize++;
colList[colListSize-1] = new Object[colListSize];
colList[colListSize-1] = object;
}
[/CODE]

colList is a Dynamic array of Pointers to class Object. It holds all the data I need for managing collisions. colListSize contains the size of the array. It starts off at 0 and increments every time the function is called. What it's supposed to do is add a pointer to the object to the end of the array. I haven't tested it out yet, so I don't know whether or not it works.
[/quote]This code shows a fundamental lack of understanding in how pointers and memory work in C++. You should really not be trying to invent your own dynamic list until you grok how pointers work.

In that code, you never actually change the size of your original list. For example, if we create the list with 2 elements, then it will always be 2 elements long. When you call [font=courier new,courier,monospace]addToList[/font], it creates a new list that is 3 elements long, then it writes the address of this new list past last slot of your original list (writing to memory you don't own, causing corruption), then it overwrites that value with 'object' (leaking the new list, and again causing corruption)! If you call [font=courier new,courier,monospace]addToList[/font] a 2nd time, then it will write even further past the end of your original array, corrupting more memory.


To illustrate, black hexidecimal numbers are memory addresses, with the value at that memory address shown in the cell to the right. Empty cells are un-owned by your program. 0xcdcdcdcd is used to show uninitialised memory.
[img]http://i.imgur.com/04iGM.png[/img]
[quote name='Captacha' timestamp='1342416461' post='4959455']So you're saying I should use a vector instead of manual memory allocation. vectors gave me a bunch of weird errors in my last project with subscript errors or something. I figured this would be easier. Oh Well.[/quote]You need to learn to walk before you run. Errors stem from misuse, not from inherent problems with vectors. Also, if you misuse vectors like this (writing past the end of the array), then the debug runtimes will likely be able to detect it and tell you that you're corrupting memory, whereas your code will just silently corrupt memory. Edited by Hodgman
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