#1
  1. Cast down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    321
    Rep Power
    12

    Dynamically alloc memory for string


    I was playing around with malloc, and I got this to work:

    c = (char*)malloc(1);
    gets(c);
    printf("%s",c);

    Why does that work? (the malloc(1) part)?

    aLso let's say I want input from the user, like "enter your name: " and then allocate enough space to hold the users name, how would I do that?
  2. #2
  3. Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    NyC
    Posts
    4
    Rep Power
    0

    ?


    I have no clue, how DOES that work?
  4. #3
  5. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    man malloc:
    void *malloc(size_t size);
    malloc() allocates size bytes and returns a pointer to the allocated memory. The memory is not cleared.
    i'm used to using new, but i would assume:
    Code:
    char *name = (char *)malloc(NAME_LEN)
    edit: random sidenote, gets() is evil and shouldnt be used. it does no bounds checking and is a great source for possible exploitation, but u prolly knew that already ;)
    Last edited by infamous41md; July 26th, 2003 at 11:23 PM.
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,648
    Rep Power
    4248
    The malloc() function tries to dynamically allocate as many bytes as you request. Incidentally, C++ has the new keyword that does the same thing. In fact, some early C++ compilers implemented the new keyword as malloc(). :)

    In C:
    c = (char*)malloc(somesize);
    In C++:
    c = new char[somesize];

    Note that malloc() actually allocates its memory in bytes. In your case, the char type occupies only one byte, but this is not true of other types (or possibly in other OS/dev environments). To be more portable, you would write something like this:
    c = (char *)malloc(somesize * sizeof(char));
    or in general, for a variable of any particular type:
    foo = (type_of_foo *) malloc( somesize * sizeof(type_of_foo));

    This way, in case the char size changes to something other than one byte, you'll still get the memory you need.
    Last edited by Scorpions4ever; July 26th, 2003 at 11:42 PM.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  8. #5
  9. Cast down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    321
    Rep Power
    12
    yeah, but why does this work, c = (char*)malloc(1); <-- I am only allocating 1 byte, but it's able to accept as much as the user enters and then prints it out fine.. Where is it storing all the bytes the user enters if I only told it to allocate 1 byte? or am I misunderstanding something.
  10. #6
  11. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,648
    Rep Power
    4248
    >> I am only allocating 1 byte, but it's able to accept as much as the user enters and then prints it out fine..
    Beginners luck! :)
    Actually, what you have there is a fine example of a buffer overflow bug. When you call malloc(), the OS carves out some memory from an area of memory called the heap and hands it to your program. Let's say you have 1 byte allocated. Now, when you use gets() to get the string, the memory past the 1 byte is overwritten (which is why infamous4md said that gets() is evil ... probably the #1 cause of overflow bugs). However, since no other part of the program is using that memory (yet!), all seems fine. If this was a larger program and you allocated more memory somewhere else (thereby causing the unused memory past the 1 byte to be allocated to some other variables), you would soon discover that some variables mysteriously change values, when other variables are assigned to :D.

    Also, another thing to remember is that most OS memory managers handle memory in relatively large chunks. So, if you ask for 1 byte, the OS might actually allocate 4K bytes from the heap and hand you the 4K.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo

IMN logo majestic logo threadwatch logo seochat tools logo