5 ms·
Of course, in C you'd just use designated initializers and avoid the problem entirely (any unspecified fields are zero-initialized).
by stephencanon 12y ago
Of course, in C you'd just use designated initializers and avoid the problem entirely (any unspecified fields are zero-initialized).
- mamcx 12y agoBad correlation. This is about a bad language that force GOOD developers to figth against it. "Always use memset onto struct variable before usage, because by default it's filled with garbage." See? The language PUT GARBAGE on your code. Good or bad developer, not matter. Now, a more experienced developer will remember that it need to correct the problems the language force on ALL the users. Not all the blame is in the people. Somethings, the tools are at fault, too..
- ChrisAntaki 12y agoThe language just gave you a block of memory. It lets you decide whether you'd like to dedicate resources to clearing that memory.
- mamcx 12y agoOk, so for what is usefull to have garbage after get memory?
- J_Darnley 12y agoWhy do I need to zero an image array if I am about render something into it? You spend time setting each byte to zero.
- JoeAltmaier 12y agoThat, and when your constructor is going to initialize every field, and when you're going to clone a copy of another structure...
- mamcx 12y agoI understand if I get null/empty... but garbage? And in relation to this post, the behaviour is not the same across compilers/archs? Thats nuts.
- notacoward 12y agoWelcome to programming in the real world. If that's the nuttiest thing you have to deal with on any given day, it's a good day.
- mamcx 12y agoYep, I know programing is hard. +18 years of it. Is crazy the amount of punishment a developer accept, and when the fail is elsewhere, it blame itself ;)
- fla 12y agoNo, the compiler avoids unecessary work unless you ask it to.
- mamcx 12y agoSo, when is necesary to let the garbage without clean it? For what is usefull?
- stephencanon 12y agostruct Foo foo; foo_init(&foo, bar, baz, quux); If the language made the first line zero-initialize foo, we'd write the whole of the structure twice; it would be zero-initialized just to be overwritten by foo_init. For many cases, this minor inefficiency would not be a concern. For some uses it would be a major problem. If you do need to zero-initialize foo, you can do that easily: struct Foo foo = { 0 };
- mamcx 12y ago"major problem"? Like when? Sorry to ask this. I have lived outside the C madness this 18 years (pascal and other more saner stuff..), except now using obj-c/js and is crazy the amount of small weird stuff it have.. This make my ask: From where it get the garbage?
- pstuart 12y agoMemory returned to the heap. It's recycled, right?
- mamcx 12y agoAh! I get it. Some If the program have 100 "cells" of memory, the first time it get blank, the second what was there before? Clear the data is costly in the long run? I have read yesterday about data oriented design and it make me understand that now is costly to access memory.
- thegeomaster 12y ago>See? The language PUT GARBAGE on your code. It didn't put it there. It just allocated some memory. Not more, not less. If you, for example, malloc a large chunk of memory but don't intend to use it sequentially or not right away, you can save time by not memset'ing it to zero, which would be pointless. C gives you the liberty to defer the initialization, but if you're careless, you'll shoot yourself in the foot. It's very much like having a car with manual vs. automatic transmission. More power = more responsibility. If you need power, you have to accept some compromises, and in this case, the compromise is that you need to mind what's in your memory all the time.
- kazinator 12y agoAnd designated initializers will eventually creep into C++.