Initializing an array in C++

20 views
Skip to first unread message

Sinead English

unread,
Mar 21, 2012, 6:28:24 AM3/21/12
to egi-progra...@googlegroups.com
Hi all

For any C++ gurus out there, I'm trying to initialize an array within a for loop without success. I'd like to initialize an array (F) of length N*T, where N is constant but T varies with each step of the loop - so ultimately I'd like to get several arrays F for all values of T. I've pasted my test code below, and the error I get is: "variable-sized object 'F' may not be initialized"

From a quick look at C++ help pages, it seems that variable-length arrays are a can of worms; but hopefully there is a simple fix to my code? 

Thanks, Sinead


int main() 

{

 const int T[2] = {5,10};

 const int N = 10;

 

for (int i=0; i<=1; i++)

{

double F[N*T[i]] = {0};

}

return 0;

}

 




Cam Allen

unread,
Mar 21, 2012, 6:48:04 AM3/21/12
to egi-progra...@googlegroups.com
I think you should have a vector of pointers to the arrays you made instead of an array of arrays.


That way you can ensure the ordering of the arrays on access via a sequence loop on the vector and by following the ptr ref you can have an array as big as you want (mem limit of course) at the end of it.

How does this sound?
cam

Cam Allen

unread,
Mar 21, 2012, 6:51:42 AM3/21/12
to egi-progra...@googlegroups.com
Alternatively if ptrs, etc are a bit too much (although to get any goodies out of C++ they are a must!), you can try and use the vector class instead of dynamic arrays.

I.e. have an array of vectors and let the vectors handle the dynamic sizing.
cam

Sinead English

unread,
Mar 21, 2012, 6:55:42 AM3/21/12
to egi-progra...@googlegroups.com
Thanks - I hadn't realised the vector/array distinction; and sounds like a good time to force myself to understand pointers, I'll let you know if I run into any problems! 


--
You received this message because you are subscribed to the Google Groups "EGI programming and R" group.
To view this discussion on the web visit https://groups.google.com/d/msg/egi-programming-and-r/-/zEgKgmJIlHkJ.

To post to this group, send email to egi-progra...@googlegroups.com.
To unsubscribe from this group, send email to egi-programming-...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/egi-programming-and-r?hl=en.

Sinead English

unread,
Mar 21, 2012, 10:48:12 AM3/21/12
to egi-progra...@googlegroups.com
OK I have solved the problem for now using vectors (the quick but less sophisticated solution, not using pointers) - thanks Cam. Code and some useful links below:

#include  <iostream>
#include  <vector>
using namespace std;


int main()
{
    const int T[2] = {5,10};
    const int N = 10;
   
    for (int i=0; i<=1; i++)

        vector<double> F(N*T[i],0);

    return 0;
   
}

# multi-dimensional arrays in C++:
http://www.cplusplus.com/forum/articles/7459/

# vector tutorials
http://www.dreamincode.net/forums/topic/33631-c-vector-tutorial/
http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c4027

Cam Allen

unread,
Mar 21, 2012, 12:23:10 PM3/21/12
to egi-progra...@googlegroups.com
Further to that, i've just added support for storing you're vectors in a vector of pointers and then looping over them and getting them back out for reference:

#include  <iostream>
#include  <vector>

using namespace std;

int main (int argc, const char * argv[])
{
   
const int T[2] = {5,10};
   
const int N = 10;
   
//setup an empty vector of vector<double> ptrs to hold the references to the vectors that get created.
    vector
<vector<double>*> vector_of_ptrs;
 
   
for (int i=0; i<=1; i++) {
       
//create a vector ptr to hold the ref to the newly created dynamic sized vector of doubles
       
// note: values of the first vector will have 0 values & the second will have 1 values depending on the i value from the above for.
        vector
<double>* ref_to_new_vector = new vector<double>(N*T[i],i);

       
//add the ref of the new vector to our ptrs vector. -> changed value of initialise to differentiate the vectors
        vector_of_ptrs
.push_back(ref_to_new_vector);
       
// OR i could write the above as
//        vector_of_ptrs.push_back(new vector<double>(N*T[i],i));
   
}

   
//    now with the created vector references stored in the vector_of_ptrs
   
//    use an iterator (could be a manual index here but iterators do all this work for us!
   
//    http://www.cplusplus.com/reference/stl/vector/begin/
   
//    http://stackoverflow.com/questions/409348/iteration-over-vector-in-c
   
for ( vector<vector<double>*>::iterator it=vector_of_ptrs.begin() ; it < vector_of_ptrs.end(); it++ ) {
       
// dereference the iterator to get the ref of the stored vector
        vector
<double>* the_vector_that_was_stored = *it;
       
//use the -> operator to use the 'type' / cast the var into it's type (a vector) instead of it's current form a ptr!
        cout
<< "The size of the vector is: " << the_vector_that_was_stored->size() << "\n";
       
for ( vector<double>::iterator it_2=the_vector_that_was_stored->begin() ; it_2< the_vector_that_was_stored->end(); it_2++ ) {
           
// you have to cast the values to force them into cout
           
//   OR store them in a typed var (cast is implied)    double the_value = *it_2;
            cout
<< "The value of the item stored in the vector is: " << double(*it_2) << " and the position in the vector is : " <<  
           
int(it_2 - the_vector_that_was_stored->begin()) << "\n";
       
}
   
}  
   
return 0;
}



To post to this group, send email to egi-programming-and-r@googlegroups.com.
To unsubscribe from this group, send email to egi-programming-and-r+unsub...@googlegroups.com.

To post to this group, send email to egi-programming-and-r@googlegroups.com.
To unsubscribe from this group, send email to egi-programming-and-r+unsub...@googlegroups.com.

To post to this group, send email to egi-programming-and-r@googlegroups.com.
To unsubscribe from this group, send email to egi-programming-and-r+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages