new and delete

This is a discussion on new and delete within the C++ Programming forums, part of the General Programming Boards category; just getting back to programming and want to check new and delete operators, are the following code snippet and comments ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    41

    Thumbs up new and delete

    just getting back to programming and want to check new and delete operators,
    are the following code snippet and comments correct, also the storage duration
    of the new object is from its creation up unto delete or end of program which
    in the latter case I would get a memory leak(I think) without delete.

    I know this code is not practical, for a start I would need to see if there
    is enough memory available to create the new object, are dynamic objects
    created from the stack or the heap, its my understanding that they are
    created on the heap to save room on the stack.

    could you also point me in the right direction for a full explanation
    of the stack and the heap.

    Code:
    #include<stdio.h>
    #include<conio.h>
    
    int main()
    {
         int age;
         int *age_ptr; /* pointer of type int */
    
         age_ptr = new int; /* age_ptr points to a new object of size\int */
                         /* and is identified by the name, name_ptr */
         age_ptr = 39;   /* the new dynamically created objects value is now 39 */
    
         printf("%d",*age_ptr);
    
         delete age_ptr;   /* destoy\free the memory on the new object */
    
         printf("\n\n%d",*age_ptr);  /* this still outputs 39 because the memory has'nt */
                                     /* been overwriten just freed */
         getch();
         return 0;
    }
    also would I delete a dynamic array like so delete[]array_name;
    and how about a multidimentional array.


    I know this is a lot of questions but I really appreciate all
    your guidence thank you.


    Please use [code][/code]Tags

  2. #2
    Registered User
    Join Date
    Jul 2003
    Posts
    11
    C doesn't have new and delete operators. You want to post this in the C++ forum.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    41

    code tags

    ps. What are code tags.
    Thanks

  4. #4
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    [code][/code] tags go around code to format it so that it is readable.

    Usage:
    [code]
    //Your code goes here.
    [/code]

    Please use them whenever posting more than a few lines of code. (A few meaning 3 or so)
    Away.

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    41
    How is it possible for me to use new and delete with functions
    from c like printf when new and delete are specific to c++.

  6. #6
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    new and delete are specific to C++ but printf() is not specific to C. You can use printf() in a C++ program.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  7. #7
    Registered User
    Join Date
    Jan 2002
    Posts
    41
    is everything in my prog perfect apart from being not very practical

    Code:
    #include<iostream.h>
    #include<conio.h>    // supported by c and c++
    
    int main()
    {
    
        int *age_ptr;  // pointer of type int
    
        age_ptr = new int;  // age_ptr now points to spare memory
                            // of size int(4bytes)
    
        *age_ptr=39;        // store 39 at the address age_ptr points to
    
        cout<<*age_ptr;     // output the value stored at the address age_ptr
                            // points to
    
        delete age_ptr;     // kill the new object(free the memory(this is vital))
    
        getch();
    
        return 0;
    }

  8. #8
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    No. You're not #including true C++ headers.

    The second is compiler-specific, but there's not a whole lot you can do about it; getch() is not a standard C or C++ function, and there is no standard function that is guaranteed to have exactly the same behavior. The first header is simply wrong. It should instead be:

    Code:
    #include <iostream>
    using namespace std;

  9. #9
    Registered User
    Join Date
    Jan 2002
    Posts
    41
    what does that mean.
    ps I have an old compiler

  10. #10
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Watch out anthonye. You're opening a Pandora's Box asking about c++ headers. The rule is, "despite the fact that there is commonly no difference in behavior between using iostream.h and iostream; using namespace std;, a bunch of people go nuts if you ask why you should use iostream".

    The new C++ standard demands that you use iostream and namespace.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  11. #11
    Registered User
    Join Date
    Jan 2002
    Posts
    41
    Yes ok. but what is namespaceand are all headers writen
    without the .h extension ie #include<iostream.h> is now
    #include<iostream>.

    thanks guys.
    Last edited by anthonye; 07-08-2003 at 03:22 AM.

  12. #12
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Originally posted by bennyandthejets
    The rule is, "despite the fact that there is commonly no difference in behavior between using iostream.h and iostream; using namespace std;, a bunch of people go nuts if you ask why you should use iostream".
    Not all that common, actually. VC++ 7.1 doesn't even have the iostream.h file anymore, and other compilers may follow suit.

    anthonye: Namespaces are ways to avoid naming conflicts between libraries. For example, the standard C++ library is in namespace std. So cout is really std::cout, endl is std::endl, etc.

    Other libraries will put things in other namespaces. E.g. if you download and install Boost, it will put everything into namespace boost, so its shared pointer is boost::shared_ptr.

    Now, you can use the full names for accessing each member (which is the better way to do things), as so:

    Code:
    #include <iostream>
    
    int main(){
      std::cout << "Hello World!" << std::endl;
    }
    or you can use the keyword using to pull symbols into the current namespace (and thus omit the std:: part.) You can pull all or some symbols into the current namespace.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21