Thread: Realloc does not work as intened...

  1. #1
    Registered User
    Join Date
    Feb 2019
    Posts
    67

    Post Realloc does not work as intened...

    Hey,

    I have got the following code:

    Code:
    /* Adds a move string to a MOVE_DATA * If there is no more space left, space of the MOVE_DATA will be doubled.
     * Returns 1, if successful
     * Returns 0, OutOfMemoryException
     */
    char add_move_to_data(MOVE_DATA* move_info, char* move){
        if(move_info->array_size == move_info->used_array_size){
            printMoves(move_info->moves, move_info->used_array_size, 0);
            printf("\n");
            char* p= realloc(move_info->moves, 2*(move_info->array_size));
            move_info->array_size = 2*(move_info->array_size);
            if(!p){
                return 0;
            } else {
                move_info->moves = (char**)p;
            }
            printMoves(move_info->moves, move_info->used_array_size, 0);
        }
        move_info->moves[move_info->used_array_size++] = move;
        return 1;
    }
    With the strcuture:

    Code:
    typedef struct {    char** moves;
        size_t array_size;
        size_t used_array_size;
    } MOVE_DATA;
    After reallocating the char**, there is only a small amount of the moves printable:

    Before:
    Move: 009,017,000,
    Move: 010,018,000,
    Move: 011,019,000,
    Move: 012,020,000,
    Move: 013,021,000,
    Move: 014,022,000,
    Move: 014,023,000,
    Move: 016,024,000,
    Move: 016,032,000,
    Move: 016,040,000,
    Move: 016,048,000,
    Move: 016,017,000,
    Move: 016,018,000,
    Move: 016,019,000,
    Move: 016,020,000,
    Move: 016,021,000,
    Move: 016,022,000,
    Move: 016,023,000,
    Move: 063,055,000,

    After:

    Move: 009,017,000,
    Move: 010,018,000,
    Move: 011,019,000,
    Move: 012,020,000,
    Move: 013,021,000,

    Why does the realloc behave that way?

    Solution:

    Remember that realloc (just as malloc) requires the size in bytes, not number of "elements".
    Last edited by SuchtyTV; 07-29-2019 at 12:48 PM.

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    628
    What printMoves() suppose to show?

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,662
    Try this.
    Code:
            char** p= realloc(move_info->moves, 2*(move_info->array_size)*sizeof(*p) );
            if(!p){
                return 0;
            } else {
                move_info->moves = p;
                move_info->array_size = 2*(move_info->array_size);
            }
    Yes, as you said, realloc counts bytes not objects.
    So you need the *sizeof thing in there as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    67
    Quote Originally Posted by Salem View Post
    Try this.
    Code:
            char** p= realloc(move_info->moves, 2*(move_info->array_size)*sizeof(*p) );
            if(!p){
                return 0;
            } else {
                move_info->moves = p;
                move_info->array_size = 2*(move_info->array_size);
            }
    Yes, as you said, realloc counts bytes not objects.
    So you need the *sizeof thing in there as well.
    Works fine now! Thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unix: Server does not works as intened
    By SuchtyTV in forum C Programming
    Replies: 1
    Last Post: 07-14-2019, 05:51 AM
  2. Realloc
    By Dr Saucie in forum C Programming
    Replies: 13
    Last Post: 03-19-2010, 11:14 PM
  3. Help on realloc
    By ssharish2005 in forum C Programming
    Replies: 7
    Last Post: 12-08-2007, 03:21 PM
  4. about realloc
    By bartleby84 in forum C Programming
    Replies: 2
    Last Post: 04-20-2007, 05:19 AM
  5. realloc() ????????
    By RoshanX in forum C Programming
    Replies: 3
    Last Post: 10-12-2003, 07:03 PM

Tags for this Thread