Thread: Dynamic new..

  1. #1
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629

    Dynamic new..

    Hi, can someone please clarify this...pointers always...grrr
    anyhow

    int *a= new int[9] ;

    creates an array of pointers, each pointer pointing to an integer in memory

    int *a = new int*[9] ;

    should create an array of pointers, each pointer pointing to a pointer in memory..it wouldn't let me do the declaration above?

    I have to do int **a for it to work..Is this because it is a pointer to a pointer?
    I always treat DMA as a normal array because it is easier..but I guess there are always exceptions... :S
    You ended that sentence with a preposition...Bastard!

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If it's a dynamic 2D array, then obviously you need the later. Because a (the first) behave like an array, the first subscript leads directly to the integer. But in a 2D array, each of the subscripts lead directly to another array.

    So anyway, why do you need int** instead of a vector?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Yeah you could do vector<int*> instead... or vector<int> instead of new int[] and then do the x * DIM_X + y calculation yourself. Either way, if you want to flatten a 2-d array like that, you have to calculate a one dimensional subscript.

  4. #4
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Hi Elysia..
    I am confused lol..
    if i do this
    int **a = new int*[10]

    the first set of array is the array of pointers that new returns..the second set would be the int * pointers ..am I right in thinking that?

    i need it to implement graph representation..
    You ended that sentence with a preposition...Bastard!

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    int **a = new int*[10];

    That would be the first dimension, but you still have to allocate all of the second dimension which is what the first dimension points to. Or you can make it easier by using the flattened layout I talked about.

  6. #6
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    xD i can like comments...haha
    yo whiteflag...
    em, yeah our lecturer was saying thats inefficient to do.
    what I am trying to do is create an array of Node pointers that will point to connected nodes..

    so I have
    Node **a = new Node*[10]
    and that will point to a connected node..and that in turn will point to another node a[] is connected to..
    since Node **a = new Node*[10] is originally a 1dimensional arrays of pointers..
    i wasn't fully sure (and really still not) why i needed the double *
    You ended that sentence with a preposition...Bastard!

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Your lecturer is an idiot if he/she thinks it's inefficient.
    std::vector<std::vector<int>> myvec(std::vector<int>(second_dimension), first_dimension);
    There is your 2D array. Even better with boost.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Oh, well that's different. Your Node* type is the type of array element, so the regular base pointer requires another star. As long as your Node* elements are treated like single nodes, the array is still flat, but if a[x] is actually a list of nodes it's more like a matrix. Saying that the elements are still connected doesn't really help me tell you what's what. It depends on your usage of the Node* element.

    And there is a problem with vector< vector<int> >... it's not going to keep things rectangular, if you push to much or too little. So if a rectangular grid is what's important and you still want to avoid new, use something like vector< tr1::array<foo> >, I guess. Anything that forces reallocation of the whole matrix when the second dimension gets extended is good.
    Last edited by whiteflags; 03-16-2011 at 05:30 AM.

  9. #9
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Figure 4 is what I am trying to implement
    Part 5: From Trees to Graphs
    does it really matter what type the array is?
    The int*[], Node*[] dynamic array is still flat..its almost like saying int[4] or Node[9] except it is pointers... and as Elysia said..the subscripts directly access the values...so the declaration should work.

    OK..i will try this

    int **a = {0x40, 0x50,0xdF} ;

    a[0] accesses 0x40..this is the first star...a[0][0] the second subscript, the second * refers to the memory location that 0x40 points to.... :S
    You ended that sentence with a preposition...Bastard!

  10. #10
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Code:
    int ** a= new int * [10];
    This means that a is a double pointer that can only points to the single pointer. So, in the above written line, you've created a dynamic array of pointers (single) through a double pointer.
    Now what you've to do is to;
    create an array that will be pointed by your single pointers pointed by your double pointer (a).


    Code:
    int ** a=new int * [10];
    
    for(int i=0;i<9;i++){
                        a[i]=new int[size_of_your_array];
                        }
    That will allow you to create arrays.
    I don't care if someone doesn't like me, i was not put on earth to entertain everyone.

    No King, no Queen, I am the ACE of battle.

  11. #11
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    em..right
    sorry..I have to get it right in my own words..

    new int *[10]

    creates a 1 dimensional array of pointers having 10 elements.
    each of this elements can have subsequent dynamic arrays allocated to it...as in, it can point to a new allocated 1d array or a predefined one.

    each of the elements could also point to normal variables like
    int d;
    a[0] = &d

    ..so the first* refers to the pointer that accesses the return addresses from the new operator..and the second * refers to the address of &d, the address pointed to by the return pointers from d

    by the way, to clarify maybe I am having the whole thing upside down

    does int* a = new int[10]

    return only one pointer to an array of 10 integers

    or an array of 10 pointers to 10 integers?
    You ended that sentence with a preposition...Bastard!

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Eman
    by the way, to clarify maybe I am having the whole thing upside down

    does int* a = new int[10]

    return only one pointer to an array of 10 integers

    or an array of 10 pointers to 10 integers?
    It returns a pointer to the first element of an array of 10 int.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    yo Llight cool..i knew I was thinking about it the wrong way around..

    so the first start refers to the dereference of the address that a contains
    *a or a[0] which is the address of the first element of some array

    and the second star refers to the dereference of the address that the first element of that array contains...

    ...warmer?
    You ended that sentence with a preposition...Bastard!

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Here is how it looks like (where p is your pointer):
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    sweet Elysia..that should pretty much clear it up..
    I am not sure about the graph representation algorithm..i will create a different thread for that to make it neater..
    first I will try and solve it.
    danke much
    You ended that sentence with a preposition...Bastard!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 12-23-2009, 01:55 PM
  2. dynamic array/vector help
    By Cpro in forum C++ Programming
    Replies: 8
    Last Post: 04-05-2008, 03:30 PM
  3. Identify dynamic IP address of network device
    By BobS0327 in forum Tech Board
    Replies: 2
    Last Post: 02-21-2006, 01:49 PM
  4. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM
  5. Dynamic Toolbars
    By nvoigt in forum Windows Programming
    Replies: 1
    Last Post: 01-11-2002, 10:21 AM