指针段错误问题

发布于 2024-10-23 14:01:23 字数 1139 浏览 2 评论 0原文

指针段错误问题...

我已经做了几个星期的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

少女净妖师 2024-10-30 14:01:23

您很可能没有为您在引用的代码中使用的以下成员分配内存。

ns2__SOAPImportResult *return_; //in the class _ns3__importAuftragResponse 


int *numberOfIgnoreds;  //in the class  ns2__SOAPImportResult

除此之外,我没有看到任何可能出错的地方!

确保在使用这些成员(以及程序中的所有其他指针)之前为它们分配内存。您可以使用new来分配内存。或者,您也可以使用malloc()。无论您使用什么,请一致地使用它,并在完成后释放内存,分别使用 deletefree()

Most likely you've not allocated memory for the following members which you're using in the code you've quoted.

ns2__SOAPImportResult *return_; //in the class _ns3__importAuftragResponse 


int *numberOfIgnoreds;  //in the class  ns2__SOAPImportResult

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 use malloc() as well. Whatever you use, use it consistently, and deallocate the memory once you done, using delete or free() respectively!

醉生梦死 2024-10-30 14:01:23

这看起来像 gsoap。在这种情况下,您必须使用soap_malloc 来分配您返回的内存。

例如,在 FAQ 页面上,您将找到以下示例:

int ns__itoa(struct soap *soap, int i, char **a)
{ *a = (char*)soap_malloc(soap, 11);
  sprintf(*a, "%d", i);
  return SOAP_OK;
}

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:

int ns__itoa(struct soap *soap, int i, char **a)
{ *a = (char*)soap_malloc(soap, 11);
  sprintf(*a, "%d", i);
  return SOAP_OK;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文