Skip to main content
gamedev.net
🔒 Locked

please explain (2)....

Started by jeff94c Mar 9, 2003 at 9:00 PM 4 replies 550+ views
Original Post
jeff94c
jeff94c
this is part 2...I''m a newbie and trying to get on board and observe the gaming programming practices...thanks in advance 1. What is wrong with this function and why is it a problem? COLOR *VidGetRenderColor( void ) { COLOR *color; color->r = 255; color->g = 255; color->b = 255; color->a = 255; return color; } // to me there''s no args passed in for this type of getmethod()?! 2. Given a world camera position, an "up" vector, and a world look-at position, write brief pseudo-code to build a camera look-at matrix. Assume a right-handed coordinate system. 3. What is wrong with this code? #ifdef NO_ASSERT #define ASSERT(x) /* nothing */ #else #define ASSERT(x) { if ( !(x) ) { printf( "Assertion failed\n" ); exit(1); } } #endif unsigned char *AllocateBytes( unsigned int Count ) { unsigned char *data; ASSERT( data = ( unsigned char * ) malloc( Count ) ); return data; }
kdogg
kdogg
Just looking at number 1. You created a pointer to a COLOR object, but never allocated memory for it. If you''re lucky, it will just segfault, but it might corrupt other data and create a hard to diagnose bug. You''re also returning a pointer to a local variable. That variable will be destroyed when the function returns, and who knows what the pointer will be pointing at. Again, it will probably seg fault.
Sneftel
Sneftel
Ugh. Don''t listen to Yoda; he''s been trolling for weeks. Yoda, if you actually are being a dumbass, rather than just purposefully misleading, remember that malloc returns NULL on an error.

Of course, that code shouldn''t be in an ASSERT if it won''t be compiled in when you aren''t compiling in debug mode.


But... but that''s what HITLER would say!!
CaptainJester
CaptainJester
quote:
Original post by kdogg
Just looking at number 1. You created a pointer to a COLOR object, but never allocated memory for it. If you''re lucky, it will just segfault, but it might corrupt other data and create a hard to diagnose bug. You''re also returning a pointer to a local variable. That variable will be destroyed when the function returns, and who knows what the pointer will be pointing at. Again, it will probably seg fault.


The local variable might be destroyed, but you can still return the address of the memory it is pointing to. So if you allocate memory in the function, you can return it without losing it. But, the caller of the function then has to know to release the memory.



First make it work,
then make it fast.

--Brian Kernighan

"I’m happy to share what I can, because I’m in it for the love of programming. The Ferraris are just gravy, honest!" --John Carmack: Forward to Graphics Programming Black Book
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][
Shannon Barber
Shannon Barber
Smells too much like homework
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
YodaTheCoda
YodaTheCoda
hahaha i foold you dumass i was testiong his skil of C++!
of cuorse i no it does that

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.