Thread: re-calloc???

  1. #1
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489

    re-calloc???

    Help! I need to reallocating memory then set the entire/partial buffer into specific value.

    calloc only works for allocating memory, not reallocating memory
    and memset only works for char data type... T_T...

    Should I do:
    Code:
      buffer <- malloc(5)
    
      for i <- 0 to 4
        buffer[i] <- 2
      end for
    
      ; do something with buffer[i]
    
      buffer <- realloc(10)
    
      for i <- 5 to 9
        buffer[i] <- 2
      end for
    Or any std function out there?

    Btw, we should prefer to use std function instead of our function because they are faster sometime.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    memset will work with any pointer.
    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.

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Yeah. You can use realloc() to do your job and set the buffer to whatever value

  4. #4
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    Some addition,...

    This will works only for 0 value.

    Code:
    long long *int64;
    
    //malloc and do something like above (previous post)
    
    int64 = realloc(int64, 10 * sizeof(long long));
    
    memset(int64, 0, 10 * sizeof(long long));

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Yes, memset only works on arrays of bytes, that's true.
    If you want to assign specific pointer, float or struct values, then you need a loop.
    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.

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    87
    from what i hear, realloc cannot be used to downsize the array.

    although i have seen it happen quite often.

  7. #7
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    It can, there's just no reason to think the memory usage will actually become less. As it's probably more efficient for the OS just to leave you with a bit more than you want.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc, calloc from the FAQ
    By salvadoravi in forum C Programming
    Replies: 10
    Last Post: 01-21-2008, 03:29 AM
  2. Help with calloc and pointers to strings.
    By sweetarg in forum C Programming
    Replies: 1
    Last Post: 10-24-2005, 02:28 PM
  3. Malloc and calloc problem!!
    By xxhimanshu in forum C Programming
    Replies: 19
    Last Post: 08-10-2005, 05:37 AM
  4. Why use calloc()?
    By dwks in forum C Programming
    Replies: 8
    Last Post: 07-20-2005, 08:22 AM
  5. difference between calloc and malloc
    By saravanan_ts in forum C Programming
    Replies: 4
    Last Post: 07-28-2003, 06:13 AM