Where the C macros stored in memory and how does it consumes more memory compared to functions?

后端 未结 4 1077
孤城傲影
孤城傲影 2021-01-22 11:05

I searched in web where will be the C macros stored in memory and how does it consumes more memory compared to functions? Could not get any satisfying answer .Can anyone please

4条回答
  •  离开以前
    2021-01-22 11:24

    Macros are not stored in memory anywhere in the final program but instead the code for the macro is repeated whenever it occurs. As far as the actual compiler is concerned they don't even exist, they've been replaced by the preprocessor before they get that far.

    The reason that this usually takes up more memory is that it gets repeated every time the macro is used. There's no general way to determine how much memory they will take up but, honestly, memory considerations are really not the reason the reason to prefer functions to macros.

    If you having a burning need to find out the amount of memory consumed you could approximate it by looking at the disassembly of a place where you use the macro and multiplying the number of times that the macro appears in the source code by the number of lines of disassembly produced. However, there's no guarantee that different uses of the macro or the same use under different circumstances will produce identical code so this can only ever be a crude measure.

提交回复
热议问题