Thread: how to initialize a new array

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    4

    how to initialize a new array

    I am struggling with a problem about automatically initializing an array and checking if memory is available for the array.

    I have created a Class that creates an array. An object of this class is created with a minimum and a maximum value (of the array).
    1). I want the constructor to initialize the array, set to NULL.
    2). The function 'CreateArray' must check if the array already exists before creating another one.

    How can I do this?

  2. #2
    Me -=SoKrA=-'s Avatar
    Join Date
    Oct 2002
    Location
    Europe
    Posts
    448
    1) Just put in the constuctor a line that says "array = NULL;" with whatever your array is called instead of "array". Also, array must be a pointer.

    2) What I usually do when I try to do that is:
    Code:
    void CreteArray(int len)
    {
        if(array != NULL) //if array is pointing to somewhere
           delete[] array; //delete whatever it's pointiting to
    
        array = new char[len]; //create the new array
    }
    change array and char to whatever suits in your case. Remember that array must be a pointer of the tipe you pass to new.

    Hope this helps
    SoKrA-BTS "Judge not the program I made, but the one I've yet to code"
    I say what I say, I mean what I mean.
    IDE: emacs + make + gcc and proud of it.

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Actually, the line "if(array != NULL)" is not needed -- it is legal to use delete or delete[] on a NULL pointer; the standard guarantees that this has no effect.

  4. #4
    Me -=SoKrA=-'s Avatar
    Join Date
    Oct 2002
    Location
    Europe
    Posts
    448
    >>it is legal to use delete or delete[] on a NULL pointer
    Well, that's at least six lines I've chopped off already from the program I'm working on and when I get my hands on some code I have at home there's gonna be some massive deleting

    After you use delete, will the pointer automatically point to NULL or do you have to do it yourself? I always set it to NULL afterwards but I like deleting not-needed lines.
    SoKrA-BTS "Judge not the program I made, but the one I've yet to code"
    I say what I say, I mean what I mean.
    IDE: emacs + make + gcc and proud of it.

  5. #5
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314
    What Cat said and make sure you set "array" to "NULL" after deleting it. delete does not do this and you're going to be in trouble if you delete an already deleted object.

    edit: yes Sokra, you have to set it to NULL, otherwise you'll have a lot of fun... I got some experience there
    Last edited by darksaidin; 08-25-2003 at 01:33 PM.
    [code]

    your code here....

    [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Modify an single passed array element
    By swgh in forum C Programming
    Replies: 3
    Last Post: 08-04-2007, 08:58 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM