Thread: how to allocate a known block of the memory to a char *

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    63

    how to allocate a known block of the memory to a char *

    Hi, i have a char * which points to a buffer which i get from the recv function. I want to treat this buffer like a stack for parsing, and after each argument is parsed it is popped from the stack. For example
    Code:
    char parseByte(char * buffer)
    {
        char ret = *buffer;
        free(buffer) //dont want to have the char allocated any more;
        buffer = buffer + 1;
        //now how do i allocate the rest of *buffer back to buffer so the OS doesnt write over them
        return ret;
    }

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    You can't choose when and how you'll free memory that way. Whatever block of memory you malloc(), you need to free() in its entirety.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    You cannot free part of the buffer

    parse buffer till the end
    free it when the work with the whole buffer is finished
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Who said buffer was dynamically allocated anyway?

    say parseByte was called as,
    Code:
    char blah[] = "this is a test";
    parseByte(blah);
    if you free() that you could find your self in strife.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by zacs7 View Post
    Who said buffer was dynamically allocated anyway?

    say parseByte was called as,
    Code:
    char blah[] = "this is a test";
    parseByte(blah);
    if you free() that you could find your self in strife.
    in this case prototype of the parseByte should be
    parseByte(const char* buf);
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Allocate memory inside allocated memory block?
    By Heidi_Nayak in forum C Programming
    Replies: 14
    Last Post: 04-15-2009, 04:19 PM
  2. char ** and argv memory question
    By simo_mon in forum C Programming
    Replies: 3
    Last Post: 08-17-2008, 08:47 AM
  3. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  4. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 01:41 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM