Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. C++ Function template help

C++ Function template help

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++graphicsperformancequestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    Indrawati
    wrote on last edited by
    #1

    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!

    A P 2 Replies Last reply
    0
    • I Indrawati

      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!

      A Offline
      A Offline
      Andrew Walker
      wrote on last edited by
      #2

      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

      I 1 Reply Last reply
      0
      • A Andrew Walker

        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

        I Offline
        I Offline
        Indrawati
        wrote on last edited by
        #3

        Hi Thanks for the reply. I've tried supplying the void return value, but I still get the same error.

        1 Reply Last reply
        0
        • I Indrawati

          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!

          P Offline
          P Offline
          Paul Ranson
          wrote on last edited by
          #4

          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 the vector, which seems rather more direct. Paul

          I 1 Reply Last reply
          0
          • P Paul Ranson

            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 the vector, which seems rather more direct. Paul

            I Offline
            I Offline
            Indrawati
            wrote on last edited by
            #5

            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.

            P 1 Reply Last reply
            0
            • I Indrawati

              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.

              P Offline
              P Offline
              Paul Ranson
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups