CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2002
    Posts
    8

    Lightbulb new and delete with multi_dimension array

    I need to dynamically generate a multi_demension array. I know
    how to do it with calloc() and free(), Could you tell me how to
    do it with new() and delete()?

    Thanks

    //Code:

    // Memory allocation
    int *** pSomething

    pSomething = (int ***)calloc(n1, sizeof(int **));
    for(int i=0; i<n1; i++) {
    pSomething[i] = (int **)calloc(n1, sizeof(int **));
    for(int j=0; j<n2; j++) {
    pSomething[i][j] = (int *)calloc(n2, sizeof(int *));
    }
    }

    //free

    for(int i=0; i<n1; i++) {
    for(int j=0; j<n2; j++) {
    free pSomeThing[i][j];
    }
    free pSomeThing[i];
    }
    free pSomeThing;

    //End of the code

  2. #2
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    Well, hum, I don't like this way of accessing arrays. My c-style arrays are always :
    Code:
    long *pArray;
    long xdim, ydim, zdim;
    
    pArray = (long *) calloc(sizeof(long) * xdim * ydim * zdim);
    this translates well when using new instead of calloc.

    To quote the standard STL response, you could use std::vector and write a wrapper class for a three dimensional array.

  3. #3
    Join Date
    Aug 2002
    Posts
    8
    Thanks, but if I did this way, I cann't access the data by using
    pArray[i][j][k]. ???

  4. #4
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    Hi,
    If you're interested in multi-dimensional vectors, check out this
    old codeguru posting:
    http://www.codeguru.com/forum/showth...ector+operator

    Here's a link from the last discussion that is relevant:
    http://www.cuj.com/articles/2000/0012/0012c/0012c.htm

    --Paul

  5. #5
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    Originally posted by slscus
    Thanks, but if I did this way, I cann't access the data by using
    pArray[i][j][k]. ???
    True, you'll have to do pArray[((i * ydim) + j) * zdim + k];
    It's a bit uglier, but not a real problem.

    But have a look at the links provided by Paul, you'll get a lot of benefits for using that. Not the least is that you'll get an introduction to STL if you don't know it already

  6. #6
    Join Date
    Oct 2002
    Location
    Shanghai China
    Posts
    3
    quote from Yves:

    code:--------------------------------------------------------------------------------
    long *pArray;
    long xdim, ydim, zdim;

    pArray = (long *) calloc(sizeof(long) * xdim * ydim * zdim);
    --------------------------------------------------------------------------------


    i think should be
    -------------------------------------------------------------------------------
    const long xdim=NUMBERx,ydim=NUMBERy,zdim=NUMBERz;
    long *pArray=new long[xdim*ydim*zdim];
    ......

    delete [] pArray
    ---------------------------------------------------------------------------------
    Last edited by room_yuy; October 10th, 2002 at 12:13 AM.

  7. #7

    Here....

    Code:
    #include <windows.h>
    #include <iostream>
    #include <stdlib.h>
    #include <time.h>
    
    int main(int _argc,char * _argv[]) throw()
    {
    	srand(::GetTickCount());
    	int *** pPointerToPointerOfPointers = new int **[10];
    	for (int i = 0; i < 10; ++i)
    	{
    		pPointerToPointerOfPointers[i] = new int *[10];
    		for (int x = 0; x < 10; ++x)
    		{
    			pPointerToPointerOfPointers[i][x] = new int(rand());
    		}
    	}
    	for (int p = 0; p < 10; ++p)
    	{
    		for (int q = 0; q < 10; ++q)
    		{
    			std::cout << "The value of at index " << q << " of the index " << p << " is " << *(pPointerToPointerOfPointers[p][q]) << std::endl;
    		}
    	}
    	// deletion
    	for (int r = 0; r < 10; ++r)
    	{
    		for (int s = 0; s < 10; ++s)
    		{
    			delete pPointerToPointerOfPointers[r][s];
    		}
    		delete [] pPointerToPointerOfPointers[r];
    	}
                    delete [] pPointerToPointerOfPointers;
    	return 0;
    }
    That answers the question - using new and delete to accomplish this. (The question was not about std::vector - although, that is a viable solution to what he wants to do - but he asked for new/delete) It is best to answer how to do something, the way the person asks how to do it - that's what he asked for. In this case, it's a question of C++ logic - knowing the answer and understanding how it works will make a better programmer.
    Last edited by JamesSchumacher; October 11th, 2002 at 08:13 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured