C++头文件改变程序中断

问题描述 投票:0回答:1

我不明白为什么这段代码会打印不同的程序中断

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <ftw.h>
#include <stdlib.h>

int main()
{
    int max_size = 1024;
    char* ptr[max_size];

    int blockCount = 1000;
    int blockSize = 4*1024*1024;
    int from = 1;
    int to = blockCount;
    int step = 1;
    int i;

    printf("Program break: 0x%p\n", sbrk(0));
    for(i = 0; i < blockCount; ++i)
    {
        ptr[i] = (char*)malloc(blockSize);
    }

    printf("Program break: 0x%p\n", sbrk(0));
    for(int i = from; i < to; i += step)
    {
        free((char*)ptr[i]);
    }

    printf("Program break: 0x%p\n", sbrk(0));
}

输出:

Program break: 0x0x5604211f1000
Program break: 0x0x560421212000
Program break: 0x0x560421212000

但是,如果我在文件开头添加

#include <iostream>
,它会打印相同的程序中断。

输出:

Program break: 0x0x556617053000
Program break: 0x0x556617053000
Program break: 0x0x556617053000

它会改变标题中的程序中断吗? 如果是这样,什么可能会改变头文件中的程序中断? 此外,如果编译为 C 代码,则最后一个程序断点指向初始断点。就好像

free
返回未使用的页面一样。 但在 C++ 中,这种情况不会发生。

g++版本:9.4.0

编译选项:-O0

操作系统/ABI:UNIX - 系统 V

ABI 版本:0

内核版本:5.15.0-87-generic

发行版:Ubuntu 20.04.6 LTS

c++ c linux malloc abi
1个回答
0
投票

正如@teapot418所说

实际上 4MB 分配高于 mmap 的阈值,所以你可以 还希望那些人根本不会移动休息时间。

© www.soinside.com 2019 - 2024. All rights reserved.