codingdir logo sitemap sitemap |
Home
PHP
C#
C++
ANDROID
JAVA
JAVASCRIPT
PYTHON

Junk after C++ string when returned


By : , Category : c++

You need to null terminate the string. That null character tells the computer when when string ends.

char * copy = new char[ length + 1];
for(int i = 0; i < length; ++i) copy[i] = stringArray[i];
copy[length] = 0; //null terminate it

Just a few things. Declare the int variable in the tighest scope possible for good practice. It is good practice so that unneeded scope wont' be populate, also easier on debugging and kepping track. And drop the 'register' keyword, let the compiler determine what needs to be optimized. Although the register keyword just hints, unless your code is really tight on performance, ignore stuff like that for now.

ReLated :

The problem boils down to:

void * MyThreadCreate(...)
{
    MyThread temp;
    // manipulate temp
    return temp;
}

Outside code is only expecting a void * on the stack so only the first sizeof(void *) bytes of temp are preserved. Try to access anything beyond that and you're getting random garbage on the stack.

Instead, your function should return MyThread like this:

MyThread MyThreadCreate(...)
{
    MyThread temp;
    // manipulate temp
    return temp;
}

Or, if you must return a void * to interface with some other code, then you must allocate the return value on the heap:

void * MyThreadCreate(...)
{
    MyThread *temp = malloc(sizeof(MyThread));
    // manipulate temp
    return temp;
}

But that is a memory leak waiting to happen, so I would return the struct by value if possible.

It is illegal to write into the pointer returned by std::string::c_str(). The documentation for std::string indicates explicitely that this results in undefined behavior. This pointer is originally of type const char *, specifically to express this limitation.

Regarding the results displayed by your program, I guess both strings point to the same empty string buffer (some internal const char empty[] = ""), so that a write to one results in changing the value of all empty strings.

If you really need to use sprintf instead of std::stringstream, you can use char vectors instead of strings:

std::vector<char> buf(10);
sprintf(buf.data(), "%d" ,7);
std::string result = buf.data();

This is because of

if (selection == '1')

Here you are comparing an int with char. That means value of '1' would be the ASCII value of this character which is not 1.

Correct conditional statement should be:-

if (selection == 1)

do it like this

Regex re = New Regex("[x0Ax0D]", RegexOptions.Compiled)

str = re.Replace(str.Trim(), String.Empty)

OR

 string str1="";
 for(int i = 0 ; i < str.lengh ; i++) { 
         if(!char.IsLetter(str[i])
              str1 += str[i];
 }

return str1
Comments


Message :
Login to Add Your Comments .
How to disable registered OpenCL platforms on Windows?
Is Observable broken in Angular 2 Beta 3?
Cross-thread operation not valid when using Invoke
How to pass an IEnumerable or queryable list of properties from Controller to View
Finding numbers after a certain keyword using Python
Pocketsphinx recognizes random phrases in a silence
Passing non-thread-safe objects through thread-safe containers
React scroll nav
BizTalk WCF-BasicHttp Adapter does not allow Empty string for Service Certificate Props
Why property ''cause" of Exception is repeating forever?
Privacy Policy 2017 © codingdir.com All Rights Reserved .