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 <[email protected]>
  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