Dynamic arrays

This is a discussion on Dynamic arrays within the C++ Programming forums, part of the General Programming Boards category; Im having a little problem with dynamic arrays.. I have a struct: Code: typedef struct _SFont { ID3DXFont *fontobj; LOGFONT ...

  1. #1
    Unregistered
    Guest

    Dynamic arrays

    Im having a little problem with dynamic arrays..
    I have a struct:

    Code:
    typedef struct _SFont
    {
    ID3DXFont  *fontobj;
    LOGFONT     fontparams;
    char        *name;
    }SFont;
    i declare a pointer to that struct:
    Code:
    SFont *rFont;
    My class constructor:
    Code:
    ObjectBuilder::ObjectBuilder()
    {
       rFont = (SFont*)malloc(sizeof *rFont);
       fontnum = 0;   //fontnum is a counter
    }
    I have a function that increases the fontnum and must resize the array:
    Code:
    void ObjectBuilder::CreateFont(char *Name, int size)
    {
       fontnum += 1;
       //allocate more space
    rFont = (SFont*)realloc(rFont, fontnum * sizeof *rFont);
    //So, now the array should have fontnum-1 slots, right?
    rFont[fontnum].Name = Name;
    There is some code to create a font, but thats all we need. The problem is that I can only call 1 time this function, meaning that I can only add 1 more font. If i call it again, it crashes! I free the array using:
    Code:
    free (rFont);
    rFont=NULL;
    But when I exit the program, having created 1 font, it crashes for once more in the debug and shows me some asm code. Thank you very much!

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    >void ObjectBuilder::CreateFont(char *Name, int size)
    >{
    >   fontnum += 1;
    >   //allocate more space
    >rFont = (SFont*)realloc(rFont, fontnum * sizeof *rFont);
    >//So, now the array should have fontnum-1 slots, right?
    >rFont[fontnum].Name = Name;
    
    Change this to:
    rFont[fontnum-1].Name = Name;

  3. #3
    Unregistered
    Guest
    Ive done it, and it doesn return the correct name.. also it crashes

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Since arrays start at 0, if fontnum is 1, this only gives you one slot:
    rFont[0]
    When you say it doesn't return the correct name, which slot are you reading from, 0 or 1?

    If you actually want a rFont[1] when fontnum is 1, then you would need to change this:

    >rFont = (SFont*)realloc(rFont, fontnum * sizeof *rFont);

    to:

    rFont = (SFont*)realloc(rFont, (fontnum+1) * sizeof *rFont);

    Then just use your original code:

    rFont[fontnum].Name = Name;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating and freeing dynamic arrays
    By circuitbreaker in forum C++ Programming
    Replies: 8
    Last Post: 02-18-2008, 10:18 AM
  2. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  3. processing dynamic arrays
    By Mario F. in forum C++ Programming
    Replies: 9
    Last Post: 06-04-2006, 11:32 AM
  4. Dynamic (Numeric) Arrays
    By DavidB in forum C++ Programming
    Replies: 5
    Last Post: 05-03-2006, 07:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21