gpt4 book ai didi

c - Linux:brk()错误 'Cannot allocate memory'

转载 作者:行者123 更新时间:2023-11-30 20:17:13 33 4
gpt4 key购买 nike

我正在尝试在 C 程序中使用 brk() 函数。我的目标是通过使用

检查当前程序中断 (pb) 来直接使用它(作为更大测试的一部分)
void *current_break = sbrk(0);

执行malloc(用于测试,如果分配的空间足够大,malloc有时应该执行brk)

void* mallow_return = malloc(1);

而不是使用当前地址 + 增量直接执行 brk()(并检查这是否会增加 pb):

int increase = 0x01; 
void * newbreak = current_break + increase;
int return_value = brk(&newbreak);

我的问题是,无论是使用大型 malloc (malloc(5000;)) 还是使用(对齐或未对齐)brk() 调用,pb 都不会更改。检查 errno 时我得到一个

Cannot allocate memory!error (as given bystrerror(errno)

有人能明白为什么我无论如何都无法增加程序中断吗?

感谢您的任何提示!

(系统是:Debian 10 (buster),内核为 4.19)

编辑:根据要求,这是主要功能,包括:

#include <unistd.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>

void main(int argc, char **argv)
{

printf("[+] Get current program break\n");
void *current_break = sbrk(0);
printf("\t[+] Current break point=\t%#x\n", current_break);


printf("[+] Malloc should call brk as well\n");
void* mallow_return = malloc(1);

printf("[+] Check if malloc changes PB\n");
void *after_break = sbrk(0);
printf("\t[+] After malloc pbreak=\t%#x\n", after_break);


int increase = 0x01;
printf("\t[+] Increasing p-break direclyby %d\n", increase);


void * newbreak = current_break + increase;
printf("\t[+] Setting break point to=\t%#x\n", newbreak);


int return_value = brk(&newbreak);

//check if error was thrown
int errornumber = errno;
if (errornumber != 0)
{
printf("\t[+] Error: %s!\n", strerror(errornumber));
return -1;
}

//check if pb was set now
printf("\t[?] New program break value?\t%#x\n", sbrk(0));
printf("[?] Return value of brk: %d\n", return_value);

return;
}

最佳答案

(感谢@Antii Haapala,他将此作为评论发布。)

我们需要删除此处的&符号:

int return_value = brk(&newbreak);

该行应该很简单

int return_value = brk(newbreak);

关于c - Linux:brk()错误 'Cannot allocate memory',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58373192/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com