Thread: Problem with pointers/strings/memory

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    You're not allocating memory.

    chunk.memory = malloc( 1024 );
    For example.
    Then read up to 1024 bytes into the memory.

    Is your data always '\0' terminated, like a proper C-string should be?
    It needs to be if things like strcpy, strlen, strtok are to work.

    > chunk.memory=&chunk.memory[1];
    This is very bad - never modify a pointer to memory you allocated. You'll find it extremely hard to free that memory in future.
    The most correct thing to do here is use memmove() to shuffle the entire string back one place in memory.
    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.

  2. #2
    Unregistered User
    Join Date
    Jan 2005
    Location
    York, UK
    Posts
    8
    Quote Originally Posted by Salem
    You're not allocating memory.
    chunk.memory = malloc( 1024 );
    For example.
    Then read up to 1024 bytes into the memory.
    The data is created with these functions:
    Code:
    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;
    }
    It's for use with the cURL C Libraries if you know them at all.

    Quote Originally Posted by Salem
    Is your data always '\0' terminated, like a proper C-string should be?
    It needs to be if things like strcpy, strlen, strtok are to work.
    Yes, and as I said, I did try appending a new \0 to the string and then using the functions.
    Quote Originally Posted by Salem
    > chunk.memory=&chunk.memory[1];
    This is very bad - never modify a pointer to memory you allocated. You'll find it extremely hard to free that memory in future.
    The most correct thing to do here is use memmove() to shuffle the entire string back one place in memory.
    Thanks for the tip, this is my first project in C (personal as opposed to homework )

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