Thread: dynamic memory?

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    30

    dynamic memory?

    why is this not possible?

    im trying to declare an two dimentional array this way but i get an error message "Cannot convert 'int ( *)[2]' to 'int *'
    Code:
    #include <iostream.h> int main()  {int * number; number = new int [5][2]; return 0; }
    sorry i cant use insert source code feature well
    Last edited by Chobo_C++; 03-14-2004 at 09:09 PM.

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    12
    I think the probelm is you have not declared number as an 2D array and possibly you have not declared new int as well.

  3. #3
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317

    The problem is you have not declared the pointer properly.

    Code:
    int (* number)[2];
    for multi dimensional arrays the compiler associates the array elements as part of the type so the pointer type has to match.

    int array[5][2] is of the type int[5][2] so the pointer has to be of type int *[2]. You need the parenthesis otherwise it will declare an array of pointers.
    Last edited by manofsteel972; 03-14-2004 at 10:09 PM.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    If you want a set size to your array then declare it as an array of pointers and dynamically allocate memory to each pointer:
    Code:
    int *array[5];
    for ( int i ( 0 ); i < 5; i++ )
      array[i] = new int[2];
    If the first dimension needs to be variable as well as subsequent dimensions then you will need to declare the array as a pointer to a pointer and allocate memory on both ends:
    Code:
    int **array = new int*[5];
    for ( int i ( 0 ); i < 5; i++ )
      array[i] = new int[2];
    With this second method, remember that the levels of indirection match the number of dimensions. And don't forget to release the memory.
    Code:
    for ( int i ( 0 ); i < 5; i++ )
      delete [] array[i];
    delete [] array;
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    30
    hmm.... it's hard to undestand what you guys are saying but i think i get the idea. thanks for the help, and by the way, why do i have to release the memory? do i relelease it at the end of the program or at the moment i want it to be released?

  6. #6
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    You need to release the memory, otherwise you have a 'memory leak', which will eventually lead to your computer crashing if you run the program too many times You can release it any time when you're done with it. Hopefully, that will be at some time before the program ends, otherwise you have some problems
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    30
    ooooooo ok. i just came up with another question. how do you pass an array from function to main function? oh and when using dynamic memory, does pointer become an array or just act as if it was an array? is 2d array an array or a pointer?

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >how do you pass an array from function to main function?
    It depends on how you declared the array. Just copy the declaration into your function parameter list and that should be enough.

    >does pointer become an array or just act as if it was an array?
    Nice catch. Not many people understand this. No, it simulates an array by taking advantage of the fact that an array name is often converted to a pointer to the first element and array subscripting is really pointer arithmetic. A dynamic array is not an array, even though it pretends to be one pretty well.

    >is 2d array an array or a pointer?
    If your declaration is an array, then it's an array. But beware the sneaky parentheses, they may try to trick you:
    Code:
    int a[10]; // Array
    int b[10][10]; // Array
    int *c[10]; // Array
    int *d; // Pointer
    int **e; // Pointer
    int (*f)[10]; // Pointer
    My best code is written with the delete key.

  9. #9
    Registered User
    Join Date
    Mar 2004
    Posts
    30

    Post

    ok thanks prelude, you sure do know a lot of things about programming

  10. #10
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Code:
    int a[10]; // Array
    int b[10][10]; // Array
    int *c[10]; // Array
    int *d; // Pointer
    int **e; // Pointer
    int (*f)[10]; // Pointer
    That's why I prefer to stick to 1-dimensional arrays
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Linking & Memory usage
    By @nthony in forum C Programming
    Replies: 2
    Last Post: 06-02-2007, 09:57 PM
  2. Dynamic memory allocation...
    By dicorr in forum C Programming
    Replies: 1
    Last Post: 06-24-2006, 03:59 AM
  3. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 01:41 PM
  4. dynamic memory + linked lists
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 02-10-2002, 04:50 PM
  5. Dynamic Memory Allocation for fstream (binary)
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 12-12-2001, 10:52 AM