• 11033阅读
  • 10回复

QT中new的用法? [复制链接]

上一主题 下一主题
离线zhuhehz
 

只看楼主 倒序阅读 楼主  发表于: 2010-10-28
— 本帖被 XChinux 执行加亮操作(2010-10-29) —
我在一个槽函数里:char *to_sent = new char[10];可是申请不成功,程序已运行到这就报错,为什么呢?

谢谢大家。
离线XChinux

只看该作者 1楼 发表于: 2010-10-28
为什么要这样用呢?没必要吧。
二笔 openSUSE Vim N9 BB10 XChinux@163.com 网易博客 腾讯微博
承接C++/Qt、Qt UI界面、PHP及预算报销系统开发业务
离线zhuhehz

只看该作者 2楼 发表于: 2010-10-28
回 1楼(XChinux) 的帖子
我怕这样char *to_sent;不安全。

原来我申请的比较大char *to_sent = new char[10240];报错,改到char *to_sent = new char[10];还报错。不知为什么?

谢谢总版主。
离线zhuhehz

只看该作者 3楼 发表于: 2010-10-28
回 1楼(XChinux) 的帖子
还有,请教总版主,QT + MinGWE + ECLIPSE可以运行程序,不能调试程序,控制台的提示为“[New Thread 3188.0xa00]
gdb: unknown target exception 0xc0000135 at 0x7c98478e”如何进行设置可以debug程序呢!

QT Creator可以debug的。这个问题:“char *to_sent = new char[10];可是申请不成功,程序已运行到这就报错”就是在QT Creator debug中发现的。在eclipse程序就崩溃了,不能debug没有提示错误。

谢谢了。
离线dbzhang800

只看该作者 4楼 发表于: 2010-10-28
能多贴几行代码,提供点上下文。单纯这条语句,不应该有问题。
离线zhuhehz

只看该作者 5楼 发表于: 2010-10-28
回 4楼(dbzhang800) 的帖子
void MyComTool::manualCom_Sent()
{
    QString user_input;
        char *to_sent = new QChar[10240];
    char sent_buf[2];
        QChar *to_sent_tmp = new QChar[10240];
    int b;
    qint64 char_count;
    user_input = comSentTextEdit->toPlainText();
    to_sent =  user_input.toAscii().data();
    to_sent_tmp = user_input.data();
    char_count = 0;
    already_sent_times++;
    if(!sent_hex_model)
    {

                sent_byte_counter  +=myCom->write(to_sent);
        myCom->waitForBytesWritten(-1);
        comReceiveLabel->setNum (sent_byte_counter);
    }
    else
    {
         while (!to_sent_tmp->isNull())
         {
             b= (int)*to_sent;
            sprintf(sent_buf, "%x", b);
            hex_to_sent[char_count] = sent_buf[0];
            hex_to_sent[char_count + 1]  = sent_buf[1];
            ++to_sent_tmp;
            ++to_sent;
             ++char_count;
             ++char_count;
             if(char_count >= 10240)
             {
                 char_count = 0;
                 QMessageBox warningBox(this);
                QString mypix;
                mypix = ":/erroring.png";
                QPixmap img(mypix);
                warningBox.setIconPixmap(mypix);
                warningBox.setWindowTitle(tr("错误"));
                warningBox.setText(tr("发送数据过长!"));
                warningBox.exec();
                break;
             }
         }
         sent_byte_counter  +=  myCom->write(hex_to_sent, char_count);
         myCom->waitForBytesWritten(-1);
        comReceiveLabel->setNum (sent_byte_counter);
    }
    sentTimesClearLabel->setNum(already_sent_times);
        delete [] to_sent_tmp;
        delete []to_sent;
}

该槽函数是相应发送按钮的,向串口发送数据。
谢谢了!
离线dbzhang800

只看该作者 6楼 发表于: 2010-10-28
Re:回 4楼(dbzhang800) 的帖子
引用第5楼zhuhehz于2010-10-28 12:38发表的 回 4楼(dbzhang800) 的帖子 :
void MyComTool::manualCom_Sent()
{
    QString user_input;
        char *to_sent = new QChar[10240];
    char sent_buf[2];
.......

离线zhuhehz

只看该作者 7楼 发表于: 2010-10-28
回 6楼(dbzhang800) 的帖子
不好意思,我粘贴错了。

void MyComTool::manualCom_Sent()
{
    QString user_input;
    char *to_sent = new char[10240];
    QChar *to_sent_tmp = new QChar[10240];
    char sent_buf[2];

    int b;
    qint64 char_count;
    user_input = comSentTextEdit->toPlainText();
    to_sent =  user_input.toAscii().data();
    to_sent_tmp = user_input.data();
    char_count = 0;
    already_sent_times++;
    if(!sent_hex_model)
    {
        //10.27//while (!to_sent_tmp->isNull())
        //10.27//{
        //10.27//    ++to_sent_tmp;
        //10.27//     ++char_count;
        //10.27//}
        //char_count += 1;
        sent_byte_counter  +=myCom->write(to_sent);
        myCom->waitForBytesWritten(-1);
        comReceiveLabel->setNum (sent_byte_counter);
    }
    else
    {
         while (!to_sent_tmp->isNull())
         {
             b= (int)*to_sent;
            sprintf(sent_buf, "%x", b);
            hex_to_sent[char_count] = sent_buf[0];
            hex_to_sent[char_count + 1]  = sent_buf[1];
            ++to_sent_tmp;
            ++to_sent;
             ++char_count;
             ++char_count;
             if(char_count >= 10240)
             {
                 char_count = 0;
                 QMessageBox warningBox(this);
                QString mypix;
                mypix = ":/erroring.png";
                QPixmap img(mypix);
                warningBox.setIconPixmap(mypix);
                warningBox.setWindowTitle(tr("错误"));
                warningBox.setText(tr("发送数据过长!"));
                warningBox.exec();
                break;
             }
         }
         sent_byte_counter  +=  myCom->write(hex_to_sent, char_count);
         myCom->waitForBytesWritten(-1);
        comReceiveLabel->setNum (sent_byte_counter);
    }
    sentTimesClearLabel->setNum(already_sent_times);
        delete [] to_sent;
       delete [] to_sent_tmp;
}
这样还是不行。
离线浪漫天使
只看该作者 8楼 发表于: 2010-10-28
    to_sent =  user_input.toAscii().data();
    to_sent_tmp = user_input.data();
是什么意图,
指针不是这样玩的吧?

如果 你怀疑 是 char *to_sent = new char[10240];
导致问题的话
你可以在
int b;
的前面加入一句
return;
试试基本就可以确定问题出在什么地方
离线zhuhehz

只看该作者 9楼 发表于: 2010-10-28
回 8楼(浪漫天使) 的帖子
把char *to_sent = new char[10240];改成char *to_sent ;就没问题了。
离线g_tree
只看该作者 10楼 发表于: 2010-11-13
如果怕不安全就初始指向NULL.
快速回复
限100 字节
 
上一个 下一个