【发布时间】:2011-03-18 08:30:20
【问题描述】:
指针段错误问题...
与此同时,我已经做了几个星期的 c++,但我又遇到了这个问题。
基本上我已经给出了这些课程。我不能改变它们。我从_ns3__importAuftragResponse kout;的实例开始
class SOAP_CMAC _ns3__importAuftragResponse
{
public:
ns2__SOAPImportResult *return_;
...
class SOAP_CMAC ns2__SOAPImportResult
{
public:
bool *error;
int *numberOfIgnoreds;
....
我的代码需要检查 numberOfIgnoreds
第一种方法
ns2__SOAPImportResult* imp_result;
imp_result = kout.return_;
int num;
num = *imp_result->numberOfIgnoreds;
或者我使用
ns2__SOAPImportResult imp_result;
imp_result = *(kout.return_);
int* num;
*num = *imp_result.numberOfIgnoreds;
我大多遇到分段错误 我通常知道运行时会发生什么,但无法提出正确的颂歌。请帮忙。
编辑
感谢您的回答,Nawaz 取得了进展,但仍需要一些理解
ns2__SOAPImportResult * imp_ptr = new ns2__SOAPImportResult;
imp_ptr = kout.return_;
int * num = new (int);
// next line segfaults
*num = *imp_ptr->numberOfIgnoreds;
我很难理解的是,如何或为什么为已经“存在”的东西分配内存,因为对象 kout 的成员 return_
所以说我需要为我分配给它的变量分配内存是否正确(当然是同一类型)?
【问题讨论】:
标签: c++ pointers segmentation-fault