Malloc and New

The Partridge Family were neither partridges nor a family. Discuss.
User avatar
Asimov
Posts: 814
Joined: May 19th, 2012, 11:38 pm

Re: Malloc and New

Post by Asimov » June 22nd, 2012, 1:23 pm

Hi Lux,

I might do that.

I suppose a transparent overlay like that could be used to put clouds in different parts of a game like nebuli in a space game.

Asimov
Attachments
PerfRes.jpg
(286.2 KiB) Downloaded 80 times
----> Asimov
"You know no matter how much I think I have learnt. I always end up hitting brick walls"
http://www.asimoventerprises.co.uk

User avatar
Asimov
Posts: 814
Joined: May 19th, 2012, 11:38 pm

Re: Malloc and New

Post by Asimov » June 23rd, 2012, 11:19 am

Hi Chilli,

I was researching using new instead of Malloc LOL, and failed big time.

Thought I could initialize my class with
LoadSpr png = new LoadSpr();

and it didn't quite work.

Well failure is part of learning heh heh.

Asimov
----> Asimov
"You know no matter how much I think I have learnt. I always end up hitting brick walls"
http://www.asimoventerprises.co.uk

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Malloc and New

Post by chili » June 23rd, 2012, 11:21 am

Just like malloc, you need to use a POINTER Asimov. :lol:

i.e. LoadSpr* png = new LoadSpr;
Chili

User avatar
Asimov
Posts: 814
Joined: May 19th, 2012, 11:38 pm

Re: Malloc and New

Post by Asimov » June 23rd, 2012, 11:36 am

Hi Chilli,

I tried that and I get a red squiggle under the equals sign.
Think I will wait until the new tutorials LOL. I probably have to add something to the class too to make it work anyway.

Got a ton more models to build and my brother is coming to visit today anyway heh heh.

Asimov
----> Asimov
"You know no matter how much I think I have learnt. I always end up hitting brick walls"
http://www.asimoventerprises.co.uk

User avatar
LuX
Posts: 1492
Joined: April 22nd, 2012, 12:33 pm
Location: Finland

Re: Malloc and New

Post by LuX » June 23rd, 2012, 12:15 pm

Look at your results above, your computer should be running just fine.

I haven't been doing much coding right now, but I'll see if I get my game done in the couple next days, so you can have a look how I did it in my game and maybe get it working for you.

I made some research on malloc, it seems malloc shouldn't be any slower, however there is a case where it might slow down. Malloc tries to find a memory block equal or larger to its size, but if such can't be found, it will look for multiple smaller blocks and use those. In that case malloc will slow down, the more it has to jump around in memory.

http://mikehearn.wordpress.com/2006/10/ ... tion-slow/

Not sure how relevant that is in the current version of c++
ʕ •ᴥ•ʔ

User avatar
Asimov
Posts: 814
Joined: May 19th, 2012, 11:38 pm

Re: Malloc and New

Post by Asimov » July 6th, 2012, 1:05 pm

Hi Chilli,

Hey just replaced malloc with new in my sprite loader and it worked woohoo

Code: Select all

void SpriteLoader::LoadSprite(const wchar_t* filename, int sWidth,int sHeight,int nFrames,int tWidth )
{
	//surface = (D3DCOLOR*)malloc( sizeof( D3DCOLOR ) * tWidth * sHeight );
	surface = new D3DCOLOR[ tWidth * sHeight ]; //Changed here
	LoadPNG(filename);
	SpriteHeight=sHeight;
	SpriteWidth=sWidth;
	Frame=0;
	Time=0;
	NumberOfFrames=nFrames;
}

Now in my game deconstructor where I call freesprite for malloc would I change it from this

Code: Select all

void SpriteLoader::FreeSprite()
{
	free (surface);
}
to this

Code: Select all

void SpriteLoader::FreeSprite()
{
	delete [] surface;
}
Asimov
----> Asimov
"You know no matter how much I think I have learnt. I always end up hitting brick walls"
http://www.asimoventerprises.co.uk

Post Reply