Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:49:05

0001 // name=populate_array_test ; gcc $name.cc -std=c++11 -lstdc++ -o /tmp/$name && /tmp/$name
0002 
0003 #include <iostream>
0004 #include <sstream>
0005 #include <string>
0006 
0007 const int N = 16 ; 
0008 
0009 template<typename T>
0010 std::string desc( const T* tt, int num )
0011 {
0012     std::stringstream ss ; 
0013     for(int i=0 ; i < num ; i++ ) ss << tt[i] << " " ; 
0014     ss << std::endl ; 
0015     std::string str = ss.str(); 
0016     return str ;  
0017 }
0018 
0019 /**
0020 populate_array_0
0021 ------------------
0022 
0023 pointer-to-pointer argument enables the function 
0024 to populate a structure from the calling scope
0025 
0026 **/
0027 
0028 template<typename T>
0029 void populate_array_0( T** arr, int num ) 
0030 {
0031     for(int i=0 ; i < num ; i++) (*arr)[i] = T(i) ; 
0032 }
0033 void test_0()
0034 {
0035     float ss[N] ;
0036     float* ss_ptr = ss ; 
0037     populate_array_0( &ss_ptr, N ); 
0038 
0039     std::cout << "test_0: " << desc(ss, N) ; 
0040 }
0041 
0042 /**
0043 populate_array_1
0044 -------------------
0045 
0046 BUT : no need for pointer-to-pointer
0047 because there us no need to change the location
0048 of that structure. Just need to fill it, so 
0049 one level of indirection is fine. 
0050 
0051 **/
0052 
0053 template<typename T>
0054 void populate_array_1( T* arr, int num ) 
0055 {
0056     for(int i=0 ; i < num ; i++) arr[i] = T(i) ; 
0057 }
0058 void test_1()
0059 {
0060     float ss[N] ;
0061     populate_array_1( ss, N ); 
0062     std::cout << "test_1: " << desc(ss, N) ; 
0063 }
0064 
0065 
0066 int main(int argc, char** argv)
0067 {
0068     test_0(); 
0069     test_1(); 
0070     return 0 ; 
0071 }