Thread: Error, Access Violation, using Templated Class

  1. #1
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034

    Error, Access Violation, using Templated Class

    Hey guys, anyone mind helping, I'd really appreciate it.

    I'm getting an error, access violation according to MSVC6, and I've used cout/endl debugging method to show which method its in.

    Code:
    template< typename Datatype >
    int Array<Datatype>::Resize (USHORT p_sizeChanged)
    {
      Datatype* tempArray = new Datatype[p_sizeChanged];
      if (tempArray == 0) {
        std::cout << "Could not allocate enough memory." << std::endl;
        return -1;
      }
    
      USHORT* reSized;
    
      if (p_sizeChanged < m_size) {
        std::cout << "DEBUG 2" << std::endl;
        *reSized = p_sizeChanged;
      } else if (p_sizeChanged > m_size) {
        std::cout << "DEBUG 2++" << std::endl;
        *reSized = m_size;
      } else {
        std::cout << "Could not figure out the changed array size." << std::endl;
        return -1;
      }
    
      for (USHORT index = 0; index < *reSized; ++index)
        tempArray[index] = m_array[index];
    
      m_size = p_sizeChanged;
    
      if (m_array != 0)
        delete[] m_array;
    
      m_array = tempArray;
      tempArray = 0;
    
      return 0;
    }
    
    int main ()
    {
      Array<int> listLevels(10);
    
      USHORT index;
    
      for (index = 0; index < 10; ++index) {
        listLevels[index] = index;
      }
    
      listLevels.Display ();
    
      std::cout << "DEBUG 1" << std::endl;
    
      std::cin.get ();
    
      //increase the size of the array with Renew.
      listLevels.Resize (30);
    
      std::cout << "DEBUG 3" << std::endl;
    }
    The Array class is in a .h file, and I'm using templates for the class so I had to use a template for the method thats in a different file. It doesnt get further than:
    std::cout << "DEBUG 2++" << std::endl;
    in the Resize method. I'm assuming its angry with me trying to access the private variable of the class: m_size. If it is, that really annoys me.. I mean if its going to pull some violation error because I'm using templates then what the hell is the point of them. Unless an error is occuring because of Datatype being unaccessible? since the method is seperated from the class and its templated.

    Either way.. templates are starting to ........ me off.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Why is reSized a pointer? You never allocate memory for it so it is not pointing to anything valid. You should just make it a plain USHORT. This has nothing to do with templates, just a simple pointer error.

  3. #3
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by Daved
    Why is reSized a pointer? You never allocate memory for it so it is not pointing to anything valid. You should just make it a plain USHORT. This has nothing to do with templates, just a simple pointer error.
    Woah.. didnt even notice that, thanks! Why the hell did I make it a pointer in the first place... odd...

    Topic Closed.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  4. #4
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Don't start losing hope about working with templates. They are your friends and afford you magical opportunities in programming.

    Your error is that you are dereferencing an uninitialized pointer. You declare reSized as a pointer that is pointing at nothing (except that it contains some garbage address) then you dereference it to assign it a value. What I believe you mean to be doing is making reSized point at a variable. So instead of *reSized = someval; you should be saying reSized = &someval;, setting the pointer to the address of an existing variable.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Dae already found the error.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    *this
    Join Date
    Mar 2005
    Posts
    498
    Quote Originally Posted by dwks
    Dae already found the error.
    That was a pointless post...And so is this one.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  2. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  3. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  4. Using a templated object in another class
    By harry_p in forum C++ Programming
    Replies: 3
    Last Post: 08-18-2002, 11:35 PM