Could anyone explain to me what I am doing wrong here please?
I get this error:Code:void testFree(){ char * s=(char *)calloc(sizeof(char),5); sprintf(s, "[%d]",321); printf(s); free(s); return; }
Could anyone explain to me what I am doing wrong here please?
I get this error:Code:void testFree(){ char * s=(char *)calloc(sizeof(char),5); sprintf(s, "[%d]",321); printf(s); free(s); return; }
The string you are writing to s contains 6 elements (remember the terminating null character), but you only allocated memory for 5.
ok, I thought I'd forgotten someting having to do with EOS. Thx!
> printf(s);
Also, get into the habit of ALWAYS specifying a const format string for printf, even if it is just "%s".
If s were user input, any % characters would cause all sorts of havoc.
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.