Dynamic Array Size

This is a discussion on Dynamic Array Size within the C Programming forums, part of the General Programming Boards category; This is my first post so im unsure if this has been asked before. Im trying to write a program ...

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    1

    Dynamic Array Size

    This is my first post so im unsure if this has been asked before.

    Im trying to write a program that will update the size of an array while running. Say a count is given that will get larger and larger as information is entered, how do I reflect that in the size of the array, without putting in some rediculous number.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Press the little search button and search for dynamic array.

    To summarize all of the links you'll find, you dynamicly allocate memory using malloc or calloc . Be sure to free it when you're done.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    and you'll need memcpy() to maintain the data in the array as well as memset() to initialize the array.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >as well as memset() to initialize the array
    Unless we're talking about an array of anything but integral values. Then you can't be sure that all bits zero represents the equivalent of 0 for that type.
    My best code is written with the delete key.

  5. #5
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    fair enough prelude. picky picky

    In C++ we would let the constructor do the work. In C I suppose you'll have some sort of Init function that does the work. memset is just the usual way of setting everything to zero when zero is an acceptable initialization.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >picky picky
    That's what I'm known for.
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Jun 2003
    Posts
    14
    And you could use realloc to give it a chance to run faster.
    The specified object is a mutex object that was not released by the thread that owned the mutex object before the owning thread terminated. Ownership of the mutex object is granted to the calling thread, and the mutex is set to nonsignaled.
    Microsoft, Win32 API documentation
    You don't say? I'm owned.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >And you could use realloc to give it a chance to run faster.
    This should be good. And why would using realloc give it a change of running faster?
    My best code is written with the delete key.

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Prelude
    >as well as memset() to initialize the array
    Unless we're talking about an array of anything but integral values. Then you can't be sure that all bits zero represents the equivalent of 0 for that type.
    In the scenario given, its highly unlikely the array even needs initialising anyway. It would appear that data is going straight into it. You only need to initialise if the array contents is going to be read before real data is stored there.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Registered User
    Join Date
    Jun 2003
    Posts
    14
    Originally posted by Prelude
    This should be good. And why would using realloc give it a change of running faster? [/B]
    Well correct me if I'm wrong, or if didn't understand what minignaz means to do, but what I understood is that he wants to "resize" an array. In this case using malloc/calloc then free and memcpy repeatidly will allocate new memory each time you resize it. Realloc will avoid that if possible, i.e. if memory is available just after the one already allocated, in particular if no memory was granted on the heap between two resizes. At least that's what I thought :-) . Plus it will take care of the memcpy if needed.
    The specified object is a mutex object that was not released by the thread that owned the mutex object before the owning thread terminated. Ownership of the mutex object is granted to the calling thread, and the mutex is set to nonsignaled.
    Microsoft, Win32 API documentation
    You don't say? I'm owned.

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Well correct me if I'm wrong
    Well, you're not wrong (for the most part). However, you didn't quite understand quzah's reply. He gave a link to the man pages for calloc, malloc, free, and realloc. Presumably the OP would read the page thoroughly and realize that realloc is the correct function to use, thus saving us the effort of explaining the gory details.
    My best code is written with the delete key.

  12. #12
    Registered User
    Join Date
    Jun 2003
    Posts
    14
    I think I skipped quzah's post, at least I didn't follow the link.
    for the most part
    Any precision would be appreciated , I read the K&R chapter about this a bit quickly I must confess... But an idea of how this behave with real life libraries would be most welcomed.

    Thanks
    The specified object is a mutex object that was not released by the thread that owned the mutex object before the owning thread terminated. Ownership of the mutex object is granted to the calling thread, and the mutex is set to nonsignaled.
    Microsoft, Win32 API documentation
    You don't say? I'm owned.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Dynamic Array Allocation function
    By P4R4N01D in forum C++ Programming
    Replies: 6
    Last Post: 05-15-2009, 02:04 AM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. size of dynamic array
    By tommy_gunn in forum C Programming
    Replies: 3
    Last Post: 12-30-2004, 07:01 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM

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