CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: realloc()

  1. #1
    Join Date
    Feb 2012
    Location
    INDIA
    Posts
    25

    realloc()

    {
    int*ptr = (int*)malloc(10*sizeof(int));

    ptr = realloc(ptr, 20*sizeof(int));

    }

    As realloc function is used to dynamically reallocate the memory and ptr is a pointer which points the dynamically allocated memory, but in case of realloc function why we have written (ptr, 20*sizeof(int))...

    Why we have written ptr here?? Like a malloc function it should be some integer value.
    TANUSHREE-AGRAWAL...

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: realloc()

    Because malloc and realloc are two different functions and different functions very often have different parameters.
    http://msdn.microsoft.com/en-us/libr...(v=VS.71).aspx
    http://msdn.microsoft.com/en-us/libr...(v=vs.71).aspx
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: realloc()

    How would it know which memory to reallocate if you don't tell it?

  4. #4
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: realloc()

    Do you have some kind of quiz in front of you Tanushreeagr?
    http://forums.codeguru.com/showthread.php?t=523950
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured