It cannot allocate enough memory, ok, now how to fix it?????

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Locked
sg57
Posts: 144
Joined: Fri Oct 14, 2005 2:26 pm

It cannot allocate enough memory, ok, now how to fix it?????

Post by sg57 »

Id really really really love to get an answer to this as this is the only thing stopping my world from rendering :( ...

Here is the code pertaining to the error recieved.

Code: Select all

/* Image type - contains height, width, and data */
typedef struct {
    unsigned long sizeX;
    unsigned long sizeY;
    char *data;
} Image;

// Load Texture
Image *textures[5];
int sg;
for&#40;sg=0;sg<5;z++&#41; &#123;
     // allocate space for texture
     textures&#91;sg&#93; = &#40;Image *&#41; malloc&#40;sizeof&#40;Image&#41;&#41;;
     if &#40;textures&#91;sg&#93; == NULL&#41; &#123;
	          printf&#40;"Error allocating space for image"&#41;;
	              exit&#40;0&#41;;
     &#125;
&#125;
Im reciveing the error that he allocation of the memory for hte image failed... (PSPLink printed it if that matters)

Ok, now that i know i cannot allocate the memory... Now what do i do to enable myself to allocate the memory of the Image structure?
User avatar
Jim
Posts: 476
Joined: Sat Jul 02, 2005 10:06 pm
Location: Sydney
Contact:

Post by Jim »

There's nothing wrong with the snippet you posted. I suspect somewhere earlier on you've written over the end of a previous allocation and corrupted the heap.

Jim
sg57
Posts: 144
Joined: Fri Oct 14, 2005 2:26 pm

Post by sg57 »

Danzel said something of the sort. Oh and the textures is an array of 5 integers if that helps...

GLint textures[5];

So your saying somewhere before my malloc call there is something corrupting the stack... Any hints on what could?
bada
Posts: 4
Joined: Thu Apr 27, 2006 9:18 pm

Post by bada »

Code: Select all

for&#40;sg=0;sg<5;z++&#41; &#123; 
     // allocate space for texture 
     textures&#91;sg&#93; = &#40;Image *&#41; malloc&#40;sizeof&#40;Image&#41;&#41;; 
     if &#40;textures&#91;sg&#93; == NULL&#41; &#123; 
             printf&#40;"Error allocating space for image"&#41;; 
                 exit&#40;0&#41;; 
     &#125; 
&#125; 
hmm - look - z++ - so you have infinite loop and he take all memory

IMHO also its better use mamalign(16, sizeof(Image)) instean fo malloc
siberianstar
Posts: 70
Joined: Thu Jun 22, 2006 9:24 pm

Post by siberianstar »

ahah z++! xD
jimparis
Posts: 1145
Joined: Fri Jun 10, 2005 4:21 am
Location: Boston

Post by jimparis »

bada wrote:IMHO also its better use mamalign(16, sizeof(Image)) instean fo malloc
malloc() is already 16-byte aligned. See http://forums.ps2dev.org/viewtopic.php?t=3601
sg57
Posts: 144
Joined: Fri Oct 14, 2005 2:26 pm

Post by sg57 »

Ahhh thanks for that! But that really shouldnt matter will it? Since it doesnt even allocate the first time around? But im changing it.

Also, what should i use then? memset or malloc? W/e you guyz think is best. Danzel says malloc as i have it already.
User avatar
Raphael
Posts: 646
Joined: Tue Jan 17, 2006 4:54 pm
Location: Germany
Contact:

Post by Raphael »

sg57 wrote:Ahhh thanks for that! But that really shouldnt matter will it? Since it doesnt even allocate the first time around? But im changing it.

Also, what should i use then? memset or malloc? W/e you guyz think is best. Danzel says malloc as i have it already.
memset and malloc are completely different, so what's that for an question?

Besides, not your initial malloc fails, but this (endless) loop allocates a new block overwriting the pointer at textures[0] until sometime it runs out of memory and spits out your error string. So that's your luck that you have that check inserted, or else it would just loop forever until you hardreset your psp.
zilt
Posts: 45
Joined: Tue Feb 21, 2006 11:59 pm
Location: Ontario, Canada
Contact:

Post by zilt »

sg57 - you'll learn eventually that you can't just cut & paste code all the time and assume it'll work without even understanding what it does. As Raphael said, memset and malloc perform two entirely different functions and shows a fundamental lack of understanding of those features. After your little stunt on qj, I thought you might have learnt a lesson - but I guess not.
"We are dreamers, shapers, singers, and makers. We study the mysteries of laser and circuit, crystal and scanner, holographic demons and invocations of equations. These are the tools we employ and we know... many things." -- Elric, B5
sg57
Posts: 144
Joined: Fri Oct 14, 2005 2:26 pm

Post by sg57 »

Omg shut up about that. First, I DIDNT know a tutorial or w/e was out for that. I saw it at Cprogramming.com and as a little experiement converted it to work on the PSP. As for the 'from scratch' part, i added a fully transparent wireframe inside the robot and rotated those. Not really verticies and such like skeletal. when i thought it was skeletal animations, I called it my own, but i saw i was wrong, and now its been over with.

Second, its working now.

Third, I was also being told to use memset above if you read, and no im not an expert like some, so ill ask questions on what to do in cases i havent been in before, so be easy.

Fourth, people are assuming I was begging the brain dead blogger for posting that on front page. Raphael definitely has assumed this as he said over there. I never asked, nor want front page at all as its as over rated as LUA apps getting front page. ( "this deserves front page", "front pager right here" , etc. ).

On topic... Thank you for informing me on switching the z to sg (not really as it was an example) but thanks alot, its all working well, just some clipping and z-clipping ( :( ) which ill hide with fog. So thanks!
danzel
Posts: 182
Joined: Fri Nov 04, 2005 11:03 pm

Post by danzel »

Third, I was also being told to use memset above if you read
Just going to defend myself here, I told you on irc that memset does not perform the same function as malloc and that you couldn't use it to replace malloc.
I did however tell you that using malign instead of malloc would not fix the problem. I think you've got these two mixed up.

I did totally miss the z++ error in the loop however, lmao.
User avatar
Raphael
Posts: 646
Joined: Tue Jan 17, 2006 4:54 pm
Location: Germany
Contact:

Post by Raphael »

sg57 wrote:Raphael definitely has assumed this as he said over there.
Dude, you really start to annoy me (not only here). First, because I didn't bring this up here, so refering me is just shoving the insults on you to someone other.

Second:
[email protected] wrote:
[email protected] wrote: Yeah Raphael, thats it, I was kissing the bloggers shoes for front page.
Funny how you turn the words inside my mouth
I never said anything like that. I only said how most people and even bloggers (known to do not-that-well research on their posts) fell for your announcement.
Start really reading posts, instead of just having a glimpse over them and brabbling nonsense.
ooPo
Site Admin
Posts: 2023
Joined: Sat Jan 17, 2004 9:56 am
Location: Canada
Contact:

Post by ooPo »

Drama content has reached dangerous levels!

Locked for posterity. Take it to private messages, please.
Locked