local string always corrupt
-
This works fine for me, so there is something more going on. You'll have to share some more information. What does the debugger say for the address and the data contained in the array?
Also how do you know
pr
is not accessible? You never output it via qdebug or anything else.I tried this code (granted I used qt5 since I don't have 4 installed) but it worked fine.
-
@rohit713 said in local string always corrupt:
value of string becomes not accessible.
Value of which string? pr?
Why notmemset(pr, 0, 40);
?
-
This works fine for me, so there is something more going on. You'll have to share some more information. What does the debugger say for the address and the data contained in the array?
Also how do you know
pr
is not accessible? You never output it via qdebug or anything else.I tried this code (granted I used qt5 since I don't have 4 installed) but it worked fine.
@ambershark yes its work fine in qt 5 but my problem occur in qt 4 .ijl is coruupetd debugger shows the locale value not accesible while we copy this to pr its crash .
-
@rohit713 But what do you mean by "not accessible"? Was it accessible before strcpy?
Note this (from http://doc.qt.io/qt-5/qstring.html#toLocal8Bit):
"The returned byte array is undefined if the string contains characters not supported by the local 8-bit encoding." -
You do not check if ijl.toLocal8Bit() is shorter than 40 bytes.
-
@rohit713 But what do you mean by "not accessible"? Was it accessible before strcpy?
Note this (from http://doc.qt.io/qt-5/qstring.html#toLocal8Bit):
"The returned byte array is undefined if the string contains characters not supported by the local 8-bit encoding." -
@ambershark yes its work fine in qt 5 but my problem occur in qt 4 .ijl is coruupetd debugger shows the locale value not accesible while we copy this to pr its crash .
@rohit713 As @Christian-Ehrlicher said: you implemented a nice example for stack overflow :-)
You should use strncpy. -
@rohit713 As @Christian-Ehrlicher said: you implemented a nice example for stack overflow :-)
You should use strncpy. -
@rohit713 As @Christian-Ehrlicher said: you implemented a nice example for stack overflow :-)
You should use strncpy. -
@rohit713 said in local string always corrupt:
@jsulm can u share the stack overflow link
Lol he means an actual stack overflow, i.e. you have the potential for overrunning the memory in your buffer with how you used
strcpy
andstrncpy
. Not stackoverflow.com the website. ;)You also don't leave room for the null terminator (or copy it). So you're resulting string if used in any string function is almost guaranteed to crash, at least in release mode. I don't think either of these are your actual issue though.
before strcpy it contain "risk" after calling strcpy it gives not accesible.
What does that mean? What gives not accessible? The debugger? If it was truly not accessible then it would cause a crash. After it's "not accessible" can you output it via qdebug? My guess here is QString is just reallocating it's internal memory and you are now watching the wrong memory location.
If you could show us the backtrace from the crash log that would help. Like I said I tested the code and it worked. I realize Qt5 is different that 4, but that string memory should be good during your copy even in Qt4. It worked in 5 I suspect if I had easy access to Qt4 it would work there too. Can you give us more information on what the actual problem is? "Not accessible" doesn't mean much.