+ 1

Memory allocation im C

I am interested im memory allocation in C language. I can't imagine how variable with allocated memory (malloc) behave when has bigger size than allocated bytes. Here is code: int main() { char* str = (char*) malloc(3); strcpy(str, "Long sentence."); printf("%s", str); return 0; }:

1st Jun 2021, 9:15 PM
bwilk315
bwilk315 - avatar
8 odpowiedzi
+ 2
Always assume the worst: Your program will probably crash.
1st Jun 2021, 10:29 PM
Schindlabua
Schindlabua - avatar
+ 1
Now I understand, unpredictable results are unwanted forced changes in memory.
1st Jun 2021, 10:06 PM
bwilk315
bwilk315 - avatar
+ 1
Sometimes I get Segmentation Fault but mostly program runs and able to corrupt other variable If you are getting ERROR then try to re-run it https://code.sololearn.com/cTiww2y4hfG9/?ref=app
3rd Jun 2021, 1:11 AM
🌀 Shail Murtaza شعیل مرتضیٰ
🌀 Shail Murtaza شعیل مرتضیٰ - avatar
0
Thanks for fast answer, so when allocated memory is too small for value like char array, it is re-allocated?
1st Jun 2021, 9:42 PM
bwilk315
bwilk315 - avatar