Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19
Return value of memory allocation ?
Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Return value of memory allocation ?

  1. #1
    Senior Member
    Join Date
    Jul 2001
    Posts
    280

    Return value of memory allocation ?

    I'm reading a tutorial about c++ programming and it says "you should check the return value of every memory allocation". What does that mean?

  2. #2
    Guest

    Re: Return value of memory allocation ?

    Code:
    #include <cassert>
    
    ptr = new int[size] // dynamic allocate memory
    assert(ptr != 0)    // terminate if memory not allocated
    Like this ??

  3. #3
    Senior Member
    Join Date
    Jul 2001
    Posts
    280

    Re: Return value of memory allocation ?

    Oh, I see. *I thought it had something to do with removing things from memory and then checking to see if the memor y was in fact freed.

  4. #4
    Guest

    Re: Return value of memory allocation ?


    Oh, I see. I thought it had something to do with removing things from memory and then checking to see if the memor y was in fact freed.
    no no, delete should remove all traces of the data.

  5. #5
    Senior Member
    Join Date
    Jul 2001
    Posts
    280

    Re: Return value of memory allocation ?

    So delete doesn't just remove the pointers to the memory and mark it free, it actually destroys the data?

  6. #6
    Guest

    Re: Return value of memory allocation ?


    So delete doesn't just remove the pointers to the memory and mark it free, it actually destroys the data?
    Deleting the pointer is removing all traces, it's marked as usable space, to be overwritten.

  7. #7

    Re: Return value of memory allocation ?


    So delete doesn't just remove the pointers to the memory and mark it free, it actually destroys the data?
    There is an easier way to say it than Lovechild (but he's not incorrect.)

    free()'d memory is not necessarily destroyed, that's compiler/OS dependent. In some cases, it may be destroyed, in others, it may merely be marked as unused. In the latter case, you could, if you really wanted to, keep using that data, but you can't rely on that data staying the same.

  8. #8
    Senior Member
    Join Date
    Jul 2001
    Posts
    280

    Re: Return value of memory allocation ?

    Code:
    #include <cassert>
    
    ptr = new int[size] *// dynamic allocate memory
    assert(ptr != 0) * * * *// terminate if memory not allocated
    Like this ??
    Is this how one would declared the variable [tt]ptr[/tt]?
    Code:
    ...
    int *ptr;
    ...
    ptr= new int[size];
    ...
    Also, what is the difference between these two lines:
    Code:
    myclass->mymethod();
    (*myclass).mymethod();

  9. #9

    Re: Return value of memory allocation ?

    I don't se any diffrence if it's the same as ansi c
    there -> == (*variabel).(element)

  10. #10
    Moderator
    Advisor
    redhead's Avatar
    Join Date
    Jun 2001
    Location
    Copenhagen, Denmark
    Posts
    811

    Re: Return value of memory allocation ?



    Is this how one would declared the variable [tt]ptr[/tt]?
    Code:
    ...
    int *ptr;
    ...
    ptr= new int[size];
    ...
    Yes, that is the way to declare an int array, which is created with new, or you could use malloc() or calloc() or realloc() or alloca() any other memory allocating version.

    Also, what is the difference between these two lines:
    Code:
    myclass->mymethod();
    (*myclass).mymethod();
    This is playing with pointers, the derefferencing method with '->' is my favorit.

Similar Threads

  1. About Space allocation
    By ubunfan in forum Linux - General Topics
    Replies: 2
    Last Post: 07-22-2011, 04:58 AM
  2. The Return of cga
    By cga in forum General Chat
    Replies: 2
    Last Post: 12-18-2005, 01:21 AM
  3. Freeing ext3 file allocation blocks
    By gabbath in forum Linux - General Topics
    Replies: 0
    Last Post: 11-14-2005, 08:02 PM
  4. The Return of Il Duce
    By jman_77 in forum General Chat
    Replies: 4
    Last Post: 04-27-2002, 03:07 AM
  5. Return to Castle Wolfenstein Singleplayer
    By in forum Linux - Software, Applications & Programming
    Replies: 6
    Last Post: 01-20-2002, 01:50 AM

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
  •