Any way to dynamically change an array size in C?

This is a discussion on Any way to dynamically change an array size in C? within the C Programming forums, part of the General Programming Boards category; Hello Is there any way to dynamically change the size of an array? For example - Code: unsigned char result[] ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    18

    Any way to dynamically change an array size in C?

    Hello

    Is there any way to dynamically change the size of an array?

    For example -

    Code:
    unsigned char result[] = {0x01, 0x01}
    
    // do stuff with result
    
    // now I need to resize result so it can store three bytes, e.g. {0x01, 0x01, 0x01}
    Can this be done?

    Thanks!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    No. (You may want to look into the exciting world of malloc() and free().)

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    What tabstop said. The idea is to declare a pointer instead of an array and alter the block of memory it points to as you need it.

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    18
    Thank you for the replies.

    Do you mean this kind of thing?

    Code:
    unsigned char *result;
    
    // I want my array to store two bytes
    result = (unsigned char *) malloc(2);
    
    // Assign values to both array elements
    *(result+0) = 0x01;
    *(result+1) = 0xFF;
    
    free(result);
    
    // Now I want my array to store three bytes
    result = (unsigned char *) malloc(3);
    
    // Assign values to the three array elements
    *(result+0) = 0x01;
    *(result+1) = 0xFF;
    *(result+1) = 0xCA;
    
    free(result);
    Thanks again.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Right. If you're often going to keep the original parts the same, like you do here, then you can look into realloc().

    And also, you are free to use array notation on pointers too: result[2] instead of *(result+2).

  6. #6
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    The key here is realloc().

    Edit: Beaten by tabstop.

  7. #7
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    best to use sizeof() for malloc.

    [code]

    result = malloc(2 * sizeof(char));

    /*assign */

    result = realloc(result, 3 * sizeof(char)); /*can be intinsive for large memory blocks */

    /*then call free()*/

    [code/]

  8. #8
    Registered User
    Join Date
    Mar 2008
    Posts
    18
    Thanks everyone.

    slingerland3g:

    When I execute the following:

    Code:
    	unsigned char *result;
    
    	result = malloc(2 * sizeof(unsigned char));
    
    	printf("size of result: %d\n", sizeof *result);
    I get the following:

    size of result: 1

    Surely this should be 2?

  9. #9
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Nope. result is a pointer to a char, thus the size of what result points to will be 1. This is one of those differences between arrays and pointers.

    Since you malloc()ed the block of memory, though, you should already know its size.

  10. #10
    Registered User
    Join Date
    Mar 2008
    Posts
    18
    So it's size will always be 1 (according to sizeof), but I can still use result to store 2 unsigned chars?

  11. #11
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    If it points to a block of memory that enables you to do that, yes. Check the return value of malloc() before you start trying to use it.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Yes. One character (which is what *result is) is always one. sizeof does not report the entire memory block, just what you hand it.

  13. #13
    Registered User
    Join Date
    Mar 2008
    Posts
    18
    Thanks everyone.

  14. #14
    Registered User
    Join Date
    Apr 2006
    Posts
    2,009
    Quote Originally Posted by slingerland3g View Post
    best to use sizeof() for malloc.

    [code]

    result = malloc(2 * sizeof(char));

    /*assign */

    result = realloc(result, 3 * sizeof(char)); /*can be intinsive for large memory blocks */

    /*then call free()*/

    [code/]
    While a good policy in general, there is never any reason to call sizeof(char), since that is always, always 1.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  15. #15
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    Good point King Mir, I was just being explicit and showing clear intent within the code.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 02:55 AM
  2. Invalid conversion from 'void*' to 'BYTE' help
    By bikr692002 in forum C++ Programming
    Replies: 9
    Last Post: 02-22-2006, 10:27 AM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. Changing a Structures Members array size
    By Xei in forum C++ Programming
    Replies: 1
    Last Post: 11-07-2002, 06:45 PM
  5. Dynamically allocate size of array for strings
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 05-04-2002, 05:06 PM

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