Oleksandr
I prefer to write safe code with no potentional segfaults
i agree, but... thats like C++ and Java)) unsafe safe))
Oleksandr
but we still here
Vladimir
In Linux not return null by default
malloc returns null in case of error - no available memory.
Oleksandr
when u call some function, you create its frame on the stack
Daniele°
By default, you can change a feature
Daniele°
Try assert(malloc(1024*1024*1024*1024));
Vladimir
In Linux you have Always memory
In C - you don't. Imagine you code stuff with about... 32 kb ram?
Oleksandr
frame contains stack variables, params, return_address, and place for return value(may be) when u call return, u just "jump" to return_address
Oleksandr
allocators doesn't work in such way)))
Oleksandr
mmap called only when u really use memory
Oleksandr
malloc(max_size_t); doesn't crush
Vladimir
I specified in Linux
Linux can't reach memory limit or what?
Oleksandr
Linux can't reach memory limit or what?
if u don't use this memory, linux can't
Oleksandr
if u call malloc, that does not guarantee that allocation done
Vladimir
Mm
Daniele°
malloc(max_size_t); doesn't crush
# echo 2 > /proc/sys/vm/overcommit_memory
Daniele°
Linux can't reach memory limit or what?
optimistic memory allocation strategy
Vladimir
Be realistic
Daniele°
And malloc return null
joel
but it's better than other operating systems
Vladimir
*triggered*
joel
who? me?
Oleksandr
Oleksandr
if u really use all this memory, system'll try to allocate it
Oleksandr
and return NULL
Vladimir
May be show what's in ptr?
Oleksandr
yeah, there would be 0
Daniele°
Probably compiler optimized out Memory allocation
Vladimir
Yes, and if you try dereference, you get segfault. That's the point of all previous discussion
Vladimir
To prevent it
Vladimir
Instead of checking before every access, I can check one time after malloc
Oleksandr
allocation may be only 1 time, and use it 10 time... so 1 check vs 10 checks
Daniele°
bad variant
Allocate big big buffer, try to write with system call write at last address and now you have all memory
Daniele°
Otherwise enter in sleep and wait to purchase other Memory 😂
Oleksandr
but for real, are you see such cases in real life?
Oleksandr
when malloc return 0
Daniele°
but for real, are you see such cases in real life?
in real case many people check if address is valid
Oleksandr
i check each pointer before use(if it passed to me trow params)
Oleksandr
put checking pointer after allocation...
Oleksandr
okay, for example, malloc returned 0.. i see it.. and so what? exit(-1) ?)
Vladimir
Better check before function, inside function, and in the start despite of not checking at all
Oleksandr
More checks = better code
more checks = slower code
Vladimir
In the big programms it goes that way
Vladimir
Print "out of memory" and exit
Oleksandr
exit(1), good solution.
i don't really see the diffrence between exit(1) and crush
Vladimir
You can restore something before
Vladimir
For example terminal state
Vladimir
If you changed its params before
Oleksandr
okay, for serialization some data.. good
Vladimir
Crush is just crush without warnings
Vladimir
We don't want user to experience program crushes without any message why this happened
Oleksandr
i think if malloc return 0, there is only two reasons: 1) u force malloc to do this(pass max_size_t) 2) ur code is bullshit
joel
crash*
Oleksandr
Vladimir
crash*
Yea, I just wrote like him 😅
Vladimir
Crush is person you in love, I know
joel
c++ is so complex
Oleksandr
in fact, problem we discussed, is more for C lang
Oleksandr
Oleksandr
C++ gives compile time error))
Vladimir
Seems like compiler knows about value of ::max()
Oleksandr
Seems like compiler knows about value of ::max()
seems like compiler knows about 2 cases when malloc return 0)))))