+ Reply to Thread
Page 2 of 4 FirstFirst 1234 LastLast
Results 11 to 20 of 34

Thread: Dynamic Arrays: Using malloc() and realloc()

  1. #11
    Join Date
    Jul 2006
    Posts
    16,478
    Blog Entries
    75
    Rep Power
    143

    Re: Dynamic Arrays: Using malloc() and realloc()

    Yes, C++ vectors do automatic resizing. From what I can tell, it isn't required that they double on each resize (It looks like it could just allocate 100 elements at a time, for example).
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #12
    sumitsen is offline Newbie
    Join Date
    Oct 2009
    Posts
    1
    Rep Power
    0

    Re: Dynamic Arrays: Using malloc() and realloc()

    some lines in the code needs to be user friendly.................but still the explanation was superb............

  4. #13
    dcs
    dcs is offline Guru
    Join Date
    Mar 2008
    Posts
    775
    Rep Power
    23

    Re: Dynamic Arrays: Using malloc() and realloc()

    Quote Originally Posted by Guest View Post
    Code:
    data=realloc(data,(i+2)*sizeof(int));
    Avoid this idiom. Use a temporary.

  5. #14
    Join Date
    Sep 2009
    Location
    USA
    Posts
    3,400
    Blog Entries
    5
    Rep Power
    37

    Re: Dynamic Arrays: Using malloc() and realloc()

    Can you explain why I should use a temporary?
    Root Beer == System Administrator's Beer
    Download the new operating system programming kit! (some assembly required)

  6. #15
    dcs
    dcs is offline Guru
    Join Date
    Mar 2008
    Posts
    775
    Rep Power
    23

    Re: Dynamic Arrays: Using malloc() and realloc()

    Quote Originally Posted by Guest View Post
    Can you explain why I should use a temporary?
    Did you follow the link? If realloc fails, you leak the original memory.

  7. #16
    Join Date
    Jul 2006
    Posts
    16,478
    Blog Entries
    75
    Rep Power
    143

    Re: Dynamic Arrays: Using malloc() and realloc()

    If the realloc fails, you just lost your handle on the data.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  8. #17
    psam is offline Learning Programmer
    Join Date
    Jun 2009
    Posts
    34
    Rep Power
    10

    Re: Dynamic Arrays: Using malloc() and realloc()

    Great tutorial.
    But what's with the goto? You can simply use:
    Code:
    else
    {
    	free(data);
    	printf("Not enough memory!\n");
    	return 1;
    }
    Right in the spot.

    And maybe you could add calloc(), though it's the same as malloc().

  9. #18
    Join Date
    Sep 2009
    Location
    USA
    Posts
    3,400
    Blog Entries
    5
    Rep Power
    37

    Re: Dynamic Arrays: Using malloc() and realloc()

    For some reason, I was probably thinking you had to return values at the end of the program. It is changed now.
    Root Beer == System Administrator's Beer
    Download the new operating system programming kit! (some assembly required)

  10. #19
    n00py is offline Newbie
    Join Date
    Jan 2010
    Posts
    4
    Rep Power
    0

    Re: Dynamic Arrays: Using malloc() and realloc()

    You can define a pointer like so
    No, that is an example of a declaration for a pointer variable. The definition of pointer is not "int *array;".

    The pointer points to a random location at first
    A pointer does always points to something (or has the value NULL).
    According to the standard, a pointer variable contains an indeterminate value before it is initialised, and after free() is called with it's pointer value as an argument. Your debugger might set it to 0xbadc0de, for example. Using an indeterminate value such as this in any way (including a pointer comparison) is enough to invoke undefined behaviour according to the standard.

    malloc() returns a void pointer and takes an argument of how many bytes to give to the pointer.
    As previously stated, malloc accepts an argument that shall be the size of the allocation that the returned void pointer points to, providing the allocation is successful. malloc() does not "take an argument of how many bytes to give the pointer". The size in bytes of a void * pointer is constant when considering a specific implementation.

    Using malloc like the above is very similar to doing this
    I disagree. int array[2]; This is an example of using automemory. Automemory is valid only for it's scope. The memory allocated and pointed to by the return value of malloc() is valid up until it is deallocated using free().

    Now I will show you how to use realloc().
    realloc() may lead to code which leaves stale pointers. Using those stale pointers (even in a simple pointer comparison) invokes undefined behaviour according to the C standard.

    You use realloc after you have used malloc to give the array more or less memory.
    malloc() and realloc() are not operations that result in or operate on arrays. Arrays decay to pointers during runtime. Calling realloc() on an array would invoke undefined behaviour, and malloc()/realloc() return pointers that point to an allocated sequence of bytes. Here's an example that displays an obvious difference between a pointer and an array:
    Code:
    int array[100];
    int *pointer = malloc(100);
    printf("sizeof(array) = %zu\n", sizeof(array));
    printf("sizeof(pointer) = %zu\n", sizeof(pointer));
    When you done using a pointer, you can free the memory
    You can only free() pointers that were specifically returned by malloc() or realloc(). free()ing any pointer which was not returned by malloc() or realloc() invokes undefined behaviour. Furthermore, when you are done with a pointer that is returned by malloc() or realloc(), you must deallocate it by calling free() with the pointer as it's argument in order to avoid memory leaks.

    for (i--;i>=0;i--)
    putchar(data[i]);
    This is an example of invoking undefined behaviour.
    Last edited by n00py; 01-30-2010 at 05:02 PM.

  11. #20
    Join Date
    Sep 2009
    Location
    USA
    Posts
    3,400
    Blog Entries
    5
    Rep Power
    37

    Re: Dynamic Arrays: Using malloc() and realloc()

    Quote Originally Posted by n00py View Post
    No, that is an example of a declaration for a pointer variable. The definition of pointer is not "int *array;".
    That's what I meant, I'll change it.

    According to the standard, a pointer variable contains an indeterminate value before it is initialised, and after free() is called with it's pointer value as an argument. Your debugger might set it to 0xbadc0de, for example. Using an indeterminate value such as this in any way (including a pointer comparison) is enough to invoke undefined behaviour according to the standard.
    That's why I said the location is random. You don't know where it points to unless you initialize it.

    As previously stated, malloc accepts an argument that shall be the size of the allocation that the returned void pointer points to, providing the allocation is successful. malloc() does not "take an argument of how many bytes to give the pointer". The size in bytes of a void * pointer is constant when considering a specific implementation.
    The wording is a little weird there. I'll make it more clear.

    I disagree. int array[2]; This is an example of using automemory. Automemory is valid only for it's scope. The memory allocated and pointed to by the return value of malloc() is valid up until it is deallocated using free().
    I said it's similar, not exactly the same.

    realloc() may lead to code which leaves stale pointers. Using those stale pointers (even in a simple pointer comparison) invokes undefined behaviour according to the C standard.
    I don't know what a stale pointer is.

    malloc() and realloc() are not operations that result in or operate on arrays. Arrays decay to pointers during runtime. Calling realloc() on an array would invoke undefined behaviour, and malloc()/realloc() return pointers that point to an allocated sequence of bytes. Here's an example that displays an obvious difference between a pointer and an array:
    Code:
    int array[100];
    int *pointer = malloc(100);
    printf("sizeof(array) = %zu\n", sizeof(array));
    printf("sizeof(pointer) = %zu\n", sizeof(pointer));
    Just problems with the wording I can easily fix.

    You can only free() pointers that were specifically returned by malloc() or realloc(). free()ing any pointer which was not returned by malloc() or realloc() invokes undefined behaviour. Furthermore, when you are done with a pointer that is returned by malloc() or realloc(), you must deallocate it by calling free() with the pointer as it's argument in order to avoid memory leaks.
    I'm aware of this, again it's something I should probably have clarified.

    This is an example of invoking undefined behaviour.
    How? I already know variable i is the index at the end of the allocated memory, and I'm just going backwards until i is less than zero.
    Root Beer == System Administrator's Beer
    Download the new operating system programming kit! (some assembly required)

+ Reply to Thread
Page 2 of 4 FirstFirst 1234 LastLast

Thread Information

Users Browsing this Thread

There are currently 4 users browsing this thread. (0 members and 4 guests)

Similar Threads

  1. what exactly is the second argument of realloc()???
    By shakisparki in forum C and C++
    Replies: 2
    Last Post: 04-05-2011, 08:18 PM
  2. realloc issues
    By chili5 in forum C and C++
    Replies: 4
    Last Post: 03-20-2011, 03:13 PM
  3. Using malloc() and realloc() for a variable-length string
    By DarkLordofthePenguins in forum C and C++
    Replies: 6
    Last Post: 03-17-2011, 10:04 PM
  4. Possible malloc issue, not really sure...
    By MichaelNQ in forum C and C++
    Replies: 4
    Last Post: 11-25-2009, 04:49 PM
  5. Dynamic Arrays
    By Fedex in forum C and C++
    Replies: 3
    Last Post: 12-02-2007, 02:45 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts