dynamic object growth

This is a discussion on dynamic object growth within the C++ Programming forums, part of the General Programming Boards category; im making a class that will load and store all my textures. when the capacity is reached and you try ...

  1. #1
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640

    dynamic object growth

    im making a class that will load and store all my textures. when the capacity is reached and you try to load/add a new texture, it should 'expand' the capacity

    the code i've written works but when i quit the program i get a an error that pops up and says "Debug Error DAMAGE: after normal block ....." i know it is this function causing it but i dont know why, any ideas?

    (MSVC++ 6)

    Code:
    void TexLoader::grow()
    {
    	int cap = capacity*1.8; // calculate new capacity
    	Texture* temp;			// to hold temp data
    
    	temp = textures;			 // make temp point to current textures
    	textures = new Texture[cap]; // allocate new space for textures
    
    	for(int j =0; j < index; j++) // copy textures to new array
    	{
    		textures[j].ID = temp[j].ID; 
    		textures[j].tex = temp[j].tex;
    	}
    
    	capacity = cap;				// update capacity
    	delete temp;				// delete old/temp objects
    }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>for(int j =0; j < index; j++)
    Is index the correct value?

    >>delete temp;
    How many items is that deleting. Do you need to use []
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136

    Re: dynamic object growth

    Originally posted by Perspective
    im making a class that will load and store all my textures. when the capacity is reached and you try to load/add a new texture, it should 'expand' the capacity
    *cough*vector*cough*

  4. #4
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    >*cough*vector*cough*

    lol, yeah, i know. but i like getting the experience of making my own things rather than always using STL items. if i were going to use one it would be more like...

    *cough*map<string, texture>*cough*refernce them easily by name now*

    >>for(int j =0; j < index; j++)
    >Is index the correct value?

    yes, index gets set to 0 in the constructor and is increased after each texture is added. once 'index >= capacity' i call the grow function.

    >>delete temp;
    >How many items is that deleting. Do you need to use []

    doh! thats right. but it didnt fix the problem.
    i can post the rest of the class if you want to see it but i know the problem is with this function ( when i allocate enough room to begin with, so the function doesnt get called, i dont get the error )


    [edit] hmmmm..... weird. i only get the error when i start with space for 1 texture.

    ex) i start with room for 1 texture, add 4 textures, get error... but
    if i start with room for 2 textures, add 4 textures, everything works fine.
    ?
    Last edited by Perspective; 03-13-2003 at 07:55 PM.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>int cap = capacity*1.8;
    Why *1.8, why not a whole number, like 2. Maybe the precision is causing a screw-up.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Originally posted by Hammer
    >>int cap = capacity*1.8;
    Why *1.8, why not a whole number, like 2. Maybe the precision is causing a screw-up.
    2 just seemed a little big. not that 1.8 is much smaller. it was pretty much an arbitrary pick for testing.

    ...... omg ..... *reason hits me like a ton of bricks*

    your right.
    (as stated in my edit above ->) everything works fine as long as the initial capacity is > 1. duh, thats because 1*1.8 = 1 (integer mult.) and the capacity will never be able to expand if i start with 1. i cant believe i didnt pick up on that.
    thanx for your help Hammer!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GDI object lifetime and C++ object lifetime
    By VirtualAce in forum Windows Programming
    Replies: 4
    Last Post: 06-16-2006, 05:26 AM
  2. Set Classes
    By Nicknameguy in forum C++ Programming
    Replies: 13
    Last Post: 10-31-2002, 01:56 PM
  3. Replies: 6
    Last Post: 10-30-2002, 08:03 PM
  4. Set Classes
    By Nicknameguy in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2002, 07:40 PM
  5. dynamic object initialisation??
    By splendid bob in forum C++ Programming
    Replies: 2
    Last Post: 07-04-2002, 12:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21