zeroing out memory

后端 未结 12 2146
無奈伤痛
無奈伤痛 2021-01-31 09:52

gcc 4.4.4 C89

I am just wondering what most C programmers do when they want to zero out memory.

For example, I have a buffer of 1024 bytes. Sometimes I do this:<

12条回答
  •  轮回少年
    2021-01-31 10:18

    I'm not familiar with the:

    char buffer[1024] = {0};
    

    technique. But assuming it does what I think it does, there's a (potential) difference to the two techniques.

    The first one is done at COMPILE time, and the buffer will be part of the static image of the executable, and thus be 0's when you load.

    The latter will be done at RUN TIME.

    The first may incur some load time behaviour. If you just have:

    char buffer[1024];
    

    the modern loaders may well "virtually" load that...that is, it won't take any real space in the file, it'll simply be an instruction to the loader to carve out a block when the program is loaded. I'm not comfortable enough with modern loaders say if that's true or not.

    But if you pre-initialize it, then that will certainly need to be loaded from the executable.

    Mind, neither of these have "real" performance impacts in the small. They may not have any in the "large". Just saying there's potential here, and the two techniques are in fact doing something quite different.

提交回复
热议问题