Difference between... (pointers)

This is a discussion on Difference between... (pointers) within the C++ Programming forums, part of the General Programming Boards category; What's that difference between Code: int **foo; *foo = new int[x]; for(int i = 0; i < x; i++) foo[i] ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    129

    Wink Difference between... (pointers)

    What's that difference between

    Code:
    int **foo;
    *foo = new int[x];
    for(int i = 0;  i < x;  i++)
        foo[i] = new int[y];
    and

    Code:
    int **foo;
    foo = new int*[x];
    for(int i = 0;  i < x;  i++)
        foo[i] = new int[y];
    which causes

    Code:
    for(int i = 0;  i < x;  i++)
        delete [] foo[i];
    delete [] foo;
    crash in the first?
    Last edited by kooma; 02-13-2002 at 02:35 AM.
    kooma - t.h@iki.fi

  2. #2
    Registered User Hoxu's Avatar
    Join Date
    Nov 2001
    Posts
    25
    dunno but it would be nice to know :P

  3. #3
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Deleting the first allocation isn't causing the crash, it's the actual allocation.

    int **foo;
    *foo = new int[x];

    You are de-referencing foo, but it doesn't point to anything. You need to point foo at something, then de-reference as you've done in the second example.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers
    By Big_0_72 in forum C Programming
    Replies: 3
    Last Post: 10-28-2008, 08:51 PM
  2. MergeSort with array of pointers
    By lionheart in forum C Programming
    Replies: 18
    Last Post: 08-01-2008, 11:23 AM
  3. vector destruction question
    By dudeomanodude in forum C++ Programming
    Replies: 4
    Last Post: 04-04-2008, 02:20 PM
  4. Pointers on pointers to pointers please...
    By Morgan in forum C Programming
    Replies: 2
    Last Post: 05-16-2003, 12:24 PM
  5. Replies: 1
    Last Post: 05-07-2002, 05:18 AM

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