Thread: Problem with pointers/strings/memory

  1. #16
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Exile
    I doubt it. You don't have any constant strings by the sounds of things. The write protected/constant strings are those written in code as "String", for example:
    Code:
    char String[] = "Constant string";
    Except that isn't a constant string. This is:
    Code:
    char * stringliteral = "Don't modify this string. Can change the pointer, but not the string.";
    char notconstant[] = "Can edit this string. Can't change what it points to. Because it's not a pointer.";
    That is to say, as long as you don't run past the end of the second one, you can change all the values inside it.

    Quzah.
    Last edited by quzah; 01-27-2005 at 04:03 PM.
    Hope is the first step on the road to disappointment.

  2. #17
    Unregistered User
    Join Date
    Jan 2005
    Location
    York, UK
    Posts
    8
    OK here's the actual code, I did simplify it a bit initially, but I think this may help if I paste the code proper. (There's a few things I've tried and failed that are commented out)
    Code:
    #define CHARS_IN_ADVERT 714
    struct MemoryStruct {
      char *memory;
      size_t size;
    };
    void stripads(char *s2)
    {
        if (strlen(s2) >CHARS_IN_ADVERT)
        {//chunk.memory is bigger than CHARS_IN_ADVERT characters
            s2[strlen(s2) - CHARS_IN_ADVERT] = '\0';
        }
        else
        {//Not enough characters, so cut them all off I guess
            s2[0] = '\0';
        }
        return *s2;
    }
    
    void *myrealloc(void *ptr, size_t size)
    {
      // There might be a realloc() out there that doesn't like reallocing NULL pointers, so we take care of it here 
      if(ptr)
        return realloc(ptr, size);
      else
        return malloc(size);
    }
    
    size_t WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data)
    {
      register int realsize = size * nmemb;
      struct MemoryStruct *mem = (struct MemoryStruct *)data;
    
      mem->memory = (char *)myrealloc(mem->memory, mem->size + realsize + 1);
      if (mem->memory) {
        memcpy(&(mem->memory[mem->size]), ptr, realsize);
        mem->size += realsize;
        mem->memory[mem->size] = 0;
      }
      return realsize;
    }
    
    struct message getstatus(void)
    {
        char tmp[4],url[255]="",fileline[9000]="",*thedataPtr,thedata[9000]="";
        struct MemoryStruct chunk;
        struct message messages[100];
        int i=0,nlpos=0,error;    
        
        chunk.memory=NULL; // we expect realloc(NULL, size) to work
        chunk.size = 0;    // no data at this point
        
        if(curlhttp) {
            sprintf(url,"%s/messages.php?user=%s&hash=%s",SERVERADDR,usrname,hash);
    #if DEBUG
            printf("Sending data...\n");
    #endif
            curl_easy_setopt(curlhttp, CURLOPT_URL, url);
    #if DEBUG
            printf("Opened %s\nSetting up memory...\n",url);
    #endif
            // send all data to this function
            curl_easy_setopt(curlhttp, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
    
            // we pass our 'chunk' struct to the callback function
            curl_easy_setopt(curlhttp, CURLOPT_WRITEDATA, (void *)&chunk);
    
    #if DEBUG
            printf("Reading into memory...\n",url);
    #endif
            // get it!
            if(0!=(error=curl_easy_perform(curlhttp))) {
                fprintf(stderr,"Error: Getstatus: curl: %s.\n",curl_easy_strerror(error));
                return *messages;
            }
    
            if(strlen(chunk.memory)<6) {
                fprintf(stderr,"Error: Getstatus: Too little, or no data returned.\n");
                return *messages;
            }
            memmove((char *)&chunk.memory[0],(char *)&chunk.memory[1],chunk.size-1); // strip initial newline char.
            stripads(chunk.memory);
    #if DEBUG
            printf("Getstatus: Data Received:\n\"%s\"\n",chunk.memory);
            printf("Getstatus: Checking for errors\n");
    #endif
            sscanf(chunk.memory,"%3s",&tmp);
            printf("tmp: \"%s\"\n",tmp);
            if(0!=strcmp(tmp,"ERR")) {
    #if DEBUG
                printf("Getstatus: We have results!\n");
    
    #endif
                // parse the data
                // tokenize it into individual lines and then into the different parts and create the array of filedetails.
                
    //            nlpos=chrpos(chunk.memory,10,0);
                printf("Getstatus: %p fileline: %s\n",&fileline,fileline);
    //            filelinePtr=fileline;
    //            substr(filelinePtr,chunk.memory,0,nlpos);
            printf("Getstatus: Data Received:\n\"%s\"\n",chunk.memory); 
    //            thedata=malloc(sizeof(*chunk.memory));
                memcpy(thedata,chunk.memory,chunk.size);
                strcat(thedata,'\0');
                thedataPtr = thedata;
    //            sscanf(chunk.memory,"%s",thedata);
    / *************** segfault now after output of string here, but before "here" is printed. *************/
                printf("Thedata: \"%s\"\n",thedata);
                printf("here");
                *fileline=strtok(thedataPtr,10);
    HTH

    details on the curl library are available here: http://curl.haxx.se/libcurl/c/
    Last edited by Mitch; 01-27-2005 at 07:39 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM