dynamic array of pointers, how to initialize quickly?

This is a discussion on dynamic array of pointers, how to initialize quickly? within the C++ Programming forums, part of the General Programming Boards category; I still have trouble understanding how to handle initialization of an array when its very large and unmanageable, i am ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    266

    dynamic array of pointers, how to initialize quickly?

    I still have trouble understanding how to handle initialization of an array when its very large and unmanageable, i am trying to test out memset function to see if that will do the trick.

    I have an array of pointers for a 'multidimensional array' ..I need the something that will do this here..without a loop that takes so much time.


    size of len1 will be around ~250,000
    Code:
    	
    
            int **dp = new int*[2];
    	for(int i=0;i<2;++i)
    		dp[i] = new int[len1+1];
    
            // Really need a faster solution for this!...250,000*2 loops is no good.
    	for(int i=0;i<2;++i)
    		for(int j=0; j<len1+1;++j)
    			dp[i][j] = 0;
    how can I use memset to place 0's in every spot of this kind of array?

    I see this example somewhere..for normal array.
    But, it gets complicated with pointer array of pointers...bleh
    Code:
    memset (an_array, 0, sizeof(an_array));
    any ideas? should I be using memset?

    Thanks a ton!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,993
    You should use memset on each of the two sub-arrays.

    If you are not using a buggy compiler, you can avoid memset altogether:
    Code:
    int **dp = new int*[2];
    for(int i=0;i<2;++i)
        dp[i] = new int[len1+1]();
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    is it as quick?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,993
    Quote Originally Posted by rodrigorules
    is it as quick?
    As what?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,827
    You should probably use a vector instead of pointers. Anyhow, memset is not possible here. memset works on contiguous arrays and obviously this isn't. It's more like a tree, so the only solution is a loop. Why do you need to 0 it in the first place, though?
    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.

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    as memset

    sorry i lack the knowledge on how the exactly the program sets the data..might be exactly the same way

    (i need zeros in it because I will sometimes accessing array without possibly assigning a value to it previously...i need it to be 0 by default)

    edit: hmm the () notation at the end of array doesn't seem to work for me.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,993
    Quote Originally Posted by rodrigorules
    as memset
    It should be no worse, but as I mentioned before, I have used a compiler that has a bug such that this value initialisation does not work on an array of objects of built-in types.

    By the way, why is dp a pointer to a pointer? It should be an array of 2 pointers, or an array of 2 vectors.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    why do you guys recommend vectors?

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,993
    Quote Originally Posted by rodrigorules
    why do you guys recommend vectors?
    Read:
    How do I deal with memory leaks?
    What's wrong with arrays?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,252
    Unless you are repeatedly initializing the array, the initialization time simply isn't going to matter. Use a loop.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  11. #11
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    sizeof(an_array) only returns the number of bytes in the array for static arrays. if you try this with a dynamically allocated array, you will return sizeof(pointer)

    if you were to use memset, you would need to:

    memset(ptr,0,sizeof(type)*length)


    if you wanted to initialize the memory all at once, you would have to do the indexing yourself.

    Code:
    int* 2darray = new int[height*width];
    memset(2darray,0,sizeof(int)*height*width);
    you lose the [][] notation this way, so you'd do better to use a wrapper class e.g:

    Code:
    class 2darray
    {
      private:
        int* buf;
      public:
      2darray(unsigned int _width,unsigned int _height):
        height(_height),
        width(_width),
        buf(new int[_height*_width])
      {
        memset(buf,0,sizeof(int)*height*width);
      }
      const unsigned int height;
      const unsigned int width;
      int* operator[](unsigned int y)
      {
          return buf[width*y];
      }
       ~2darray()
      {
        delete[] buf;
      }
    };
    
    //uncompiled, untested - simply for demonstration purposes.

    using the STL is probably the better choice, however.
    Last edited by m37h0d; 02-12-2010 at 11:27 AM.

  12. #12
    Registered User
    Join Date
    Feb 2010
    Posts
    4
    Why not just doing one big malloc (which is a lot faster than calling new for every byte of your array) and calculate pointer offsets in a for loop? The fact that you are calling new for every item in your array is what cause it to be slow, unless I misunderstood your code or what you are trying to do.

  13. #13
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    he's only doing 2 allocations.

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,827
    Why not malloc? Because this is C++, and malloc is bad in C++.
    It is possible to do...
    int (* p)[100] = new int[100][100];
    ...to get a pointer to a 2D array (which is contiguous). However, once again, you should be using vector if possible.
    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
    Registered User
    Join Date
    Feb 2010
    Posts
    4
    Oh ok nevermind I thought he was doing 2*250000 allocations.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Array of Pointers to Arrays
    By Biozero in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 02:31 PM
  3. dynamic array of pointers?
    By baniakjr in forum C++ Programming
    Replies: 3
    Last Post: 12-29-2006, 08:46 AM
  4. **Pointer to a Dynamic Array of Pointers
    By The Brain in forum C++ Programming
    Replies: 2
    Last Post: 06-03-2005, 06:52 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM

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