CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Aug 2019
    Posts
    72

    corrupted string

    Hi,
    string s;
    Code:
    function(char* GetChArr);
    function(&s[0])
    what is the side effect of this?. s becomes nonassignable after return.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: corrupted string

    Please provide full example code that compiles and shows the issue. What does function() do with GetChArr? If it gets chars and puts them into the memory pointed to by GetChArr and s is of type std::string, then you can only get .size(s) chars. You can't exceed this value. If the number of chars entered is less than s.size() then the remainder will not be changed. In all cases s.size() doesn't change.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Aug 2019
    Posts
    72

    Re: corrupted string

    I have no access to the code for the function1. it is part of a library with no source.

    when I pass:
    char c[20];
    function1(c);

    then s = std::string(c),
    then i can use the s inside a map to find a key.
    if I use the s inside the map array to find a key it won't work. s inside the map become empty. In terms of making it work, it works that way but not sure what is happening.
    sorry, if it is not clear.
    Last edited by @EE@; October 13th, 2022 at 02:11 AM.

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: corrupted string

    Depending upon what function() does and the size of s that is passed, this may have undefined behaviour. You may need to resize s first? What does function() do? As I mentioned above, if function() changes the contents passed then it cannot add more then s.size() chars.

    In the code in post #3 function operates on a char array for up to 19 chars (plus the null terminator) which is fine (for a max of 19). This c-style char array is then assigned to a type std::string which again is fine.

    As you can't change function() I suspect that the code in post #3 is how you'll need to use it. Or re-write the functionality of function() to work with a ref to std::string.

    Does function() have a return value - or is it void? If function add chars to the given memory how does function() know how much memory has been allocated - as you don't seem to be passing a size to function()?

    Without knowing what function() does or seeing it's code it seems that function() is ill-defined and is a prime candidate for a 'buffer overflow' memory error.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured