local string always corrupt
-
I am developing application in mac os mavericks using Qt Creator 3.0.0. having QT 4.8.4 my problem is when i trying to copy QString in char array value of string becomes not accessible.
for refernce
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QString> #include <QDebug> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); abc("risk"); } MainWindow::~MainWindow() { delete ui; } void MainWindow::abc(QString ijl) { char pr[40]; memset(pr,NULL,40); strcpy(pr,ijl.toLocal8Bit().data()); qDebug()<<ijl.toLocal8Bit().data(); }
-
Hi
What is ijl ?
and can you also show the output ? -
I am developing application in mac os mavericks using Qt Creator 3.0.0. having QT 4.8.4 my problem is when i trying to copy QString in char array value of string becomes not accessible.
for refernce
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QString> #include <QDebug> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); abc("risk"); } MainWindow::~MainWindow() { delete ui; } void MainWindow::abc(QString ijl) { char pr[40]; memset(pr,NULL,40); strcpy(pr,ijl.toLocal8Bit().data()); qDebug()<<ijl.toLocal8Bit().data(); }
@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.
-
@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.