Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. local string always corrupt
Forum Update on Monday, May 27th 2025

local string always corrupt

Scheduled Pinned Locked Moved Unsolved General and Desktop
23 Posts 6 Posters 3.2k Views
  • 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.
  • R Offline
    R Offline
    rohit713
    wrote on 18 Oct 2018, 06:28 last edited by VRonin
    #1

    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();
    
    
    }
    
    J 1 Reply Last reply 18 Oct 2018, 07:07
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 18 Oct 2018, 06:40 last edited by
      #2

      Hi
      What is ijl ?
      and can you also show the output ?

      R 1 Reply Last reply 22 Oct 2018, 05:56
      2
      • R rohit713
        18 Oct 2018, 06:28

        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();
        
        
        }
        
        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 18 Oct 2018, 07:07 last edited by
        #3

        @rohit713 said in local string always corrupt:

        value of string becomes not accessible.

        Value of which string? pr?
        Why not

        memset(pr, 0, 40);
        

        ?

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        R 1 Reply Last reply 22 Oct 2018, 05:58
        2
        • A Offline
          A Offline
          ambershark
          wrote on 19 Oct 2018, 04:12 last edited by
          #4

          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.

          My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

          R 1 Reply Last reply 22 Oct 2018, 06:00
          4
          • M mrjj
            18 Oct 2018, 06:40

            Hi
            What is ijl ?
            and can you also show the output ?

            R Offline
            R Offline
            rohit713
            wrote on 22 Oct 2018, 05:56 last edited by
            #5

            @mrjj ijl is qstring which is pass as an parameter in function abc ().

            1 Reply Last reply
            0
            • J jsulm
              18 Oct 2018, 07:07

              @rohit713 said in local string always corrupt:

              value of string becomes not accessible.

              Value of which string? pr?
              Why not

              memset(pr, 0, 40);
              

              ?

              R Offline
              R Offline
              rohit713
              wrote on 22 Oct 2018, 05:58 last edited by
              #6

              @jsulm value of ijl string which is passed in function abc().

              J 1 Reply Last reply 22 Oct 2018, 06:01
              0
              • A ambershark
                19 Oct 2018, 04:12

                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.

                R Offline
                R Offline
                rohit713
                wrote on 22 Oct 2018, 06:00 last edited by rohit713
                #7

                @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 .

                J 1 Reply Last reply 22 Oct 2018, 06:04
                0
                • R rohit713
                  22 Oct 2018, 05:58

                  @jsulm value of ijl string which is passed in function abc().

                  J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 22 Oct 2018, 06:01 last edited by
                  #8

                  @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."

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  R 1 Reply Last reply 22 Oct 2018, 06:04
                  0
                  • C Offline
                    C Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on 22 Oct 2018, 06:02 last edited by
                    #9

                    You do not check if ijl.toLocal8Bit() is shorter than 40 bytes.

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    1 Reply Last reply
                    2
                    • J jsulm
                      22 Oct 2018, 06:01

                      @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."

                      R Offline
                      R Offline
                      rohit713
                      wrote on 22 Oct 2018, 06:04 last edited by
                      #10

                      @jsulm before strcpy it contain "risk" after calling strcpy it gives not accesible.

                      1 Reply Last reply
                      0
                      • R rohit713
                        22 Oct 2018, 06:00

                        @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 .

                        J Offline
                        J Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on 22 Oct 2018, 06:04 last edited by jsulm
                        #11

                        @rohit713 As @Christian-Ehrlicher said: you implemented a nice example for stack overflow :-)
                        You should use strncpy.

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        A R 2 Replies Last reply 22 Oct 2018, 06:06
                        1
                        • J jsulm
                          22 Oct 2018, 06:04

                          @rohit713 As @Christian-Ehrlicher said: you implemented a nice example for stack overflow :-)
                          You should use strncpy.

                          A Offline
                          A Offline
                          aha_1980
                          Lifetime Qt Champion
                          wrote on 22 Oct 2018, 06:06 last edited by
                          #12

                          @jsulm

                          Even better: qstrncpy ;)

                          Qt has to stay free or it will die.

                          1 Reply Last reply
                          1
                          • J jsulm
                            22 Oct 2018, 06:04

                            @rohit713 As @Christian-Ehrlicher said: you implemented a nice example for stack overflow :-)
                            You should use strncpy.

                            R Offline
                            R Offline
                            rohit713
                            wrote on 22 Oct 2018, 06:07 last edited by
                            #13

                            @jsulm i also tried strncpy but the same problem occurs .

                            J 1 Reply Last reply 22 Oct 2018, 06:07
                            0
                            • R rohit713
                              22 Oct 2018, 06:07

                              @jsulm i also tried strncpy but the same problem occurs .

                              J Offline
                              J Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on 22 Oct 2018, 06:07 last edited by
                              #14

                              @rohit713 Can you show how you used strncpy?

                              https://forum.qt.io/topic/113070/qt-code-of-conduct

                              R 2 Replies Last reply 22 Oct 2018, 06:12
                              0
                              • J jsulm
                                22 Oct 2018, 06:07

                                @rohit713 Can you show how you used strncpy?

                                R Offline
                                R Offline
                                rohit713
                                wrote on 22 Oct 2018, 06:12 last edited by
                                #15
                                This post is deleted!
                                1 Reply Last reply
                                0
                                • J jsulm
                                  22 Oct 2018, 06:07

                                  @rohit713 Can you show how you used strncpy?

                                  R Offline
                                  R Offline
                                  rohit713
                                  wrote on 22 Oct 2018, 06:13 last edited by rohit713
                                  #16

                                  @jsulm strncpy(pr,ijl.toLocal8Bit().data(),ijl.length());

                                  J A 2 Replies Last reply 22 Oct 2018, 06:16
                                  0
                                  • R rohit713
                                    22 Oct 2018, 06:13

                                    @jsulm strncpy(pr,ijl.toLocal8Bit().data(),ijl.length());

                                    J Offline
                                    J Offline
                                    jsulm
                                    Lifetime Qt Champion
                                    wrote on 22 Oct 2018, 06:16 last edited by
                                    #17

                                    @rohit713 This is same stack overflow as before...

                                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    R 1 Reply Last reply 22 Oct 2018, 06:16
                                    0
                                    • J jsulm
                                      22 Oct 2018, 06:16

                                      @rohit713 This is same stack overflow as before...

                                      R Offline
                                      R Offline
                                      rohit713
                                      wrote on 22 Oct 2018, 06:16 last edited by rohit713
                                      #18

                                      @jsulm can u share the stack overflow link

                                      J A 2 Replies Last reply 22 Oct 2018, 06:17
                                      0
                                      • R rohit713
                                        22 Oct 2018, 06:16

                                        @jsulm can u share the stack overflow link

                                        J Offline
                                        J Offline
                                        jsulm
                                        Lifetime Qt Champion
                                        wrote on 22 Oct 2018, 06:17 last edited by
                                        #19

                                        @rohit713 What link? Think about what will happen if ijl.toLocal8Bit().data() is longer than pr.

                                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                                        1 Reply Last reply
                                        0
                                        • R rohit713
                                          22 Oct 2018, 06:13

                                          @jsulm strncpy(pr,ijl.toLocal8Bit().data(),ijl.length());

                                          A Offline
                                          A Offline
                                          aha_1980
                                          Lifetime Qt Champion
                                          wrote on 22 Oct 2018, 06:17 last edited by
                                          #20

                                          @rohit713

                                          Use qstrncpy(pr, ijl.toLocal8Bit().constData(),sizeof(pr));

                                          Qt has to stay free or it will die.

                                          R 1 Reply Last reply 22 Oct 2018, 06:24
                                          2

                                          6/23

                                          22 Oct 2018, 05:58

                                          topic:navigator.unread, 17
                                          • Login

                                          • Login or register to search.
                                          6 out of 23
                                          • First post
                                            6/23
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved