[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: A very strange problem in C code.
- From: Sean Conner <sean@...>
- Date: Thu, 6 May 2010 17:21:29 -0400
It was thus said that the Great Aurora once stated:
> Dear,
>
> there is a strange problem in my C code, as below:
>
> program:
>
> printf("---%p\n", soap->header);
> soap_header(soap);
> printf("---%p\n", soap->header);
>
> soap_header function:
>
> void soap_header(struct soap *soap)
> {
> printf("soap_header enter %p\n", soap->header);
> if(!soap->header)
> {
> printf("soap_header in IF %p\n", soap->header);
> if((soap->header = (struct SOAP_ENV__Header*)soap_malloc(soap,
> sizeof(struct SOAP_ENV__Header))))
The if statement is assigning soap->header to the result of soap_malloc(),
and testing that assignment against NULL. Since a pointer is being passed in to
soap_header(), that function can change the underlying structure being
pointed to by the passed in pointer.
The code fragment could be rewritten as:
soap->header = (struct SOAP_ENV__Header *)soap_malloc(soap,sizeof(struct SOAP_ENV_Header));
if (soap->header != NULL)
soap_default_SOAP_ENV__Header(soap,soap->header);
Further more, soap_malloc() may end up changing the structure pointed to by
soap, but you would have to check the source code to make sure.
-spc (Sorry for the off-topic reply ... )