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

This is a discussion on how to allocate a known block of the memory to a char * within the C Programming forums, part of the General Programming Boards category; Hi, i have a char * which points to a buffer which i get from the recv function. I want ...

  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,211
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    You cannot free part of the buffer

    parse buffer till the end
    free it when the work with the whole buffer is finished
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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);
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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, 04:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21