Thread: global string gets corrupted

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    2

    Question global string gets corrupted

    Hi

    I am currently working on a program, that parses a shoutcast stream and gives back some information. i declared three global variables (2 char pointers (stationinfo and station) and one int (metaint)).
    all three variables give back exactly what i want in the size_t stream_process and the size_t hdrprocess, that is the stationname, songtitle and the metaint value.
    but when i try to get access to the variables in my int main the strings are corrupted and printf for example shows some weird stuff, or nothing.
    the integer is fine, it keeps its value.


    Code:
    #include <stdio.h>
    #include <curl/curl.h>
    #include <string.h>
    #include <stdlib.h>
    
    int metaint;
    static char *station;
    static char *stationinfo;
    
    
    /* Function to get the song title parsed */
    size_t stream_process(void *buffer, size_t size, size_t nmemb, void *userp) {
      
        int loop;
        char *metadata, *token;
       
        
        for (loop=0;loop<=nmemb*size;loop++){
            metadata = ((buffer)+loop);
          
          /* the trick with counting the bytes till bytes = metaint 
           * never really worked out....or I never really figured it out ;) 
           * so we just search for the string 'StreamTitle'
           * and then we parse the Title into our info-variable 
           * might be more cpu-intense, but it actually works! */
             
          if (!strncmp(metadata, "StreamTitle", 11)){
            char data[256];
            strncpy(data, metadata+12, 256);
            token = strtok(data, "'");
            stationinfo = (char *) token;
            printf("&#37;s\n", stationinfo);
          }
        }
        
      return size*nmemb;
    }
    
    
    
    /* headerprocessing: */
    size_t hdrprocess( void *ptr, size_t size, size_t nmemb, void *stream){
      
      int loop=0;
      char *data, *token, info[100];
      data = (char *) ptr;
      
      if(!strncmp(data, "icy-name:", 9)) {
        printf("Got Servername!\n");
        
        token = strtok(data, " ");
        while((token = strtok(0, " "))){
          if(loop == 0) {
            strcpy(info, token);
            loop++;
          } else {
          strcat (info, " ");
          strcat (info, token);
          }
        }
        
        station = (char *)info;
        printf("Servername: %s\n", station);
      }
      
      if(!strncmp(data, "icy-metaint:", 12)) {
        printf("Got metaint-data!\n");
        token = strtok(data, " ");
        token = strtok(0, " ");
        if (token) {
          metaint = atoi(token);
        } else {
          metaint = 0;
        }
        printf("metaint = %i\n", metaint);
      }
      
      return size*nmemb; /* we must return the amount of bytes processed */ 
    }
    
    int main (int argc, char **argv){
      
      char *easyhandle;
      char *headerdata;
      char *URL;
      struct curl_slist *headerlist=NULL;
    
    
      if(argc<2){
        printf("\n\nUSAGE:\n\n");
        printf("%s [URL]\n\n", argv[0]);
        return 0;
      }
      
      URL = argv[1];
      
      /* CURL initialization*/
      curl_global_init(CURL_GLOBAL_ALL);
      easyhandle = curl_easy_init();
      
      /* append 'Icy-MetaData:1' to the header, bc we want the metadata*/
         
      headerlist = curl_slist_append(headerlist, "Icy-MetaData:1");
      headerlist = curl_slist_append(headerlist, "ICY 200 OK");
        
      curl_easy_setopt(easyhandle, CURLOPT_HTTPHEADER, headerlist);
      
      /* Where are we connecting to?*/
      curl_easy_setopt(easyhandle, CURLOPT_URL, URL);
      curl_easy_setopt(easyhandle, CURLOPT_TIMEOUT, 2);
      curl_easy_setopt(easyhandle, CURLOPT_HTTP200ALIASES, headerlist);
      curl_easy_setopt(easyhandle, CURLOPT_HEADERFUNCTION, hdrprocess);
      curl_easy_setopt(easyhandle, CURLOPT_WRITEHEADER, headerdata);
     
      /* Don't send data recieved to stdout --> send to processing-function instead*/
      curl_easy_setopt(easyhandle, CURLOPT_WRITEFUNCTION, stream_process); 
      
      
      curl_easy_perform(easyhandle);
      
    
      /*where are the actual values of my variables? */
        printf("%s\n", station);    /*outputs some crap*/
        printf("%s\n", stationinfo);/*here too*/
        printf("%i\n", metaint); /*everything fine here*/
    
    
      curl_slist_free_all(headerlist); /* free the list again */
      curl_global_cleanup();
      return 0;
      
    }

    thx in advance


    PS i know this is probably really sloppy work, but i am far from a pro and needed something that fitted my needs, but couldn't find it.

    PPS i am developing under linux and cygwin

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > station = (char *)info;
    This is a pointer to a local variable.
    As soon as the function returns, the info variable goes out of scope, it's value is lost and station is now pointing at junk.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    2
    thx that worked

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM