initialization of double pointer with new

This is a discussion on initialization of double pointer with new within the C++ Programming forums, part of the General Programming Boards category; Hi! Can anyone, please, write me an example of initialization of double pointer with new? char **tableOfStrings = new ...??? ...

  1. #1
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669

    Question initialization of double pointer with new

    Hi!

    Can anyone, please, write me an example of initialization of double pointer with new?

    char **tableOfStrings = new ...???

    Thanks!
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    You would do something like this:
    Code:
    #include <iostream>
    
    int main(void)
    {
      int np = 10;
      int *p;
      int **dp;
      
      p = new int;
      dp = new int *;
      
      if(p == NULL || dp == NULL)
      {
        std::cout<<"Could Not Allocate Memory"<<std::endl;
        std::cin.get();
        return 0;
      }  
      
      p = &np;
      dp = &p;
      
      std::cout<<np<<std::endl;
      std::cout<<*p <<std::endl;
      std::cout<<**dp<<std::endl;
          
      **dp = 15;
      
      std::cout<<np<<std::endl;
      std::cout<<*p <<std::endl;
      std::cout<<**dp<<std::endl; 
        
      delete p;
      delete dp;
    
      std::cin.get();
      return 0;
      
    }
    Last edited by prog-bman; 01-19-2005 at 03:29 AM.
    Woop?

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    delete p;
    delete dp;

    Except you trashed your allocated pointers with
    p = &np;
    dp = &p;
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Can anyone, please, write me an example of initialization of double pointer with new?
    Code:
    const int len = 80;
    char **tableOfStrings = new char[10];
    for (int i=0; i<10; i++)
       tableOfStrings[i] = new char[len];

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Swoopy: You mean,
    Code:
    char **tableOfStrings = new char*[10];
    for (int i=0; i<10; i++)
       tableOfStrings[i] = new char[len];
     
    //Work with your tableOfStrings
     
    for(int i = 0; i < 10; i++)
       delete[] tableOfStrings[i];
    delete tableOfStrings;
    >Can anyone, please, write me an example of initialization of double pointer with new?
    Lightning fast method, but don't try this at home otherwise your instructor will shoot you!
    Code:
    const int rows = 500;
    const int maxSizeOfEachRow = 200;
     
    char** tableOfStrings = (char**)(new char[rows * sizeOfRow]);
     
    //... do something with your table of strings
     
    delete[] tableOfStrings[0];
    Just Google It. √

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

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Swoopy: You mean,
    Yes, thanks Hunter, as one of our other members would say, I was on the phone at the time.

  7. #7
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    I get a debug assertion on delete when I try that code, Hunter (using VC6). After poking at it so that the delete worked, I got access violations when I tried to use tableOfStrings[x][y] to access an individual character.
    Code:
    int main()
    {
    	const int rows = 500;
    	const int maxSizeOfEachRow = 10;
    
    	char** tableOfStrings = NULL;
    	tableOfStrings = (char**)(new char[rows * maxSizeOfEachRow]);
     
    	//... do something with your table of strings
    // 	tableOfStrings[0][0] = 'a';  //causes bad things to happen
    	tableOfStrings[0] = "123456789";
    	tableOfStrings[1] = "abcdefghi";
    
    	cout << tableOfStrings[0] << endl;
    	cout << tableOfStrings[1] << endl;
    	
    	delete tableOfStrings;
    //	delete[] tableOfStrings[0];  //debug assertion
    
    	return 0;
    }
    Am I doing something wrong, or should I not expect this sort of thing to allow individual character access?
    Last edited by pianorain; 01-19-2005 at 09:50 AM. Reason: Forgot to ask my question
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    > Am I doing something wrong
    Actually, you're doing everything wrong.

    > tableOfStrings = (char**)(new char[rows * maxSizeOfEachRow]);
    If you're casting new, think that you're doing something very wrong.

    In this case, you should have written
    Code:
    tableOfStrings = new char*[rows];

    Now that's only part of the story, now allocate each row as well
    Code:
    for ( int r = 0 ; r < rows ; r++ ) tableOfStrings[r] = new char[maxSizeOfEachRow];

    Now you can do this
    Code:
    tableOfStrings[0][0] = 'a';

    > tableOfStrings[0] = "123456789";
    Whilst this is legal, you've just leaked some memory, and set up a failure when you come to delete this memory.
    Code:
    strcpy( tableOfStrings[0], "123456789" );

    > delete tableOfStrings;
    Nested structures are freed in reverse order.
    Code:
    for ( int r = 0 ; r < rows ; r++ ) delete [] tableOfStrings[r];
    delete [] tableOfStrings;
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    > tableOfStrings = (char**)(new char[rows * maxSizeOfEachRow]);
    If you're casting new, think that you're doing something very wrong.
    Oops. I remember this method being posted some time ago as a faster method of allocating a 2D array, though apparently I remembered the method wrong. It should really be:
    Code:
    char** tableOfStrings = new char*[rows];
    char* bigMemoryChunk = new char[rows * rowLength];
    for(int i = 0; i < rows; ++i)
       tableOfStrings[i] = bigMemoryChunk + (rowLength * i);
     
    ...
     
    delete[] bigMemoryChunk;
    delete[] tableOfStrings;
    My apologies for the confusion. And this still probably shouldn't be done unless what you're doing is time-critical anyway, which I highly doubt it is at this point. It's just something to keep at the back of your mind until the day comes when you'll actually need to use it.
    Last edited by Hunter2; 01-19-2005 at 10:11 AM.
    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. pointer with double asterisk
    By skringla in forum C Programming
    Replies: 10
    Last Post: 11-27-2008, 06:33 AM
  2. double pointer to a structure
    By rohit_second in forum C Programming
    Replies: 5
    Last Post: 11-25-2008, 03:32 AM
  3. No Match For Operator+ ???????
    By Paul22000 in forum C++ Programming
    Replies: 24
    Last Post: 05-14-2008, 10:53 AM
  4. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  5. Conversion From C++ To C
    By dicon in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 02:54 PM

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