Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C++ HALP

Name: Seven 2007-05-11 23:39 ID:Yp9UrS/m

Okey, im a memory noob when it comes to C++, i dunt really understand pointers and shallow-deep copy shit, and its causing me a problem.

Pointer to an image object, is passed to a class which makes some changes and uses it every step/frame. Works fine when theres only one instance of the object, but when i try to create another, my game crashes! Is passing a pointer to multiple instances bad??

Name: Anonymous 2007-05-11 23:46 ID:zts986Uq

It is if one of them destroys the object and the other tries to use free()'d memory.

Name: Anonymous 2007-05-11 23:47 ID:dz+aWhlf

Look into Singleton objects.

Name: Anonymous 2007-05-11 23:48 ID:b95LStoo

I'm not going to try to explain pointers in this tiny box.

Shallow copy = Just copy the object, leaving it as is; use this if the object doesn't change the state of anything else it has pointers to
Deep copy = Copy the object and also deep copy anything else it references, and then change the object to point to the new copies; use this if it does change the state of something it has pointers to, so you don't have multiple objects treating said things as though they have exclusive access to them.

As to your problem, if you mean that you're passing a single instance of an image object to several other objects, that might be a problem. If object A changes the image object to state foo and doesn't expect it to change while it's not looking, and then object B changes it to state bar, the next time object A does something with it and finds it in an unexpected state, exploding programs may result.

Name: Anonymous 2007-05-11 23:53 ID:jlF1C1ns

Think about it. Same pointer passed to, say, two instances of some class. Both of those classes now point at the same space in memory. Any changes in one will be seen by the other.

In particular, if you free the memory in one instance (using free() or delete or whatever), the other instance(s) will attempt to free it again. That'll crash.

Grab your resources in the constructor, release in destructor. http://en.wikipedia.org/wiki/RAII

Name: Seven 2007-05-11 23:58 ID:Yp9UrS/m

>>2
WOO YOU FIXED IT! :D:D:D
It wasnt acctually the image object, i had an array of gridNode's that i was accidentally freeing when the enemy object was deleted, then i converted the enemy class to all use the same one, i forgot to remove the 'delete' line, so i removed that, and it woiks!! woo!

This is the first acctual help that ive ever gotten from 4chan.

Name: Anonymous 2007-05-11 23:59 ID:zts986Uq

Shit. Next time, I'm just going to sage you.

Name: Anonymous 2007-05-12 0:49 ID:qDmAthph

>>1
Shallow Copy: If there is no copy constructor, compilers generate code that only copies the actual bits of an object to the new object. This is the default action if you are not wise enough to create your own copy constructor.

Deep Copy: You have a copy constructor and it replicates members of dynamic built-in types and/or calls copy constructors of members of user defined classes and references the copies.

On a side note: if you don't understand pointers, you should stick to flipping... err, Java. 'Nuff said.

Name: Anonymous 2007-05-12 17:27 ID:7nydRjyl

>>8
Yeah i had a copy constructor, and java isnt available for PSP yet ;)

Name: Anonymous 2007-05-17 5:50 ID:Heaven

<code>test</code>

Name: Anonymous 2007-05-17 5:51 ID:Heaven

test

Name: Anonymous 2009-01-14 14:36

Optimize THIS

Name: ​​​​​​​​​​ 2010-10-22 18:52

Name: Anonymous 2010-11-14 19:24

Name: Anonymous 2010-12-17 1:27

Xarn is a bad boyfriend

Name: Anonymous 2010-12-23 18:13

Name: Anonymous 2011-02-04 18:17

Name: Sgt.Kabu櫽覢kiman叐㿜 2012-05-29 1:38

Bringing /prog/ back to its people
All work and no play makes Jack a dull boy

Name: Anonymous 2013-08-31 20:52


Those are round during combat. When you're in combat things slow down hugely and the action is tracked in detail at the resolution of rounds. You don't use combat rounds for just wandering around, looking at things, and talking to people.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List