passing parameter

Discussion in 'C++' started by Eric Kaplan, Apr 4, 2008.

  1. Eric Kaplan

    Eric Kaplan Guest

    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;
    }
    Eric Kaplan, Apr 4, 2008
    #1
    1. Advertising

  2. Eric Kaplan

    Sharad Guest

    "Eric Kaplan" <> wrote in message

    > 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
    Sharad, Apr 4, 2008
    #2
    1. Advertising

  3. Eric Kaplan

    Eric Kaplan Guest

    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


    >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
    >
    Eric Kaplan, Apr 4, 2008
    #3
  4. Eric Kaplan

    Sharad Guest

    "Eric Kaplan" <> wrote in message
    > 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
    Sharad, Apr 4, 2008
    #4
  5. Eric Kaplan wrote:

    > 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
    Michael Oswald, Apr 4, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    517
    Matteo
    Mar 29, 2007
  2. Stuart Redmann
    Replies:
    5
    Views:
    476
    Stuart Redmann
    Dec 14, 2007
  3. Replies:
    5
    Views:
    630
  4. Isti
    Replies:
    2
    Views:
    709
  5. Mister B
    Replies:
    8
    Views:
    543
    Nick Keighley
    Aug 26, 2010
Loading...

Share This Page