I've been tinkering with computers since I was a teen. I'm currently pivoting from my current role as Tech Support manager to Full Stack Web Developer. I'm actively seeking employment in the field.
Void *ptr = realloc(str, new size)
If (!ptr) {
.... Handle error...
}
str = ptr
This way if realloc fails the reference to str is not lost.
Of course in a normal Linux environment it is rare for any of the allocation functions such as malloc, calloc etc.. to fail. See anything about overcommit for an explanation.. This would make good subject for a followup article.
I've been tinkering with computers since I was a teen. I'm currently pivoting from my current role as Tech Support manager to Full Stack Web Developer. I'm actively seeking employment in the field.
Can you elaborate and/or provide an example of what is considered best practice?
Use a dummy ptr something like this...
Void *ptr = realloc(str, new size)
If (!ptr) {
.... Handle error...
}
str = ptr
This way if realloc fails the reference to str is not lost.
Of course in a normal Linux environment it is rare for any of the allocation functions such as malloc, calloc etc.. to fail. See anything about overcommit for an explanation.. This would make good subject for a followup article.
Thanks! These are my notes as I learn the language, so comments and clarifications like this are most welcome 😀👍
Pleasure.
You might be interested in how linux actually allocates memory and why the malloc() type functions actually rarely fail.
Google overcommit, oomkiller and page tables.
Might make a good subject for your next blog