It is sample code from http://softwareandfinance.com/CPP_3D_Dynamic_Arrays.html

for creating dynamic arrays. Hope this helps...!

template<class Type>
int Create3D_Array(Type ****pResult, int x, int y, int z)
{
    Type ***p = new Type **[x];
    for(int i = 0; i < x; i++)
    {
        p[i] = new Type *[y];
        for(int j = 0; j < y; j++)
            p[i][j] = new Type[z];
    }
    *pResult = p;
    return x * y * z;
}
 
template<class Type>
int Delete3D_Array(Type ****pResult, int x, int y, int z)
{
    Type ***p = *pResult;
    for(int i = 0; i < x; i++)
        for(int j = 0; j < y; j++)
            delete p[i][j];
 
    for(int i = 0; i < x; i++)
        delete p[i];
    delete p;
    return 0;
}
 
void TestFunc()
{
    int ***pInt = NULL;
    float ***pFloat = NULL;
    Create3D_Array(&pInt, 2, 4, 6);
    Create3D_Array(&pFloat, 2, 3, 5);
 
    int count = 0;
    for(int i = 0; i < 2; i++)
    {
        for(int j = 0; j < 3; j++)
            for(int k = 0; k < 5; k++)
                pFloat[i][j][k] = count += 3;
    }
 
    for(int i = 0; i < 2; i++)
    {
        for(int j = 0; j < 3; j++)
        {
            for(int k = 0; k < 5; k++)
            {
                char buf[512];
                sprintf(buf, "Array[%d][%d][%d] = %.2f\n", i, j, k, 
pFloat[i][j][k]);
                std::cout << buf;
            }
        }
    }
 
    Delete3D_Array(&pFloat, 2, 3, 5);
    Delete3D_Array(&pInt, 2, 4, 6);
}


--- In [email protected], "Gerald Dunn" <dun...@...> wrote:
>
> Admittedly I don't know why it works in one environment and not the other for 
> you. My understanding is you can only reserve memory on the stack with 
> constant values that are known at compile time. In your example you provided:
> 
> int sampleTest = 1;
> Vector3D randomArray[sampleTest];
> 
> Rob was suggesting using a #define because that provides a constant value. 
> The preprocessor would replace 'SAMPLE_SIZE' with (in his example) '1'. Since 
> you've told the compiler where to find the definition of a Vector3D it 
> therefore knows its size, knows there's an array of '1' and can therefore 
> allocate an array of constant size.
> 
> However, it seems your example was just to express what you were trying to 
> accomplish. After reading your last post it seems you're really going for 
> something more like:
> 
> void someFunction(int size) {
>     Vector3D randomArray[size];
> }
> 
>  If you need an array with a dynamic size then you will most likely need:
> 
> void someFunction(int size) {
>     Vector3D *randomArray = (Vector3D*) malloc(size * sizeof(Vector3D));
>     
>     // do stuff ...
>     
>     free((void*) randomArray);
> }
> 
> Granted, that doesn't check '0 < size'. My solution was more C. As for C++ 
> I'm not an expert but if your Vector3D is a class then you will probably need 
> to inialize your array somehow using 'new'.
> 
> I hope this helps. Thanks. Jerry.
> 
>   ----- Original Message ----- 
>   From: Jos Timanta Tarigan 
>   To: [email protected] 
>   Sent: Tuesday, August 03, 2010 2:18 PM
>   Subject: Re: [c-prog] array initialization
> 
> 
>     
>   hi, 
> 
>   the problem is the vlaue is not a constant. its an input from a 
>   SetSampleQuantity(int input) method. so it has to be defined at run time 
> (user 
>   input specifically) . this method called in the beginning whne the program 
> is 
>   called. how can i do that? any way to do that?
>   this is very weird considering it works fine on os x environment.
>   ================================= 
>   http://www.svnstrk.blogspot.com
> 
>   ________________________________
>   From: Rob Richardson <rob.richard...@...>
>   To: [email protected]
>   Sent: Tue, August 3, 2010 7:13:55 PM
>   Subject: RE: [c-prog] array initialization
> 
>   Since you have to rebuild this code every time you change the sampleTest
>   value, you might as well use a copiler constant instead of a constant
>   whose value is not known until run time:
> 
>   #define SAMPLE_SIZE 1
>   Vector3D randomArray[SAMPLE_SIZE];
> 
>   RobR
> 
>   [Non-text portions of this message have been removed]
> 
> 
> 
>   
> 
> [Non-text portions of this message have been removed]
>


Reply via email to