+ Reply to Thread
Page 2 of 3
FirstFirst 1 2 3 LastLast
Results 11 to 20 of 27

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

  1. #11
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,684
    Blog Entries
    57

    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).
    CodeCall Blog | CodeCall Wiki | Shareware
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  2. #12
    Newbie sumitsen is an unknown quantity at this point
    Join Date
    Oct 2009
    Posts
    1

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

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

  3. #13
    dcs
    dcs is offline
    Guru dcs is just really nice dcs is just really nice dcs is just really nice dcs is just really nice
    Join Date
    Mar 2008
    Posts
    768

    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.

  4. #14
    Code Warrior Guest is a name known to all Guest is a name known to all Guest is a name known to all Guest is a name known to all Guest is a name known to all Guest is a name known to all Guest's Avatar
    Join Date
    Sep 2009
    Location
    United States
    Age
    16
    Posts
    2,639
    Blog Entries
    3

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

    Can you explain why I should use a temporary?
    Code:
     / ___|_   _  ___  ___| |_ 
    | |  _| | | |/ _ \/ __| __|
    | |_| | |_| |  __/\__ \ |_ 
     \____|\__,_|\___||___/\__|

  5. #15
    dcs
    dcs is offline
    Guru dcs is just really nice dcs is just really nice dcs is just really nice dcs is just really nice
    Join Date
    Mar 2008
    Posts
    768

    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.

  6. #16
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,684
    Blog Entries
    57

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

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

  7. #17
    Learning Programmer psam is on a distinguished road
    Join Date
    Jun 2009
    Posts
    34

    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().

  8. #18
    Code Warrior Guest is a name known to all Guest is a name known to all Guest is a name known to all Guest is a name known to all Guest is a name known to all Guest is a name known to all Guest's Avatar
    Join Date
    Sep 2009
    Location
    United States
    Age
    16
    Posts
    2,639
    Blog Entries
    3

    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.
    Code:
     / ___|_   _  ___  ___| |_ 
    | |  _| | | |/ _ \/ __| __|
    | |_| | |_| |  __/\__ \ |_ 
     \____|\__,_|\___||___/\__|

  9. #19
    Newbie n00py is on a distinguished road
    Join Date
    Jan 2010
    Posts
    4

    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 07:02 PM.

  10. #20
    Code Warrior Guest is a name known to all Guest is a name known to all Guest is a name known to all Guest is a name known to all Guest is a name known to all Guest is a name known to all Guest's Avatar
    Join Date
    Sep 2009
    Location
    United States
    Age
    16
    Posts
    2,639
    Blog Entries
    3

    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.
    Code:
     / ___|_   _  ___  ___| |_ 
    | |  _| | | |/ _ \/ __| __|
    | |_| | |_| |  __/\__ \ |_ 
     \____|\__,_|\___||___/\__|

+ Reply to Thread
Page 2 of 3
FirstFirst 1 2 3 LastLast

Thread Information

Users Browsing this Thread

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

     

Similar Threads

  1. Replies: 5
    Last Post: 10-22-2008, 08:40 PM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

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