3 ms·
Hi gravypod. It depends. Oh I hate that word. When I see int i; without context I don't know anything about where memory is allocated. It can either be the f
by hackits 10y ago
Hi gravypod.
It depends. Oh I hate that word.
When I see int i; without context I don't know anything about where memory is allocated.
It can either be the following.
1) Stack allocated
2) Heap allocated
3) Is in the read only data segment.
Just starting with a int, it can be any of the above three, two that reserve the memory on the heap/stack and another that is only a read only part of memory. The heap is the only place where I would need to call malloc/free.
Though for most common libraries when I see string s; They mostly have a pointer null terminated string in memory, and the primitive data types residing as the member's of string.
RAII helps in the situation where instead of having to do tedious code such as.
String s;
allocate_string(&s);
// do something with string.
free_string(&s);
It can all be done via a standard convention of calling the deconstructor or constructor to initialise and free itself.
- gravypod 10y agoIsn't "It depends" part of the problem? For instance my professor when explaining to the other students why we use context-free grammars for parser he said "we always want a language to work how we tell it to. An if doesn't mean something different depending on where it is" While I do agree that he is incorrect and that is a very close minded, and out dated, view of language design he is accidentally correct in another sense. I'd like to ask "why" C++ chooses int i is different based on the location. I know the answer myself, but my peers don't and probably wont until years of pain in industry while parroting the same nonsense that C++ is the best language same as all of the other loons who think that there is a "best language". Why is it ok for struct packing, for aligning data, and for initial memory state to decide how the program behaves? In C++ it does, in C it's more sane but it sill happens. > RAII helps in the situation where instead of having to do tedious code such as. How is: string s = ""; and int i = 0; Tedious? That seems like the sane alternative as it puts the behavior in the hand of the programmer. In nearly every language this is decided as the preferred way of allocating variables. Define AND assign to create something useful. I don't think I understand how adding more case-specific behavior that doesn't apply to everything should be considered sane. It's more edge cases that aren't needed. It's much simpler to say "we are just going to hold a slot in memory for any object, you need to initialize it" or to say "we are going to have default initialization for EVERY object even primitives" but it's horrible to say "we're going to do a combination of both and you have to figure it out"