Dynamically creating an object

This is a discussion on Dynamically creating an object within the C++ Programming forums, part of the General Programming Boards category; Sorry to post so many newbie questions, but is this correct? Code: aClass *myClass = new aClass; is, afaik , ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    28

    Dynamically creating an object

    Sorry to post so many newbie questions, but is this correct?

    Code:
    aClass *myClass = new aClass;
    is, afaik, the way that you creat a new class object dynamically.

    My question is, is this the proper way to delete it?

    Code:
    free(myClass);
    by delete I am of course primarily concerned with freeing the memory up for further use in the program.

  2. #2
    Registered User Noose's Avatar
    Join Date
    Jul 2003
    Posts
    11
    Thats the right way to create it but never "free()" "new"

    malloc() you free()

    new you delete

    or if its an array of objects

    delete[]

    dont cross the two over

  3. #3
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    To elaborate:

    Code:
    int *a=new int;
    char *b=new char [256];
    char *c=(char *)malloc(256);
    
    free(c);
    delete [] b;
    delete a;
    Those are the correct ways to free allocated memory.

    EDIT: Blindingly obvious type error
    Last edited by bennyandthejets; 09-26-2003 at 11:01 PM.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    28

    Ok but...

    Now, this is going to seem REALLY newbish, but in the case of

    Code:
    aClass *myClass = new aClass;
    do I do

    Code:
     delete myClass;
    or

    Code:
    delete *myClass;
    and if I

    Code:
    aClass *myClass = new aClass[10]
    first of all is that the way I create an array for class? and secondly
    once again do I delete with the * or not? When do I delete with the pointer * and when don't I?

  5. #5
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Code:
    aClass *myClass = new aClass;
    delete myClass;
    Code:
    aClass *myClass = new aClass[10];
    delete[] myClass;
    You never dereference the pointer -- delete and delete[] expect pointers, not objects.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating A Thread W/ PThreads within an Object
    By HalNineThousand in forum Linux Programming
    Replies: 12
    Last Post: 03-28-2008, 03:57 PM
  2. Object destroy itself?
    By cminusminus in forum C++ Programming
    Replies: 28
    Last Post: 03-27-2008, 02:08 AM
  3. using this as synchronization object
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 03-22-2008, 08:49 AM
  4. Really Hope I'm not Overposting (Object passing)
    By Shamino in forum Game Programming
    Replies: 10
    Last Post: 12-16-2005, 09:38 AM
  5. DirectX 8: Creating the DirectX Object
    By Toraton in forum Game Programming
    Replies: 3
    Last Post: 11-28-2002, 03:45 AM

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