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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Access unallocated memory...

Access unallocated memory...

Scheduled Pinned Locked Moved C / C++ / MFC
questionperformancehelp
3 Posts 2 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.
  • N Offline
    N Offline
    nmhuy
    wrote on last edited by
    #1

    Hi guys, I've got a problem as follow. Suppose we have a string class named MyString. This class has a constructor to construct a MyString object base on a char * like this: class MyString { private: char *m_pData; int *m_iLength; public: MyString(char *s) { m_iLength = strlen(s); m_pData = new char[m_iLength + 1]; strcpy(m_pData, s); } }; And we have a main function like this: void main() { char *p = new char[5]; MyString s(p); } The question is: the p pointer is allocated only 5 char in the memory, but the strlen function can return a value bigger than 5 (depend on where it finds the '\0' char in the memory of pointer). So the MyString object can access unallocated memory. Does it safe? How can we solve this problem? Best regards.

    Lan hue sau ai lan hue heo Lan hue sau doi trong heo ngoai tuoi

    R N 2 Replies Last reply
    0
    • N nmhuy

      Hi guys, I've got a problem as follow. Suppose we have a string class named MyString. This class has a constructor to construct a MyString object base on a char * like this: class MyString { private: char *m_pData; int *m_iLength; public: MyString(char *s) { m_iLength = strlen(s); m_pData = new char[m_iLength + 1]; strcpy(m_pData, s); } }; And we have a main function like this: void main() { char *p = new char[5]; MyString s(p); } The question is: the p pointer is allocated only 5 char in the memory, but the strlen function can return a value bigger than 5 (depend on where it finds the '\0' char in the memory of pointer). So the MyString object can access unallocated memory. Does it safe? How can we solve this problem? Best regards.

      Lan hue sau ai lan hue heo Lan hue sau doi trong heo ngoai tuoi

      R Offline
      R Offline
      Roger Stoltz
      wrote on last edited by
      #2

      nmhuy wrote:

      So the MyString object can access unallocated memory. Does it safe?

      As long as you don't write to the memory you haven't allocated, you're fine as long as strlen() finds a '\0' char within the virtual address space assigned to the process. You won't get an access violation by reading addresses on the heap even if the memory hasn't been allocated.

      nmhuy wrote:

      How can we solve this problem?

      Well, it depends on how you want it to work. Probably you can guess a maximum length of the strings and provide that as a default parameter to the constructor, e.g:

      MyString( const char* pStr, int nMaxLength = 256 );

      If the calculated length of the string exceeds the nMaxLength you could either treat is as an error and not copy the string and possibly throw an exception, or only copy nMaxLength chars of it. You would also have the possibility to make a new guess for the maximum string length for each string object constructed if you need to. BTW, in your example you've declared the length member as a pointer to an integer... I assume that it's really an integer in your code.


      "It's supposed to be hard, otherwise anybody could do it!" - selfquote
      "High speed never compensates for wrong direction!" - unknown

      1 Reply Last reply
      0
      • N nmhuy

        Hi guys, I've got a problem as follow. Suppose we have a string class named MyString. This class has a constructor to construct a MyString object base on a char * like this: class MyString { private: char *m_pData; int *m_iLength; public: MyString(char *s) { m_iLength = strlen(s); m_pData = new char[m_iLength + 1]; strcpy(m_pData, s); } }; And we have a main function like this: void main() { char *p = new char[5]; MyString s(p); } The question is: the p pointer is allocated only 5 char in the memory, but the strlen function can return a value bigger than 5 (depend on where it finds the '\0' char in the memory of pointer). So the MyString object can access unallocated memory. Does it safe? How can we solve this problem? Best regards.

        Lan hue sau ai lan hue heo Lan hue sau doi trong heo ngoai tuoi

        N Offline
        N Offline
        nmhuy
        wrote on last edited by
        #3

        You're right. I had a typing mistake. m_iLength is an integer. Thanks for you answer.

        Lan hue sau ai lan hue heo Lan hue sau doi trong heo ngoai tuoi

        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