C++ Function template help
-
Hi I have created a template function as follows: template perform(int iTotal) { vector vec; for(int i = 0; i < iTotal; i++) { vec.push_back(T()); } } and I call the function in main() using the following: perform(1000); The program compiles and links without any error, but when I run it, I always get the message: Instruction at "0x...." referenced memory at "0x...". The memory could not be written! Could someone tell me what I did wrong, and how I can rectify it? BTW, if I changed the function definition to template perform(int iTotal, T NoUse); where NoUse is not used at all in the function, the program runs perfectly. My only gripe is that the solution does not seem elegant. Thanks!
-
Hi I have created a template function as follows: template perform(int iTotal) { vector vec; for(int i = 0; i < iTotal; i++) { vec.push_back(T()); } } and I call the function in main() using the following: perform(1000); The program compiles and links without any error, but when I run it, I always get the message: Instruction at "0x...." referenced memory at "0x...". The memory could not be written! Could someone tell me what I did wrong, and how I can rectify it? BTW, if I changed the function definition to template perform(int iTotal, T NoUse); where NoUse is not used at all in the function, the program runs perfectly. My only gripe is that the solution does not seem elegant. Thanks!
Not sure if this will help, however try supplying a return value to prevent the compiler from defaulting it to int. This behaviour was only supported in older compilers (I'm assuming you're using VC6)
template <typename T> void perform(int iTotal) { vector<T> vec; for(int i = 0; i < iTotal; i++) { vec.push_back(T()); } }
Make sure the constructor for T isn't doing anything it shouldn't - this won't be a problem if you're using double's however.
If you can keep you head when all about you Are losing theirs and blaming it on you; If you can dream - and not make dreams your master; If you can think - and not make thoughts you aim; Yours is the Earth and everything that's in it. Rudyard Kipling
-
Not sure if this will help, however try supplying a return value to prevent the compiler from defaulting it to int. This behaviour was only supported in older compilers (I'm assuming you're using VC6)
template <typename T> void perform(int iTotal) { vector<T> vec; for(int i = 0; i < iTotal; i++) { vec.push_back(T()); } }
Make sure the constructor for T isn't doing anything it shouldn't - this won't be a problem if you're using double's however.
If you can keep you head when all about you Are losing theirs and blaming it on you; If you can dream - and not make dreams your master; If you can think - and not make thoughts you aim; Yours is the Earth and everything that's in it. Rudyard Kipling
-
Hi I have created a template function as follows: template perform(int iTotal) { vector vec; for(int i = 0; i < iTotal; i++) { vec.push_back(T()); } } and I call the function in main() using the following: perform(1000); The program compiles and links without any error, but when I run it, I always get the message: Instruction at "0x...." referenced memory at "0x...". The memory could not be written! Could someone tell me what I did wrong, and how I can rectify it? BTW, if I changed the function definition to template perform(int iTotal, T NoUse); where NoUse is not used at all in the function, the program runs perfectly. My only gripe is that the solution does not seem elegant. Thanks!
Do you have a complete compilable example to post? Your function as it exists is obviously pointless, and even if it weren't is equivalent to calling
resize
on thevector
, which seems rather more direct. Paul -
Do you have a complete compilable example to post? Your function as it exists is obviously pointless, and even if it weren't is equivalent to calling
resize
on thevector
, which seems rather more direct. Paul -
Hi My intention was to analyze the performance of STL vector insertion, how long does it take to insert e.g. 10 million doubles, ints, strings, etc., hence the code.
I think your GPF is a consequence of the compiler rather than the code. But I don't think you're going to learn anything interesting about vector per se from this type of test. The dominant factor will be allocation and reallocation. Any real usage where you know how big you want the vector would call reserve first. And if you want to get to very large arrays of ordinary types then reverting to new/delete and manual management of length is probably going to be notably more efficient. Paul