View Full Version : Texture loading (without GLaux)
10-11-2000, 08:45 AM
Currently i'm using an ANSI C compiler and get errors trying to assign the return value of the function "malloc" to a variable because it appears to be a 'void'. Are there other ways to allocate memory for loading a texture image? Most of the examples/tutorials iv'e come accross use this method Eg."memory=malloc(size);" for allocating texture image memory.
10-11-2000, 10:06 AM
Right, malloc returns a pointer to a void. So you need to cast that void pointer to something else. E.g.:
unsigned char* mybuffer;
mybuffer=(unsigned char *)malloc(buffersize);
If you are using C++, you can simply use:
unsigned char *mybuffer=new unsigned char[buffersize];
10-11-2000, 10:10 AM
Use C++ ! http://www.opengl.org/discussion_boards/ubb/biggrin.gif
--> operator new
10-11-2000, 11:37 AM
Great, thanks. http://www.opengl.org/discussion_boards/ubb/wink.gif
I've read about the "new" operator but i couldn't figure out what the use of "malloc" was if one could use "new" instead. Would "delete" work as an alternative to "free()"?
[This message has been edited by Muerte (edited 10-11-2000).]
>Would "delete" work as an alternative to "free()"?<
Yes, if you allocate with new instead of malloc, you'll have to use delete instead of free.
There's one caveat : if you allocate an array with new, i.e. x=new char, you'll have to free it using delete, i.e. delete x. Otherwise the memory may not be correctly freed.
Powered by vBulletin® Version 4.2.2 Copyright © 2016 vBulletin Solutions, Inc. All rights reserved.