C++ question not OGL...

unsigned char *buffer;
buffer = malloc(length);

This creates an error for me…

‘=’ : cannot convert from ‘void *’ to ‘unsigned char *’


malloc just returns binary so it doesn’t have to be unsigned chars. To fix the problem type cast buffer to void*


Now it sais like this

‘=’ : left operand must be l-value

I suggest either of the following…

buffer = (unsigned char*) malloc(length);
buffer = new unsigned char[length];

unsigned char *buffer;

buffer = malloc(640 * 480 * 3);

glReadPixels (0, 0, 640, 480, GL_RGB, GL_UNSIGNED_BYTE, buffer);

This is my code, and neither Ribblem’s or Nobie’s suggestions works…


Nobie’s method should be the right one.

malloc returns a void*, so by casting it to the pointer of your type, it’s ok so

buffer = (unsigned char*)malloc( size )

does the trick.

I don’t know what you did wrong.

I’ve got an other problem:
I have a class (CPolygon)
I allocate memory for my polygons with:
CPolygon *polies = new CPolygon[NumPolies];

This always worked correct… But now i am getting 4 errors, starting with: symbol _DEBUG_NEW is not defined. (Which I don’t use anyways, but i think it’s somewhere in the source code of the ‘new’ operator)

I tried to use malloc instead, but the problem is that that function doesn’t call the construtor of my class, containing more memory allocation routines… weird…

what is the filename? *.c *.cpp

all .cpp

As long as you are not using MFC, you should be able to safely delete the preprocessor code that defines new as DEBUG_NEW.

Thanx now it worked, don’t know what I did wrong last time…


Have you defined the constructor for the class CPoligon? You see the theory says that when you make:
CPoligon poli;
you call your constructor (the constructor you defined in your class).When you call
CPoligon poli[2];
you call the default constructor.
Now when you make:
CPoligon *poli=new CPoligon[2] you JUST RESERVE THE SPACE FOR THE OBJECT (maybe it calls the default constructor also I’m not 1000% sure). After this try to call your constructor and initializxe all the members of your class. This should work. In MFC programming when you try to make an control at the runtime you make something like:
CComboBox *m_combo=new CComboBox;
Hope it helps.