指针段错误问题
指针段错误问题...
我已经做了几个星期的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;
对我来说很难理解的是,如何或为什么为已经“存在”的东西分配内存,因为有成员 return_对象 kout
那么,我需要为分配给它的变量分配内存(当然是同一类型),这样说是否正确?
pointer segfault problems...
I've been doing c++ for some weeks meanwhile but i ran again into that issue.
basically i have these classes given. I cant change them. I start with an instance of _ns3__importAuftragResponse kout;
class SOAP_CMAC _ns3__importAuftragResponse
{
public:
ns2__SOAPImportResult *return_;
...
class SOAP_CMAC ns2__SOAPImportResult
{
public:
bool *error;
int *numberOfIgnoreds;
....
My code needs to check for the numberOfIgnoreds
first approach
ns2__SOAPImportResult* imp_result;
imp_result = kout.return_;
int num;
num = *imp_result->numberOfIgnoreds;
or i use
ns2__SOAPImportResult imp_result;
imp_result = *(kout.return_);
int* num;
*num = *imp_result.numberOfIgnoreds;
I mostly get segmentation fault
I know generally what happens at runtime but cant come up with the correct ode. PLease help.
EDIT
made progress thx to your answer, Nawaz , but still need some understanding
ns2__SOAPImportResult * imp_ptr = new ns2__SOAPImportResult;
imp_ptr = kout.return_;
int * num = new (int);
// next line segfaults
*num = *imp_ptr->numberOfIgnoreds;
what's hard for me to understand is, how or why allocate memory for something that is already "there" as there is the member return_ of the object kout
So is it correct to say I need to allocate memory for the variable I assign it to (which is of same type of course)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您很可能没有为您在引用的代码中使用的以下成员分配内存。
除此之外,我没有看到任何可能出错的地方!
确保在使用这些成员(以及程序中的所有其他指针)之前为它们分配内存。您可以使用
new
来分配内存。或者,您也可以使用malloc()
。无论您使用什么,请一致地使用它,并在完成后释放内存,分别使用delete
或free()
!Most likely you've not allocated memory for the following members which you're using in the code you've quoted.
Other than this I don't see anything where things might go wrong!
Make sure you allocate memory for these members (and all other pointers in your program) before using them. You can use
new
to allocate memory. Or alternatively, you can usemalloc()
as well. Whatever you use, use it consistently, and deallocate the memory once you done, usingdelete
orfree()
respectively!这看起来像 gsoap。在这种情况下,您必须使用soap_malloc 来分配您返回的内存。
例如,在 FAQ 页面上,您将找到以下示例:
This looks like gsoap. In that case you must use soap_malloc to allocate memory which you return.
For example on the FAQ page, you will find this example: