Thread: Allocating memory for array of structures

  1. #16
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by HardOverdrive View Post
    Just one last question, my struct is only limited to 30.
    Is there any way to make it infinite?
    When you create with malloc(), you can use realloc() to adjust the size of the array, up to the limits of memory.

  2. #17
    Registered User
    Join Date
    Nov 2011
    Posts
    52
    Quote Originally Posted by HardOverdrive View Post
    Just one last question, my struct is only limited to 30.
    Is there any way to make it infinite?
    You can use malloc again to create a bigger array. Move the old array to the new one then free() the old array. There are lots of memory allocation and manipulation function that you can use like realloc and memcpy to achieve this. Try looking them up and decide how you can use them your program.

    Tater,

    Thanks for the info I will remember that.

  3. #18
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by mnd22 View Post
    You can use malloc again to create a bigger array. Move the old array to the new one then free() the old array. There are lots of memory allocation and manipulation function that you can use like realloc and memcpy to achieve this. Try looking them up and decide how you can use them your program.

    Tater,

    Thanks for the info I will remember that.
    He should just use realloc()... it does not destroy data (unless you make it too small) and doesn't require any silly memcpy() tricks at all.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Having a problem allocating space for an array of structures
    By Jesse20ghet in forum C++ Programming
    Replies: 3
    Last Post: 10-26-2011, 05:02 AM
  2. Help -- allocating memory in a multidimensional array
    By jonathan.plumb in forum C Programming
    Replies: 10
    Last Post: 05-20-2009, 11:04 PM
  3. Replies: 60
    Last Post: 01-09-2009, 01:09 PM
  4. Allocating memory of VL structures
    By darsatdusk in forum C Programming
    Replies: 8
    Last Post: 07-09-2008, 03:43 PM
  5. allocating memory for char* array
    By creeping_death in forum C++ Programming
    Replies: 2
    Last Post: 06-17-2003, 04:49 AM