Thread: Syntax for 'new' and 'delete'?

  1. #1
    Registered User LordVirusXXP's Avatar
    Join Date
    Dec 2002
    Posts
    86

    Question Syntax for 'new' and 'delete'?

    I'm using Microsoft Visual C++, and for some reason, the syntax that some tutorials give me for creating new objects and deleting them doesn't work. I've tried the following ways:

    Cat Frisky = new Cat;

    new Cat Frisky;

    Cat Frisky;
    new Frisky Fluffy;

    new Cat = Frisky;

    And none of them work. I dont know the proper syntax for this. Could someone help me out? I tried a lot of different syntax, but nothing works.
    - Dean

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    predefined class

    int * a = new int;

    user defined class, call it Cat;

    Cat * Frisky = new Cat;

    delete a;
    delete Frisky;


    new returns a pointer to the first location in memory assigned to the "new object". Note if you want a group of objects then you do this:

    Cat * kittens = new Cat[3];
    delete [] kittens;

  3. #3
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Hmmm it seems like you have some syntax errors .

    Code:
    Cat Frisky = new Cat;
    This should work (make sure that it maches the constructors for Cat).

    Code:
    new Cat Frisky;
    Shouldn´t work. I think you mean
    Code:
    //Invokes copy-construtor
    new Cat(Frisky);
    //Make sure you store the return object in a Cat-pointer to avoid a memory leak

    Code:
    Cat Frisky;
    new Frisky Fluffy;//Se my previous explantion why not us it like this
    //New return a memory adress and should be stored in a Cat-pointer
    new Cat = Frisky;
    Try change it to
    Code:
    Cat *Frisky;
    Frisky = new Cat;
    Also make sure you are creating a console application and nothing else!

  4. #4
    Registered User LordVirusXXP's Avatar
    Join Date
    Dec 2002
    Posts
    86
    Thanks guys, I guess this thread can be closed now
    - Dean

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Originally posted by LordVirusXXP
    Thanks guys, I guess this thread can be closed now
    Please dont use the link below to ask for the Mods to close this thread.......there's no need for it to be closed....you have what you want and that's great.....others can take the thread further if they wish....it will only be closed when it breaks one of the board rules (as shown at the top of each forum)

  6. #6
    Registered User LordVirusXXP's Avatar
    Join Date
    Dec 2002
    Posts
    86
    My delete syntax isn't working.

    I created a new int with:

    int * a = new int;

    and now I can delete it.

    I've already tried

    delete a;

    but it doesn't work. Any suggestions?
    - Dean

  7. #7
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    should work so my guess is that its a scope issue and that you have lost/hidden a. show us the code.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  8. #8
    Registered User LordVirusXXP's Avatar
    Join Date
    Dec 2002
    Posts
    86
    You're right. I was able to create a new int before main, but it wouldn't let me delete it outside of main. I got it working now!Z Thanks.
    - Dean

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Crazy errors caused by class, never seen before..
    By Shamino in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 11:54 AM
  2. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  3. using c++ in c code
    By hannibar in forum C Programming
    Replies: 17
    Last Post: 10-28-2005, 09:09 PM
  4. Problem need help
    By Srpurdy in forum C++ Programming
    Replies: 1
    Last Post: 07-24-2002, 12:45 PM
  5. memory management...
    By master5001 in forum Game Programming
    Replies: 24
    Last Post: 01-07-2002, 05:50 PM