Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 常量字符*在while循环的下一次迭代中被覆盖_C++_Qt_Pointers_Vector_Struct - Fatal编程技术网

C++ 常量字符*在while循环的下一次迭代中被覆盖

C++ 常量字符*在while循环的下一次迭代中被覆盖,c++,qt,pointers,vector,struct,C++,Qt,Pointers,Vector,Struct,首先,一切都发生在do{}while循环中的if{}语句中。我有一个包含一些constchar指针的结构。我试图在每次迭代中使用新的字符串值将信息输入临时结构,然后将此结构推送到所述结构的向量中,这样当函数退出时,向量将填充不同的结构对象 do{ if() { sound_device_t newDevice; //<--- Why is this the same mem address each iteration?

首先,一切都发生在do{}while循环中的if{}语句中。我有一个包含一些
constchar
指针的结构。我试图在每次迭代中使用新的字符串值将信息输入临时结构,然后将此结构推送到所述结构的向量中,这样当函数退出时,向量将填充不同的结构对象

do{
   if()
   {
     sound_device_t newDevice;  //<--- Why is this the same mem address each iteration?
                                //I thought it would be destroyed when it's scope was (the if block)


     const char * newPath;
     someFunction(&newPath); //puts a string into newPath
     newDevice.firstString = newPath;   //<-- This works.

     QString otherPath(const char *);
     //...some QString manipulation...//
     newDevice.secondString = otherPath.toLocal8Bit().data();  //<--this doesn't

     vector_of_structs -> push_back(newDevice);

   }
}while (...)
do{
if()
{
sound\u device\u t newDevice;//推回(newDevice);
}
}而(…)
我的印象是push_back将参数struct的值复制到了它自己的版本中。为什么QString会给我带来麻烦?我之所以使用QString,是因为它有一些很好的字符串操作函数(即insert和section),但如果需要的话,我会将其替换为一些有效的函数


我还尝试将QString的数据放入char*中,然后将其strcpy'到struct中,但结果相同。每次迭代都会重写newDevice.secondString

您的代码有几个问题:

  • 无条件的if语句(!)

  • 无效构造:QString otherPath(const char*);您可能需要一个类似于“newPath”的“otherPath”变量

  • 您正在将qt类型与std容器混合。你应该去QStringList上厕所

  • 不必要的指针用法:newDevice.secondString=otherPath.toLocal8Bit().data()

最后一个特别重要,因为您在下一次迭代之前破坏了otherPath。解决方案是在其中使用深度副本

我会这样写:

do {
   if(cond) {
     sound_device_t newDevice;    

     const char * newPath;
     someFunction(&newPath);
     newDevice.firstString = newPath;

     // Get other path
     QString otherPath(otherPath);
     //...some QString manipulation...
     newDevice.secondQString = otherPath;
     // or: strcpy( newDevice.secondString, otherPath.toLocal8Bit().data());

     vector_of_structs->push_back(newDevice);

   }
} while (...)
也就是说,根据您正在尝试做的事情,QT多媒体可能会更好地用于您的声音设备。只要dbus运行,就会有一个QtDBus附加模块。

只有在ByteArray保持不变的情况下才有效。破坏临时性正在改变


换句话说,在行的分号之后
newDevice.secondString=otherPath.toLocal8Bit().data()
toLocal8Bit返回的QByteArray已销毁,存储的数组
delete
d.

感谢所有帮助人员。我只做了一个调整就得到了原始代码:

newDevice.secondString = otherPath.toLocal8Bit().data();
应改为

newDevice.secondString = strdup(otherPath.toLocal8Bit().data());

正如@ratchet freak所建议的那样,这直接进行缓冲区分配。strcpy()无法工作,因为它仍然将newDevice.secondString与QByteArray连接在一起,就像toLatin1().data()一样。

出于好奇:有没有理由不使用append而不是push_back?还有,为什么不使用QStringList呢<代码>QString其他路径(常量字符*)无效,顺便说一句。另外,你能总结一下“不工作”的意思吗?newDevice.secondString是字符*?存储
const char*
供以后使用。。。为什么不使用
std::string
或者在这种情况下使用
QString
someFunction(const char**)
是否在堆上分配?感谢您回复@LaszloPapp。我在std::vector类中没有看到append。我正在看一本书,但我从未听说过一本英文书。:)不过,我尽量避免使用太多的Qt库,因为我想让我的程序尽可能小。我开始使用const char*,因为我正在与DBUS交互,所有函数都使用const char*。在本例中,我尝试使用std::string,但它也不起作用。我不知道堆上是否分配了someFunction()。我怎么检查?(这是一个DBUS函数。)请不要编辑问题以表示问题已解决,或者您已向其添加了新答案。当然,my if()有一个条件:我只是没有把它包括进去,因为我认为它对这个问题不重要。在我发送给QString构造函数的代码中还分配了一个const char*。我只是把
QString其他路径(const char*)以简化操作。第三,我不认为我可以将QString对象发送给const char*。首先需要将其转换为常量char*(这是我认为toLatin1().data()所做的)@MrUser:您没有仔细阅读。newDevice.secondQString,但如果出于某种原因您真的想避免这种情况,strcpy是手动分配的朋友,正如答案中已经提到的那样。。。“你没有仔细阅读,是不是……谢谢你的编辑,”拉兹洛·帕普说。我正在阅读,因为你张贴他们。我很想使用strcpy,但这是不可能的,因为secondString是const char*,strcpy只允许复制到非常量char*。我会尝试将它变成一个非恒定字符*并返回报告。好吧,你可以这样做,但它更难看:
char*tmp_data=new char[otherPath.toLocal8Bit().size()];strcpy(tmp_数据,otherPath.toLocal8Bit().constData());newDevice.secondString=const_cast(tmp_数据)
strcpy(const_cast(newDevice.secondString),otherPath.toLocal8Bit().data())嘿,棘轮怪胎。啊,好的。我在QString文档中看到了类似的内容,但我不确定。关于如何修复它有什么建议吗?@MrUser分配您自己的缓冲区或将QString存储在结构中,我更喜欢存储QString,这样我就不必自己处理内存管理(对firstString也这样做),嗯?Strcpy很好用?strcpy的全部要点是断开连接,但是你真的应该在代码中去掉这种糟糕的设计,即处理低级字符串,IMHO。我想我不明白你的意思。我说strcpy不好用。我使用低级字符串,因为这是DBUS提供给我的接口,正如我前面所说的。不过,谢谢你的持续关注。我很感谢你的跟进。我认为你不明白strcpy的作用;DBU不提供声音设备。