c++ class limit size


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: c++ class limit size

Hybrid View

  1. #1
    Join Date
    Oct 2009
    Posts
    3

    c++ class limit size

    hi,
    i have a very singular problem:

    i can't get my class 'A' with 48 bytes size. 44 bytes ok, 52 bytes ok. But if it takes 48 bytes then my program crashs. It dont send any error or advice.

    any idea why?

    I'm using SDL library on Linux (Ubuntu).

    ps.: Sorry my weak english.

  2. #2
    Join Date
    Dec 2003
    Posts
    3,366
    This isnt a C++ problem. It could be alignment issues, unix OS like to align data in certain ways and you can break things around this issue, if careless with a pointer or something, but the bottom line is its not your classes size thats the problem, its very likely a bug that only shows up when the class is a certain size.

  3. #3
    Join Date
    Oct 2009
    Posts
    3

    thanks

    thanks for helping.

    where can i get some reference about this issue?

    again, thanks a lot.

  4. #4
    Join Date
    May 2007
    Posts
    843
    Please post your code here.

  5. #5
    Join Date
    Oct 2009
    Posts
    3

    Code

    #ifndef _BASEGAME_
    #define _BASEGAME_

    #include "SDL.h"
    #include "keyBoard.h"
    #include <string>

    using std::string;

    class BaseGame{

    public:
    static int key[nKeys];

    BaseGame(string title, int screenWidth, int screenHeight);
    virtual ~BaseGame();

    void start();

    protected:
    bool quitGame;
    const int screenWidth;
    const int screenHeight;
    string title;
    SDL_Surface* screen;

    private:
    void HandleEvent(const SDL_Event& event);
    void init();
    virtual void render() = 0;
    virtual void logicLoop() = 0;
    virtual void eventsTreat() = 0;

    BaseGame(const BaseGame&);
    BaseGame& operator=(const BaseGame&);
    };
    #endif
    #ifndef _GAME_
    #define _GAME_

    #include "BaseGame.h"

    class Level;
    class LevelManager;
    class Border;
    class DrawBall;
    class DrawBandeja;
    class Points;

    class Game: public BaseGame{
    public:
    Game();
    virtual ~Game();
    private:
    virtual void logicLoop();
    virtual void render();
    virtual void eventsTreat();
    virtual void setReferencesUp();

    LevelManager* levelManeger;
    Level* currentLevel;
    DrawBall* ball;
    DrawBandeja* bandeja;
    Points* points;
    Border* border;

    private:
    Game(const Game&);
    Game& operator=(const Game&);
    };
    #endif
    well, in Game class, if it is exactly 48 bytes then it appears a window and then exits with no error, but without do what i want. Otherwise, if it's not then it works like i want.

    my makefile:

    CC = g++
    CFLAGS = -Wall `sdl-config --cflags --libs`
    PROG = Game
    PRE = rm -f $(PROG)

    #-------------------------------------------------------------------------------
    #BLOCK
    PATH_BLOCK = Block/
    IBLOCK = -I./$(PATH_BLOCK)
    BLOCK = $(wildcard $(PATH_BLOCK)*.cpp)

    #COMMONBLOCK
    PATH_COMMON_BLOCK = $(PATH_BLOCK)CommonBlock/
    ICOMMON_BLOCK = -I./$(PATH_COMMON_BLOCK)
    COMMON_BLOCK = $(wildcard $(PATH_COMMON_BLOCK)*.cpp)

    #ITEMBLOCK
    PATH_ITEM_BLOCK = $(PATH_BLOCK)ItemBlock/
    IITEM_BLOCK = -I./$(PATH_ITEM_BLOCK)
    ITEM_BLOCK = $(wildcard $(PATH_ITEM_BLOCK)*.cpp)

    #TODOS BLOCKS
    BLOCKS = $(BLOCK) $(COMMON_BLOCK) $(ITEM_BLOCK)
    IBLOCKS = $(IBLOCK) $(ICOMMON_BLOCK) $(IITEM_BLOCK)

    #-------------------------------------------------------------------------------
    #BALL
    PATH_BALL = Ball/
    IBALL = -I./$(PATH_BALL)
    BALL = $(wildcard $(PATH_BALL)*.cpp)

    #-------------------------------------------------------------------------------
    #ITEM
    PATH_ITEM = Item/
    IITEM = -I./$(PATH_ITEM)
    ITEM = $(wildcard $(PATH_ITEM)*.cpp)

    #-------------------------------------------------------------------------------
    #BANDEJA
    PATH_BANDEJA = Bandeja/
    IBANDEJA = -I./$(PATH_BANDEJA)
    BANDEJA = $(wildcard $(PATH_BANDEJA)*.cpp)

    #-------------------------------------------------------------------------------
    #LEVELS E MAPAS
    PATH_LEVEL = Levels/
    ILEVEL = -I./$(PATH_LEVEL)
    LEVEL = $(wildcard $(PATH_LEVEL)*.cpp)

    PATH_MAPS = $(PATH_LEVEL)Maps/
    IMAPS = -I./$(PATH_MAPS)
    MAPS = $(wildcard $(PATH_MAPS)*.cpp)

    #-------------------------------------------------------------------------------

    #Todos sources
    SRCS = $(wildcard *.cpp) $(BLOCKS) $(BALL) $(ITEM) $(BANDEJA) $(LEVEL) $(MAPS)
    INCLUDE = -I. $(IBLOCKS) $(IBALL) $(IITEM) $(IBANDEJA) $(ILEVEL) $(IMAPS)

    #-------------------------------------------------------------------------------
    #REGRAS

    all: $(PROG)

    $(PROG): $(SRCS)
    $(PRE) && $(CC) $(INCLUDE) $(CFLAGS) -o $(PROG) $(SRCS)

    clean:
    rm -f $(PROG)

  6. #6
    Join Date
    Dec 2003
    Posts
    3,366
    I dont see anything there, so its time to debug it. If you dont already know what to do, and it sounds like you do not, try this:

    1) get it back to error condition. Make the class 48 bytes and get it back into the state where the error happens.

    2) Set breakpoints in your IDE and try to find a place in the code where the program does something you did not expect.

    3) Narrow down the problem. Keep an open mind, but its very likely you have either totally messed up a pointer activity or you have somehow violated the alignment rules of your compiler and OS. I do not know the rules, but in a general sense unix does something like padding (extra bytes that are zeros) to align all the data to the machine's or compiler's or operating system's "word size". Word size used to mean the size of a register on the CPU, for example older intel computers had 16 bit registers, later those were made to be 32, and now they are 64 bits long. However, while 64 may be the CPU and "true" word size, the compiler or OS may think the size is 32. The bottom line is even on a 64 bit system it may be aligned to 32 bits, or 16 bits, or 48 bits, or some other awful thing. And this is controllable by compiler flags too, so you can change this alignment! Anyway, the most common problem with alignment is using a pointer poorly, so you are right back to looking for pointer problems....

  7. #7
    Join Date
    Nov 2003
    Posts
    4,118
    This has nothing to do with the class's size. Most likely, you program has some form of undefined behavior to to pointer allocation/deallocation bugs, which means that its behavior is erratic and predictable. Don't bother modifying the class' size; look for a pointer related bug in your code.
    Danny Kalev

Similar Threads

  1. Problem with inner class
    By IronJack in forum Java
    Replies: 12
    Last Post: 08-08-2006, 07:52 PM
  2. Objects disposed incorrectly
    By Osiris43 in forum .NET
    Replies: 1
    Last Post: 08-04-2006, 01:15 PM
  3. Help with class/applet
    By none_none in forum Java
    Replies: 17
    Last Post: 04-28-2005, 04:00 PM
  4. Replies: 205
    Last Post: 09-26-2001, 02:37 PM
  5. Abstract class
    By Manika in forum Java
    Replies: 1
    Last Post: 09-06-2000, 02:13 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center