What is a “memory stomp”?

后端 未结 3 1630
青春惊慌失措
青春惊慌失措 2020-12-12 12:52

I just came across this blog post which mentions “stomping memory”:

a C++ program which is easily capable of stomping memory (something you prob

3条回答
  •  伪装坚强ぢ
    2020-12-12 13:27

    Other answers basically are correct, but I would like to give an example.

    int a[10], i;       
    for (i = 0; i < 11 ; i++)
        a[i] = 0;
    

    int i, a[10];     
    for (i = 0; i < 11 ; i++)
        a[i] = 0;
    

    These samples may lead into infinite loop (or may not lead), because it is undefined behavior.

    Very likely variable i in memory is stored just after array. So accessing a[10] could actually access i in other words it could reset loop counter.

    I think it is good example that demonstrates memory "stomping".

提交回复
热议问题