c: memory allocation (what's going on)

Posted by facha on Stack Overflow See other posts from Stack Overflow or by facha
Published on 2010-04-27T23:29:37Z Indexed on 2010/04/27 23:33 UTC
Read the original article Hit count: 197

Filed under:
|

Hi, everyone

Please take a look at this piece of code. I'm allocating one byte for the first variable and another byte for the second one. However, it seems like the compiler allocates more (or I'm missing something). The program outputs both strings, even though their length is more the one byte.

void main() {
    char* some1 = malloc(1);
    sprintf(some1,"cool");
    char* some2 = malloc(1);
    sprintf(some2,"face");
    printf("%s ",some1);
    printf("%s\n",some2);
}

Please, could anyone spot some light on what's going on when memory is being allocated.

© Stack Overflow or respective owner

Related posts about c

    Related posts about memory-allocation