"new" operator not allocating enough memory?

Mark Mussetter mark at link-comm.com
Fri Feb 20 21:31:54 UTC 2004


Hello Everyone,

I seem to be having trouble with operator "new" not allocating enough 
memory for FLTK objects.  Displaying the sizeof the objects in the debugger 
returns the correct size, but printing the sizeof the objects from within 
the program gives the wrong result (it matches the amount of space that 
"new" allocates).  We are using the V4.6pre5 m68k-rtems-gcc compiler.  Is 
there something I could be doing wrong that would cause the compiler to 
calculate the sizeof the object incorrectly?

I am trying to run the HelloWorld.cc example program from FLTK1.1.4 on our 
target.  Our target is a Motorola 5272 running RTEMS and Microwindows-0.90 
(both of which work fine without FLTK).  The program creates two 
objects:  a Fl_Window and a Fl_Box.  The problem seems to be that the "new" 
operator isn't allocating enough memory for the Fl_Box object, so when it 
gets initialized, it overwrites part of the Fl_Window data (the vptr is one 
victim, which obviously causes problems later).  If I comment out the 
Fl_Box code and just display a window, everything works (though some other 
memory might be getting overwritten that doesn't have an obvious effect).

I loaded the app in DDD and stepped into the "new" operation and saw that 
40 bytes were allocated for the box, but when I checked 
"sizeof(*box)"  from the (gdb) prompt, I get 66 bytes as a result (66 is 
the correct size, based on manually adding up the size of the data in an 
Fl_Box).  Just to add another sanity check, I added printf statements to 
the code to see how "new" was allocating storage for the two objects and 
got the following results, which seem to agree with the amount of space 
"new" is allocating:

Sizeof(Fl_Window) = (0x5C) 92 bytes     (this should be 126 per the 
debugger and a manual count)
Sizeof(*window) = (0x5C) 92 bytes
Sizeof(Fl_Box) = (0x28) 40 bytes          (this should be 66 per the 
debugger and a manual count)
Sizeof(*box) = (0x28) 40 bytes
Address of *window = (0x57D3B8) 5755832
Address of *box = (0x57D384) 5755780

The code is small, so I'm attaching it below.

Any help would be greatly appreciated!


Mark Mussetter
Link Communications, Inc.
email: <mailto:mark at link-comm.com>mark at link-comm.com
phone: (406) 245-5002 ext 105
Fax:    (406) 245-4889


#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Box.H>
#include <stdio.h>

extern "C" void startupLCD(void);
extern "C" void touchScreenInit(void);
extern "C" int rtems_main(int argc, char **argv);

int rtems_main(int argc, char **argv) {
         Fl_Window *window = new Fl_Window(200,180);
         Fl_Box *box = new Fl_Box(20,40,170,100,"Hello, World!");

         printf( "Sizeof(Fl_Window) = (0x%X) %d bytes\n", 
sizeof(Fl_Window), sizeof(Fl_Window) );
         printf( "Sizeof(*window) = (0x%X) %d bytes\n", sizeof(*window), 
sizeof(*window) );
         printf( "Sizeof(Fl_Box) = (0x%X) %d bytes\n", sizeof(Fl_Box), 
sizeof(Fl_Box) );
         printf( "Sizeof(*box) = (0x%X) %d bytes\n", sizeof(*box), 
sizeof(*box) );
         printf( "Address of *window = (0x%X) %d\n", window,window );
         printf( "Address of *box = (0x%X) %d\n", box,box );

         startupLCD();
         touchScreenInit();

         box->box(FL_UP_BOX);
         box->labelsize(36);
         box->labelfont(FL_BOLD+FL_ITALIC);
         //box->labeltype(FL_SHADOW_LABEL);
         window->end();
         window->show(argc, argv);
         return Fl::run();
}





More information about the users mailing list