0

I have a pointer and when i dereference it, it gives me error. The problem is that the parameter *ns__personRequestResponse

int __ns3__PersonRequest(soap *, _ns1__PersonRequest *ns1__PersonRequest, _ns1__PersonRequestResponse *ns1__PersonRequestResponse)
{
    ns1__PersonRequestResponse->result = 0;
    //ns1__PersonRequestResponse = new _ns1__PersonRequestResponse();
    *ns1__PersonRequestResponse->result = 39; // Error here
    return SOAP_OK;
}

Below is the part of header file created from wsdl that has response parameter.

class _ns1__PersonRequestResponse
{ 
  public:
     /// Element result of type xs:int.
     int*                                 result                         0; ///< Nullable pointer.
     /// A handle to the soap struct that manages this instance (automatically set)
     struct soap                         *soap                          ;
};

I get segmentation fault when i assign a value to an integer variable result. How i can get it working?

Kahn
  • 755
  • 3
  • 11
  • 28

1 Answers1

0

The result field in your structure is a pointer to int. In your code, you first initialize it to 0, then attempt to assign a value through that pointer. But on most systems, that will fail, because the memory at address 0 hasn't been allocated to your program.

The fix is to ensure that result points to valid memory before attempting to assign through that pointer. Exactly how that should happen depends on how that structure is going to be used in your code. One way would be to declare an int variable outside of your function (probably at file scope), then take its address and assign it to result:

int my_result;  // This should be declared OUTSIDE of a function!

int __ns3__PersonRequest(soap *, _ns1__PersonRequest *ns1__PersonRequest, _ns1__PersonRequestResponse *ns1__PersonRequestResponse)
{
    ns1__PersonRequestResponse->result = &my_result;  // Make sure pointer points to valid memory
    *ns1__PersonRequestResponse->result = 39; // Updates the value of my_result
    return SOAP_OK;
}
Jim Lewis
  • 43,505
  • 7
  • 82
  • 96
  • It gives error on this. error: invalid conversion from 'int' to 'int*' [-fpermissive] – Kahn Jun 26 '13 at 18:10
  • @Hesper: Are you sure the "result" field in that structure is really declared as an integer? Perhaps you've got an unnecessary `*` in the structure declaration....you should probably edit your question to include the declaration. – Jim Lewis Jun 26 '13 at 18:34
  • I have now included the part of header file that has definition of ns1__PersonRequestResponse. The star is necessary as its gsoap return type and it should be a pointer. – Kahn Jun 26 '13 at 20:33
  • 1
    @Hesper: It looks like your problem is due to an attempted null pointer assignment. You need to initialize the pointer value to valid memory that belongs to your process; I've updated my answer to show one possible way of doing it. – Jim Lewis Jun 26 '13 at 22:03