PDA

View Full Version : C++ question not OGL...



CyBBe
08-12-2000, 09:14 AM
unsigned char *buffer;
buffer = malloc(length);

This creates an error for me...

'=' : cannot convert from 'void *' to 'unsigned char *'

WHAT?

ribblem
08-12-2000, 09:43 AM
malloc just returns binary so it doesn't have to be unsigned chars. To fix the problem type cast buffer to void*

(void*)buffer=malloc(lenghth);

CyBBe
08-12-2000, 09:50 AM
Now it sais like this

'=' : left operand must be l-value

08-12-2000, 10:31 AM
I suggest either of the following...

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

CyBBe
08-12-2000, 11:02 AM
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...

HELP!

Gorg
08-12-2000, 11:26 AM
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.

masterpoi
08-12-2000, 11:40 AM
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...

08-12-2000, 12:08 PM
what is the filename? *.c *.cpp

masterpoi
08-12-2000, 12:30 PM
all .cpp

DFrey
08-12-2000, 12:53 PM
As long as you are not using MFC, you should be able to safely delete the preprocessor code that defines new as DEBUG_NEW.

CyBBe
08-12-2000, 11:16 PM
Thanx now it worked, don't know what I did wrong last time...

thx

NewROmancer
08-13-2000, 09:05 PM
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;
m_combo.Create(...);
Hope it helps.

NewROmancer