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. Base types and pointer arrays

Base types and pointer arrays

Scheduled Pinned Locked Moved C / C++ / MFC
visual-studiodata-structuresquestion
9 Posts 5 Posters 2 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.
  • Richard Andrew x64R Online
    Richard Andrew x64R Online
    Richard Andrew x64
    wrote on last edited by
    #1

    I'm trying to create an array of pointers to a common base class, but each pointer in the array will point to a different derived class instance. This is a 64-bit application. Here's the critical portion of my code: (Unnecessary detail has been removed.) Intellisense is warning me that the second pointer assignment (to the second element in the array) will cause a buffer overrun. It says, "C6386: Buffer overrun while writing to 'this->m_Members': the writable size is 'this->m_MemberCount*8' bytes, but '16' bytes might be written." I have never come across this warning before, and I wanted to ask the community if there is something obviously wrong with the code that I'm not seeing.

        m\_MemberCount = 7;
    
        CGsUCharType\* m\_s\_b1 = new CGsUCharType();  // All of these "Type" classes are derived from CGsTypeBase
    	CGsUCharType\* m\_s\_b2 = new CGsUCharType();
    	CGsUCharType\* m\_s\_b3 = new CGsUCharType();
    	CGsUCharType\* m\_s\_b4 = new CGsUCharType();
    
    	CGsUShortType\* m\_s\_w1 = new CGsUShortType();
    	CGsUShortType\* m\_s\_w2 = new CGsUShortType();
    	CGsULongType\* m\_S\_addr = new CGsULongType();
    
    	CGsTypeBase\*\* m\_Members = new CGsTypeBase\*\[m\_MemberCount\]; // Is this the correct way to create an array of pointers?
    	
    	m\_Members\[0\] = m\_s\_b1;
    	m\_Members\[1\] = m\_s\_b2; // This line has the warning about the buffer overrun.
    	m\_Members\[2\] = m\_s\_b3;
    	m\_Members\[3\] = m\_s\_b4;
    	m\_Members\[4\] = m\_s\_w1;
    	m\_Members\[5\] = m\_s\_w2;
    	m\_Members\[6\] = m\_S\_addr;
    

    The difficult we do right away... ...the impossible takes slightly longer.

    D L CPalliniC J 4 Replies Last reply
    0
    • Richard Andrew x64R Richard Andrew x64

      I'm trying to create an array of pointers to a common base class, but each pointer in the array will point to a different derived class instance. This is a 64-bit application. Here's the critical portion of my code: (Unnecessary detail has been removed.) Intellisense is warning me that the second pointer assignment (to the second element in the array) will cause a buffer overrun. It says, "C6386: Buffer overrun while writing to 'this->m_Members': the writable size is 'this->m_MemberCount*8' bytes, but '16' bytes might be written." I have never come across this warning before, and I wanted to ask the community if there is something obviously wrong with the code that I'm not seeing.

          m\_MemberCount = 7;
      
          CGsUCharType\* m\_s\_b1 = new CGsUCharType();  // All of these "Type" classes are derived from CGsTypeBase
      	CGsUCharType\* m\_s\_b2 = new CGsUCharType();
      	CGsUCharType\* m\_s\_b3 = new CGsUCharType();
      	CGsUCharType\* m\_s\_b4 = new CGsUCharType();
      
      	CGsUShortType\* m\_s\_w1 = new CGsUShortType();
      	CGsUShortType\* m\_s\_w2 = new CGsUShortType();
      	CGsULongType\* m\_S\_addr = new CGsULongType();
      
      	CGsTypeBase\*\* m\_Members = new CGsTypeBase\*\[m\_MemberCount\]; // Is this the correct way to create an array of pointers?
      	
      	m\_Members\[0\] = m\_s\_b1;
      	m\_Members\[1\] = m\_s\_b2; // This line has the warning about the buffer overrun.
      	m\_Members\[2\] = m\_s\_b3;
      	m\_Members\[3\] = m\_s\_b4;
      	m\_Members\[4\] = m\_s\_w1;
      	m\_Members\[5\] = m\_s\_w2;
      	m\_Members\[6\] = m\_S\_addr;
      

      The difficult we do right away... ...the impossible takes slightly longer.

      D Offline
      D Offline
      Daniel Pfeffer
      wrote on last edited by
      #2

      Your code should work (Intellisense is a little confused here), but may I suggest using std::vector instead:

      #include <vector>
      ...

      std::vector<CGsTypeBase*> m_Members(m_MemberCount);

      This will ensure that your array is properly constructed and is destructed when it goes out of scope. You may also wish to look into storing smart pointers (rather than raw pointers) in the vector. This can help ensure that they are destructed at the right time, as well.

      Freedom is the freedom to say that two plus two make four. If that is granted, all else follows. -- 6079 Smith W.

      Richard Andrew x64R 1 Reply Last reply
      0
      • Richard Andrew x64R Richard Andrew x64

        I'm trying to create an array of pointers to a common base class, but each pointer in the array will point to a different derived class instance. This is a 64-bit application. Here's the critical portion of my code: (Unnecessary detail has been removed.) Intellisense is warning me that the second pointer assignment (to the second element in the array) will cause a buffer overrun. It says, "C6386: Buffer overrun while writing to 'this->m_Members': the writable size is 'this->m_MemberCount*8' bytes, but '16' bytes might be written." I have never come across this warning before, and I wanted to ask the community if there is something obviously wrong with the code that I'm not seeing.

            m\_MemberCount = 7;
        
            CGsUCharType\* m\_s\_b1 = new CGsUCharType();  // All of these "Type" classes are derived from CGsTypeBase
        	CGsUCharType\* m\_s\_b2 = new CGsUCharType();
        	CGsUCharType\* m\_s\_b3 = new CGsUCharType();
        	CGsUCharType\* m\_s\_b4 = new CGsUCharType();
        
        	CGsUShortType\* m\_s\_w1 = new CGsUShortType();
        	CGsUShortType\* m\_s\_w2 = new CGsUShortType();
        	CGsULongType\* m\_S\_addr = new CGsULongType();
        
        	CGsTypeBase\*\* m\_Members = new CGsTypeBase\*\[m\_MemberCount\]; // Is this the correct way to create an array of pointers?
        	
        	m\_Members\[0\] = m\_s\_b1;
        	m\_Members\[1\] = m\_s\_b2; // This line has the warning about the buffer overrun.
        	m\_Members\[2\] = m\_s\_b3;
        	m\_Members\[3\] = m\_s\_b4;
        	m\_Members\[4\] = m\_s\_w1;
        	m\_Members\[5\] = m\_s\_w2;
        	m\_Members\[6\] = m\_S\_addr;
        

        The difficult we do right away... ...the impossible takes slightly longer.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        What are the definitions of CGsTypeBase and CGsUCharType? [edit] I built this short program and it compiled and linked successfully:

        struct CGsTypeBase
        {
        char foo[8];
        };
        struct CGsUCharType : CGsTypeBase
        {
        wchar_t foo[8];
        };

        struct CGsUShortType : CGsTypeBase
        {
        uint16_t foo[8];
        };
        struct CGsULongType : CGsTypeBase
        {
        long foo[8];
        };

        int main(
        int argc,
        char* argv[]
        )
        {
        int m_MemberCount = 7;

        CGsUCharType\* m\_s\_b1 = new CGsUCharType();  // All of these "Type" classes are derived from CGsTypeBase
        CGsUCharType\* m\_s\_b2 = new CGsUCharType();
        CGsUCharType\* m\_s\_b3 = new CGsUCharType();
        CGsUCharType\* m\_s\_b4 = new CGsUCharType();
        
        CGsUShortType\* m\_s\_w1 = new CGsUShortType();
        CGsUShortType\* m\_s\_w2 = new CGsUShortType();
        CGsULongType\* m\_S\_addr = new CGsULongType();
        
        CGsTypeBase\*\* m\_Members = new CGsTypeBase\*\[m\_MemberCount\]; // Yes, this is the correct way to create an array of pointers
        
        m\_Members\[0\] = m\_s\_b1;
        m\_Members\[1\] = m\_s\_b2; // No warnings here
        m\_Members\[2\] = m\_s\_b3;
        m\_Members\[3\] = m\_s\_b4;
        m\_Members\[4\] = m\_s\_w1;
        m\_Members\[5\] = m\_s\_w2;
        m\_Members\[6\] = m\_S\_addr;
        
        return 0;
        

        }

        So how does that differ from your code? [/edit]

        Richard Andrew x64R 1 Reply Last reply
        0
        • Richard Andrew x64R Richard Andrew x64

          I'm trying to create an array of pointers to a common base class, but each pointer in the array will point to a different derived class instance. This is a 64-bit application. Here's the critical portion of my code: (Unnecessary detail has been removed.) Intellisense is warning me that the second pointer assignment (to the second element in the array) will cause a buffer overrun. It says, "C6386: Buffer overrun while writing to 'this->m_Members': the writable size is 'this->m_MemberCount*8' bytes, but '16' bytes might be written." I have never come across this warning before, and I wanted to ask the community if there is something obviously wrong with the code that I'm not seeing.

              m\_MemberCount = 7;
          
              CGsUCharType\* m\_s\_b1 = new CGsUCharType();  // All of these "Type" classes are derived from CGsTypeBase
          	CGsUCharType\* m\_s\_b2 = new CGsUCharType();
          	CGsUCharType\* m\_s\_b3 = new CGsUCharType();
          	CGsUCharType\* m\_s\_b4 = new CGsUCharType();
          
          	CGsUShortType\* m\_s\_w1 = new CGsUShortType();
          	CGsUShortType\* m\_s\_w2 = new CGsUShortType();
          	CGsULongType\* m\_S\_addr = new CGsULongType();
          
          	CGsTypeBase\*\* m\_Members = new CGsTypeBase\*\[m\_MemberCount\]; // Is this the correct way to create an array of pointers?
          	
          	m\_Members\[0\] = m\_s\_b1;
          	m\_Members\[1\] = m\_s\_b2; // This line has the warning about the buffer overrun.
          	m\_Members\[2\] = m\_s\_b3;
          	m\_Members\[3\] = m\_s\_b4;
          	m\_Members\[4\] = m\_s\_w1;
          	m\_Members\[5\] = m\_s\_w2;
          	m\_Members\[6\] = m\_S\_addr;
          

          The difficult we do right away... ...the impossible takes slightly longer.

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #4

          A wild guess: the intellisense does not think (like, instead, you assume) that m_MemberCount is equal to 7.

          "In testa che avete, Signor di Ceprano?" -- Rigoletto

          In testa che avete, signor di Ceprano?

          Richard Andrew x64R 1 Reply Last reply
          0
          • D Daniel Pfeffer

            Your code should work (Intellisense is a little confused here), but may I suggest using std::vector instead:

            #include <vector>
            ...

            std::vector<CGsTypeBase*> m_Members(m_MemberCount);

            This will ensure that your array is properly constructed and is destructed when it goes out of scope. You may also wish to look into storing smart pointers (rather than raw pointers) in the vector. This can help ensure that they are destructed at the right time, as well.

            Freedom is the freedom to say that two plus two make four. If that is granted, all else follows. -- 6079 Smith W.

            Richard Andrew x64R Online
            Richard Andrew x64R Online
            Richard Andrew x64
            wrote on last edited by
            #5

            Thank you for taking the time to respond. I will consider using vector instead.

            The difficult we do right away... ...the impossible takes slightly longer.

            1 Reply Last reply
            0
            • L Lost User

              What are the definitions of CGsTypeBase and CGsUCharType? [edit] I built this short program and it compiled and linked successfully:

              struct CGsTypeBase
              {
              char foo[8];
              };
              struct CGsUCharType : CGsTypeBase
              {
              wchar_t foo[8];
              };

              struct CGsUShortType : CGsTypeBase
              {
              uint16_t foo[8];
              };
              struct CGsULongType : CGsTypeBase
              {
              long foo[8];
              };

              int main(
              int argc,
              char* argv[]
              )
              {
              int m_MemberCount = 7;

              CGsUCharType\* m\_s\_b1 = new CGsUCharType();  // All of these "Type" classes are derived from CGsTypeBase
              CGsUCharType\* m\_s\_b2 = new CGsUCharType();
              CGsUCharType\* m\_s\_b3 = new CGsUCharType();
              CGsUCharType\* m\_s\_b4 = new CGsUCharType();
              
              CGsUShortType\* m\_s\_w1 = new CGsUShortType();
              CGsUShortType\* m\_s\_w2 = new CGsUShortType();
              CGsULongType\* m\_S\_addr = new CGsULongType();
              
              CGsTypeBase\*\* m\_Members = new CGsTypeBase\*\[m\_MemberCount\]; // Yes, this is the correct way to create an array of pointers
              
              m\_Members\[0\] = m\_s\_b1;
              m\_Members\[1\] = m\_s\_b2; // No warnings here
              m\_Members\[2\] = m\_s\_b3;
              m\_Members\[3\] = m\_s\_b4;
              m\_Members\[4\] = m\_s\_w1;
              m\_Members\[5\] = m\_s\_w2;
              m\_Members\[6\] = m\_S\_addr;
              
              return 0;
              

              }

              So how does that differ from your code? [/edit]

              Richard Andrew x64R Online
              Richard Andrew x64R Online
              Richard Andrew x64
              wrote on last edited by
              #6

              Yes, my code compiles and links successfully too. I guess it's just intellisense that has a problem. Thank you for your response.

              The difficult we do right away... ...the impossible takes slightly longer.

              1 Reply Last reply
              0
              • CPalliniC CPallini

                A wild guess: the intellisense does not think (like, instead, you assume) that m_MemberCount is equal to 7.

                "In testa che avete, Signor di Ceprano?" -- Rigoletto

                Richard Andrew x64R Online
                Richard Andrew x64R Online
                Richard Andrew x64
                wrote on last edited by
                #7

                That's what I think as well. I'm glad to know someone else has the same idea. I did come across a few posts about that warning being incorrect.

                The difficult we do right away... ...the impossible takes slightly longer.

                1 Reply Last reply
                0
                • Richard Andrew x64R Richard Andrew x64

                  I'm trying to create an array of pointers to a common base class, but each pointer in the array will point to a different derived class instance. This is a 64-bit application. Here's the critical portion of my code: (Unnecessary detail has been removed.) Intellisense is warning me that the second pointer assignment (to the second element in the array) will cause a buffer overrun. It says, "C6386: Buffer overrun while writing to 'this->m_Members': the writable size is 'this->m_MemberCount*8' bytes, but '16' bytes might be written." I have never come across this warning before, and I wanted to ask the community if there is something obviously wrong with the code that I'm not seeing.

                      m\_MemberCount = 7;
                  
                      CGsUCharType\* m\_s\_b1 = new CGsUCharType();  // All of these "Type" classes are derived from CGsTypeBase
                  	CGsUCharType\* m\_s\_b2 = new CGsUCharType();
                  	CGsUCharType\* m\_s\_b3 = new CGsUCharType();
                  	CGsUCharType\* m\_s\_b4 = new CGsUCharType();
                  
                  	CGsUShortType\* m\_s\_w1 = new CGsUShortType();
                  	CGsUShortType\* m\_s\_w2 = new CGsUShortType();
                  	CGsULongType\* m\_S\_addr = new CGsULongType();
                  
                  	CGsTypeBase\*\* m\_Members = new CGsTypeBase\*\[m\_MemberCount\]; // Is this the correct way to create an array of pointers?
                  	
                  	m\_Members\[0\] = m\_s\_b1;
                  	m\_Members\[1\] = m\_s\_b2; // This line has the warning about the buffer overrun.
                  	m\_Members\[2\] = m\_s\_b3;
                  	m\_Members\[3\] = m\_s\_b4;
                  	m\_Members\[4\] = m\_s\_w1;
                  	m\_Members\[5\] = m\_s\_w2;
                  	m\_Members\[6\] = m\_S\_addr;
                  

                  The difficult we do right away... ...the impossible takes slightly longer.

                  J Offline
                  J Offline
                  jschell
                  wrote on last edited by
                  #8

                  Richard Andrew x64 wrote:

                  CGsTypeBase** m_Members = new CGsTypeBase*[m_MemberCount]; // Is this the correct way to create an array of pointers?

                  As others have pointed out the your solution works because of the way the memory lays down. In the above you have a declaration of a variable which has a type. And you also give the variable a value. But the declaration of the variable is a pointer. Just a pointer. Not a pointer to an array. So when intellisense sees you use it as an array it goes basically 'pointer + 1' isn't going to work. It does works because of the value and not the declaration. I believe there is a declaration form that allows you to specify the form that would make intellisense happy but been a long time since I attempted C++ so I will leave it to someone else to come up with the form that makes the array specific.

                  Richard Andrew x64R 1 Reply Last reply
                  0
                  • J jschell

                    Richard Andrew x64 wrote:

                    CGsTypeBase** m_Members = new CGsTypeBase*[m_MemberCount]; // Is this the correct way to create an array of pointers?

                    As others have pointed out the your solution works because of the way the memory lays down. In the above you have a declaration of a variable which has a type. And you also give the variable a value. But the declaration of the variable is a pointer. Just a pointer. Not a pointer to an array. So when intellisense sees you use it as an array it goes basically 'pointer + 1' isn't going to work. It does works because of the value and not the declaration. I believe there is a declaration form that allows you to specify the form that would make intellisense happy but been a long time since I attempted C++ so I will leave it to someone else to come up with the form that makes the array specific.

                    Richard Andrew x64R Online
                    Richard Andrew x64R Online
                    Richard Andrew x64
                    wrote on last edited by
                    #9

                    Thanks for your input. I appreciate you taking the time to post.

                    The difficult we do right away... ...the impossible takes slightly longer.

                    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