passing parameter

E

Eric Kaplan

why calling PostSoapRequest will crash the program?

should I pass string* for start and end?


int post_soap_request(string start, string end)
{
string request = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
+ start + end;
const char* REQUEST_BODY = request.c_str();

ne_set_request_body_buffer(req, REQUEST_BODY,
strlen(REQUEST_BODY));

// some more code

return 0;
}

HRESULT PostSoapRequest(string start, string end) {
return (post_soap_request(start, end) == 0) ? S_OK : E_FAIL;
}
 
S

Sharad

Eric Kaplan said:
why calling PostSoapRequest will crash the program?

should I pass string* for start and end?

Why? What's the intention of your program?
int post_soap_request(string start, string end)
{
string request = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
+ start + end;
const char* REQUEST_BODY = request.c_str();

ne_set_request_body_buffer(req, REQUEST_BODY,
strlen(REQUEST_BODY));

Where is the definition of this function? What is req?
// some more code

return 0;
}

HRESULT PostSoapRequest(string start, string end) {
return (post_soap_request(start, end) == 0) ? S_OK : E_FAIL;
}


Some tips
- Post the minimal code that compiles and demonstrates the crash. People
here can then help you better.
- Use debugger provided for your platform (seems like Windows to me)

Sharad
 
E

Eric Kaplan

the problem is parameter using "string start" instead of

int post_soap_request(string const& start, string const& end)

but i am not very sure the difference

string start / string const& start
 
S

Sharad

Eric Kaplan said:
the problem is parameter using "string start" instead of

int post_soap_request(string const& start, string const& end)

but i am not very sure the difference

string start / string const& start

They mean pass by value, and pass by reference respectively. For user
defined types generally the latter form is preferred.
Through pass by value the caller does not get affected by the changes made
inside the function to the variable. The last statement does not hold true
for pass by reference.

Sharad
 
M

Michael Oswald

Eric said:
int post_soap_request(string start, string end)
{
string request = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
+ start + end;

I would say, this line, followed by the others is the problem:
const char* REQUEST_BODY = request.c_str();

ne_set_request_body_buffer(req, REQUEST_BODY,
strlen(REQUEST_BODY));

You take a pointer to a temporary. This is undefined behaviour.

Try this:

ne_set_request_body_buffer(req, request.c_str(),
request.size());

and remove the cont char*.

hth,
Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top