Thread: Creating double dimension array.

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    76

    Creating double dimension array.

    Hi,
    I would like to creat dynamically a double dimension array. I did something like that:
    Code:
    #include <cstdio>
    
    int main()
    {
            int **p;
            p=new int[100];
            for(int i=0;i<100;i++)
                    p[i]=new int[30];
            return 0;
    }
    But i don't know if it's smart. Maybe there is any smarter way to create such array? And if i create array like that i have to use 'delete' first for each p[i] and then for generally p, right ?

    Best regards,
    apacz

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    I hope that first assignment got a warning from your compiler.

    Code:
    int main()
    {
            int **p;
            p=new int*[100]; // 100 pointers here, not 100 ints
            for(int i=0;i<100;i++)
                    p[i]=new int[30];
            return 0;
    }
    > And if i create array like that i have to use 'delete' first for each p[i] and then for generally p, right ?
    Yes, nested allocations like this need to be deleted in inside-out order.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    76
    Thx for answer.
    But in such situation:
    Code:
    graph::graph(int i)
    {
            if(i<=0)
                    throw bad_size(i);
            else
            {
                    vertex_number=i;
                    w=new int*[i];
                    for(int y=0;y<i;y++)
                    {
                            w[y]=new int[i];
                            for(int ii=0;ii<y;ii++)
                                    w[y][ii]=-1;
                    }
            }
    }
    Do i have to code a destructor for graph? And if yes do i have to destruct in this destructor all variables from that class or only **w and the rest leave alone ?
    Regards,
    apacz

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    new/deletes always have to balance, so new in a constructor should be balanced by a delete in a destructor.

    Consider using vectors instead.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Using a vector makes things much easier (no destructor code necessary to clean it up).

    Otherwise, if you stick with dynamic allocation, you should consider disabling or implementing the copy constructor and copy assignment operator for your graph class. Otherwise you will get strange crashes if a copy of your object is made.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating 5X3 array using double astrix
    By transgalactic2 in forum C Programming
    Replies: 10
    Last Post: 03-30-2009, 09:35 PM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  4. Creating array from data file...
    By Crankit211 in forum C Programming
    Replies: 2
    Last Post: 12-06-2001, 09:45 PM
  5. error message, what does it mean?
    By Shy_girl_311 in forum C++ Programming
    Replies: 5
    Last Post: 11-09-2001, 09:54 PM