sbrk错误测试,errno转换等问题

Guy_Fwakes 2014-03-19 09:48:57
#include<stdio.h>
#include<unistd.h>
#include<errno.h>

main()
{
void *p=sbrk(1000000000*2);
if(p == (void*)-1)
{
printf("Memory:%m\n");
perror("hello");
printf("::%s\n",strerror(errno));
}
}

错误显示:

我的问题是:
1.为什么编译器说我用strerro()转换后是int型,我man strerro却说是char*
2.我企图用sbrk申请过大内存报错,但是即便超出了整形最大值,仍然不行,这不科学
希望大神指明方向!感激不尽
...全文
129 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Guy_Fwakes 2014-03-21
  • 打赏
  • 举报
回复
引用 6 楼 merlinfang 的回复:
[quote=引用 4 楼 hzt19901012 的回复:] 你看这句p == (void*)-1成立了,应该说明sbrk()返回了-1,应该是函数出错了吧,但是显示错误的信息竟然是成功了;你误会我的意图了,我就是想让sbrk()出错,然后输出错误信息,所以不停的调整sbrk()参数的大小。最后谢谢你!
恩,我看错了,在我机器上是正常报错的, 调用sbrk之前,加上这段试试, extern int errno; errno = -1;[/quote] 今天做了个测试,循环调用sbrk(++n),确实会出现Cannot allocate memory,发现了很愚蠢的错误,应该是我使用溢出整数的时候没注意,比如我传值3000000000到sbrk(),实际值是-1294967296,所以无论如何调整大小,都是无法一次实现内存分配错误的,很谢谢你的耐心跟帖,准备接分吧
merlinfang 2014-03-21
  • 打赏
  • 举报
回复
引用 4 楼 hzt19901012 的回复:
你看这句p == (void*)-1成立了,应该说明sbrk()返回了-1,应该是函数出错了吧,但是显示错误的信息竟然是成功了;你误会我的意图了,我就是想让sbrk()出错,然后输出错误信息,所以不停的调整sbrk()参数的大小。最后谢谢你!
恩,我看错了,在我机器上是正常报错的, 调用sbrk之前,加上这段试试, extern int errno; errno = -1;
赵4老师 2014-03-20
  • 打赏
  • 举报
回复
BRK Section: Linux Programmer's Manual (2) Updated: 2003-11-01 -------------------------------------------------------------------------------- NAME brk, sbrk - change data segment size SYNOPSIS #include <unistd.h> int brk(void *end_data_segment); void *sbrk(intptr_t increment); DESCRIPTION brk sets the end of the data segment to the value specified by end_data_segment, when that value is reasonable, the system does have enough memory and the process does not exceed its max data size (see setrlimit(2)). sbrk increments the program's data space by increment bytes. sbrk isn't a system call, it is just a C library wrapper. Calling sbrk with an increment of 0 can be used to find the current location of the program break. RETURN VALUE On success, brk returns zero, and sbrk returns a pointer to the start of the new area. On error, -1 is returned, and errno is set to ENOMEM. CONFORMING TO BSD 4.3 brk and sbrk are not defined in the C Standard and are deliberately excluded from the POSIX.1 standard (see paragraphs B.1.1.1.3 and B.8.3.3). NOTES Various systems use various types for the parameter of sbrk(). Common are int, ssize_t, ptrdiff_t, intptr_t. XPGv6 obsoletes this function. SEE ALSO execve(2), getrlimit(2), malloc(3) --------------------------------------------------------------------------------
Guy_Fwakes 2014-03-20
  • 打赏
  • 举报
回复
引用 3 楼 merlinfang 的回复:
[quote=引用 2 楼 hzt19901012 的回复:] [quote=引用 1 楼 merlinfang 的回复:] 1、你要#include <string.h> 2、我的很正常的报错了,估计你的是64位系统吧,另外看下你的limit设置,sbrk并不会真的申请内存,只是偏移下虚拟地址 :~/test$ ./a.out Memory:Cannot allocate memory hello: Cannot allocate memory ::Cannot allocate memory
很感谢你解决了我的第一个问题,但是我的第二个问题依然存在,我的系统是Ubuntu ,运行结果为: 我知道sbrk拓展映射范围,这只是个测试,为什么没有错误显示呢?还是说不会有内存错误?[/quote] 因为成功了啊, ulimit -a看下你的data seg配置,如果是unlimited,改小就可以了[/quote] 你看这句p == (void*)-1成立了,应该说明sbrk()返回了-1,应该是函数出错了吧,但是显示错误的信息竟然是成功了;你误会我的意图了,我就是想让sbrk()出错,然后输出错误信息,所以不停的调整sbrk()参数的大小。最后谢谢你!
merlinfang 2014-03-19
  • 打赏
  • 举报
回复
引用 2 楼 hzt19901012 的回复:
[quote=引用 1 楼 merlinfang 的回复:] 1、你要#include <string.h> 2、我的很正常的报错了,估计你的是64位系统吧,另外看下你的limit设置,sbrk并不会真的申请内存,只是偏移下虚拟地址 :~/test$ ./a.out Memory:Cannot allocate memory hello: Cannot allocate memory ::Cannot allocate memory
很感谢你解决了我的第一个问题,但是我的第二个问题依然存在,我的系统是Ubuntu ,运行结果为: 我知道sbrk拓展映射范围,这只是个测试,为什么没有错误显示呢?还是说不会有内存错误?[/quote] 因为成功了啊, ulimit -a看下你的data seg配置,如果是unlimited,改小就可以了
Guy_Fwakes 2014-03-19
  • 打赏
  • 举报
回复
引用 1 楼 merlinfang 的回复:
1、你要#include <string.h>

2、我的很正常的报错了,估计你的是64位系统吧,另外看下你的limit设置,sbrk并不会真的申请内存,只是偏移下虚拟地址
:~/test$ ./a.out
Memory:Cannot allocate memory
hello: Cannot allocate memory
::Cannot allocate memory

很感谢你解决了我的第一个问题,但是我的第二个问题依然存在,我的系统是Ubuntu ,运行结果为:

我知道sbrk拓展映射范围,这只是个测试,为什么没有错误显示呢?还是说不会有内存错误?
merlinfang 2014-03-19
  • 打赏
  • 举报
回复
1、你要#include <string.h> 2、我的很正常的报错了,估计你的是64位系统吧,另外看下你的limit设置,sbrk并不会真的申请内存,只是偏移下虚拟地址 :~/test$ ./a.out Memory:Cannot allocate memory hello: Cannot allocate memory ::Cannot allocate memory

65,209

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧