Thread: pointer problems

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    22

    pointer problems

    I have two functions:
    void getinfo(char *test) // test is initialised with some text
    void setinfo(char *test) // the value gained from getinfo is supplied here

    eg:

    // first I get the information
    char *info = NULL;
    getinfo(&info);

    // then i use the information
    setinfo(info);

    // the output that setinfo() gives me appears garbled
    info = !@h^


    this isn't the exact code, but i don't want to make the problem specific to what I'm doing. I'm assuming that there is something wrong with my syntax and not the logic?

    is this totally confusing? should i paste the original code?

  2. #2
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    post the code for getinfo. I have a pretty good idea what you're doing wrong, but I'd rather see the code first.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    22
    the code is basically trying to use libcurl to open a web page and then
    after all the redirects are done, open the page that was opened last.

    // this opens the page
    curl_easy_setopt(curl, CURLOPT_URL, "http://www.google.com/");
    curl_easy_perform(curl);

    // curl_easy_getinfo() fetches the final URL and stores it in lasturl
    // lasturl needs to be of type char *
    char *lasturl = NULL;
    curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &lasturl);

    // i then want to use lasturl to open the same page again
    // again lasturl needs to be of type char *
    curl_easy_setopt(curl, CURLINFO_URL, lasturl);
    curl_easy_perform(curl);

    //however the second call to curl_easy_perform() failes
    //if i manually set lasturl like so
    lasturl = "http://www.google.com/"
    // it works correctly

    hope that helps

  4. #4
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by shiver
    // curl_easy_getinfo() fetches the final URL and stores it in lasturl
    // lasturl needs to be of type char *
    char *lasturl = NULL;
    curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &lasturl);
    your problem is here. You need to allocate some storage for getinfo to write into. something like this should work.

    Code:
    char lasturl[100];
    curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &lasturl);
    you should also check the return value to make sure getinfo succeeds.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> lasturl needs to be of type char *

    It looks like it should be a pointer to a char*, or char**. Based on a quick glance at some libcurl documentation I would think your original code for the get is correct. It sounds like the function sets the pointer to the appropriate memory containing the url. Perhaps you can cout lasturl at that point to be sure.

    >> curl_easy_setopt(curl, CURLINFO_URL, lasturl);

    Are you sure this isn't supposed to be CURLOPT_URL instead of CURLINFO_URL?

  6. #6
    Registered User
    Join Date
    Jun 2006
    Posts
    22
    Ok, I rewrote everything. If you check in the code below, you'll see the output I get.

    Code:
    #include <iostream>
    #include "curl/curl.h"
    
    using namespace std;
    
    int main()
    {
        CURL *curl_handle;
        CURLcode result;
    
        curl_handle = curl_easy_init();
    
        curl_easy_setopt(curl_handle, CURLOPT_URL, "http://www.google.com/");
        curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, true);
        curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, true);
        curl_easy_setopt(curl_handle, CURLOPT_AUTOREFERER, true);
        curl_easy_perform(curl_handle);
    
        char *lasturl = NULL;
        result = curl_easy_getinfo(curl_handle, CURLINFO_EFFECTIVE_URL, &lasturl);
    
        /*
            here is the output:
    
            LastURL: http://www.google.co.nz/
            * <url> malformed
            * URL using bad/illegal format or missing URL
            * Closing connection #0
            * Closing connection #1
        */
        if ((result == CURLE_OK) && lasturl) {
            cout << "LastURL: " << lasturl << endl;
    
            curl_easy_setopt(curl_handle, CURLOPT_URL, lasturl);
            curl_easy_perform(curl_handle);
        }
    
        curl_easy_cleanup(curl_handle);
    	return 0;
    }
    Now if I set lasturl manually, like so:

    Code:
    lasturl = "http://www.google.co.nz/";
    It works perfectly? I still think this is a syntax issue though?

  7. #7
    Registered User
    Join Date
    Jun 2006
    Posts
    22
    Ok, problem solved.
    It appears that I needed to cpy the info held in lasturl elsewhere as it is soon destroyed by libcurl.

    thanks for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with passing an array of structures by pointer
    By raptor1770 in forum C Programming
    Replies: 9
    Last Post: 11-29-2008, 11:01 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. base class pointer problems
    By ... in forum C++ Programming
    Replies: 3
    Last Post: 11-16-2003, 11:27 PM