smilelc3 / leetcode

Leetcode Solution C/C++/Golang Version(leetocde 刷题解答,C、C++、Golang 版本 )
GNU General Public License v3.0
3 stars 2 forks source link

mem_test #38

Closed smilelc3 closed 1 year ago

smilelc3 commented 1 year ago
#include <malloc.h>
#include <string.h>

void test() {
    int *ptr = malloc(sizeof(int) * 10);
    ptr[10] = 7;    // 内存越界
    memcpy(ptr + 1, ptr, 5);    // 踩内存

    free(ptr);
    free(ptr);
    int *p1;
    *p1 = 1;
}

int main() {
    test();
    return 0;
}