Crashes operator new C++

kurlyak 101 Feb 25, 2012 at 14:20

Hi folk!
i had encountered with a little problem which are confused me… I show my code at C++ and VS 2010. I have same code at pure C and it is works. My code don`t work. Why?

Why code line Level->Rooms = new tr2_room[Level->NumRooms]; crashes.

typedef signed   char     bit8; // 8-bit,  signed
typedef signed   short int bit16;   // 16-bit, signed
typedef signed   long int  bit32;   // 32-bit, signed
typedef unsigned char     bitu8;   // 8-bit,  unsigned
typedef unsigned short int bitu16;  // 16-bit, unsigned
typedef unsigned long int  bitu32;  // 32-bit, unsigned

struct tr2_room {

   tr2_room_info info;          // where the room exists, in world coordinates
   bitu32 NumDataWords;                      // number of data words (bitu16)
   bitu8  *Data;                                // the raw data from which the rest of this is derived
   tr2_room_data RoomData;      // the room mesh
   bitu16 NumPortals;                          // number of visibility portals that leave this room
   tr2_room_portal *Portals;      // list of visibility portals
   bitu16 NumZsectors;                        // width of sector list
   bitu16 NumXsectors;                        // height of sector list
   tr2_room_sector *SectorList;   // list of sectors in this room
   bit16  Intensity1,
   bitu16 NumLights;                            // number of lights in this room
   tr2_room_light *Lights;      // list of lights
   bitu16 NumStaticMeshes;                    // number of static meshes
   tr2_room_staticmesh *StaticMeshes;          // static meshes
   bit16  AlternateRoom;
   bit16  Flags;                                // 0x0001 - room is filled with water
                                                // 0x0020 - Lara's ponytail gets blown by the wind

struct tr2_level {

   char   *FileName;                            // filename (not in .TR2 file)
   bitu32 Version;                            // .TR2 file version
   TR2_Version_type EngineVersion;            // TombRaider_1, TombRaider_2, TombRaider_3
   tr2_colour Palette8[256];      // 8-bit palette
   bitu32 Palette16[256];                      // 16-bit palette
   bitu32 NumTextiles;                        // number of textiles
   tr2_textile8 *Textile8;      // 8-bit (palettised) textiles
   tr2_textile16 *Textile16;      // 16-bit (ARGB) textiles
   bitu32 UnknownT;                          // 32-bit unknown (always 0 in real TR2 levels)
   bitu16 NumRooms;                          // number of rooms in this level
   tr2_room *Rooms;            // list of rooms

tr2_level *Level;
Level = new tr2_level;
ZeroMemory(Level, sizeof(tr2_level));
//read number of rooms of game level
TR2fread(&Level->NumRooms, sizeof(Level->NumRooms), 1, fp);
//next line crashes with bad_alloc memory
Level->Rooms = new tr2_room[Level->NumRooms];
ZeroMemory(Level->Rooms, Level->NumRooms * sizeof(tr2_room));

Thanks in advance!

3 Replies

Please log in or register to post a reply.

v71 105 Feb 25, 2012 at 15:25

Normally these things happens for the reason that you are trying to get access past the dimension of the array or memory buffer.
I’d check that if i were you.
Look at TR2fread function, you are usinng sizeof(Level->NumRooms) , are you sure you want to read the number of rooms or the memory size ?
in this case you should multiply the number of rooms by the size in byte of the structure.

kurlyak 101 Feb 25, 2012 at 16:43

Thank you for you reply!
At first I am trying read number of Room in my level.

TR2fread(&Level->NumRooms, sizeof(Level->NumRooms), 1, fp);

Next I am using NumRooms for allocate memory:

Level->Rooms = new tr2_room[Level->NumRooms];

I had check NumRooms = 38 (via Debugger). This is right value (i had saw source code of level = 38 room).

This is my func read from file:

static int TR2fread(void *buffer, size_t size, size_t count, FILE *fp)
   int NumRead;
   NumRead  =(int) fread(buffer, size, count, fp);
   if ((size_t)NumRead != count) {
   char szBuff[256];
   sprintf_s(szBuff, 256, "fread(buffer, %d, %d, fp) returned %d\n", size, count, NumRead);
      return 0;

Reading value NumRooms is right, because I had check this value, and as I say it = 38 room. But the memory allocation… failure.

By the words, I am trying do simply for check:

Level->Rooms = new tr2_room[38];

And the same error allocation memory!

Thank you!

kurlyak 101 Feb 25, 2012 at 19:55

I’m sorry that something did not finish in the source code!
I fix this problem and now it works!