malloc失败时的行为 [英] malloc behavior on failure

查看:84
本文介绍了malloc失败时的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最初在讨论线程的小组中发布了以下代码

但经过进一步研究后我觉得我对代码有疑问。

我知道有一对非标准c包括这里和POSIX

的东西是非标准的,但这是我偶然发现这个问题。


#include< INTEGRITY.h> ;

#include< stdlib.h>

#include< stdio.h>

#include< pthread.h>

#include< signal.h>

#include< time.h>


timer_t l_timer_2;

int counter = 0;


// void测试(union sigval sigval)

void测试(union sigval sigval)

{

printf(" Count \ n");

counter ++;


return;

}

int main()

{

struct itimerspec l_timer_spec1;

struct sigevent l_tevent1 ;

int * ptr;

int status;


l_tevent1.sigev_notify = SIGEV_THREAD;

l_tevent1.sigev_value.sival_ptr =& l_timer_2;

l_tevent1.sigev_notify_function =测试;

l_tevent1。 sigev_notify_attributes = NULL;

l_tevent1.sigev_signo = SIGRTMIN;

l_timer_spec1.it_interval.tv_sec = 2;

l_timer_spec1.it_interval.tv_nsec = 0;

//设置后续计时器间隔

l_timer_spec1.it_value.tv_sec = 2;

l_timer_spec1.it_value.tv_nsec = 0;


//创建计时器

if(timer_create(CLOCK_REALTIME,& l_tevent1,& l_timer_2)< 0)

{

status = 1;

}


if(status == 0 )

{

if(timer_settime(l_timer_2,0,& l_timer_spec1,NULL)< 0)

{

status = 1;

}

}


ptr = malloc(1000000);


if(ptr == NULL)

printf(" NULL PTR\ n);


counter ++;


退出(0);

}


现在我知道这段代码snipet很没用但是它是由于

解决问题。当malloc失败时,计时器停止按预期运行
。经过一些研究后,似乎当malloc

失败时,返回NULL,由于资源不足,它实际上将所有可用内存标记为已分配。因此,当线程弹出时,计时器无法启动。所以我的问题是,在这种情况下,

标准是否必须说明malloc的行为。

允许这种行为,如果是这样,那就很常见了。在我看来,

是更多优雅的方法来失败。

I originally posted the following code in a group discussing threads
but after further research I think I have a c question about the code.
I know there are a couple of non standard c includes here and the POSIX
stuff is non standard but this is how I stumbled onto this question.

#include <INTEGRITY.h>
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <signal.h>
#include <time.h>

timer_t l_timer_2;
int counter =0;

//void Test(union sigval sigval)
void Test (union sigval sigval)
{
printf("Count\n");
counter++;

return;
}
int main()
{
struct itimerspec l_timer_spec1;
struct sigevent l_tevent1;
int *ptr;
int status;

l_tevent1.sigev_notify = SIGEV_THREAD;
l_tevent1.sigev_value.sival_ptr = &l_timer_2;
l_tevent1.sigev_notify_function = Test;
l_tevent1.sigev_notify_attributes = NULL;
l_tevent1.sigev_signo = SIGRTMIN;
l_timer_spec1.it_interval.tv_sec = 2;
l_timer_spec1.it_interval.tv_nsec = 0;
//set subsequent timer intervals
l_timer_spec1.it_value.tv_sec = 2;
l_timer_spec1.it_value.tv_nsec = 0;

//create timer
if (timer_create(CLOCK_REALTIME, &l_tevent1, &l_timer_2) < 0)
{
status = 1;
}

if (status == 0)
{
if (timer_settime(l_timer_2, 0, &l_timer_spec1, NULL) < 0 )
{
status = 1;
}
}

ptr = malloc(1000000);

if (ptr == NULL)
printf("NULL PTR\n");

counter++;

Exit(0);
}

Now I know this code snipet is pretty useless but it was the result of
troubleshooting a problem. When the malloc fails the timer stops
operating as expected. After some research it appears that when malloc
fails, returns NULL, due to not enough resources it actually marks all
the available memory as allocated. Due to this the timer can''t start
the thread when it pops. So my question is what, if anything, does the
standard have to say about the behavior of malloc in this situation. Is
this behavior allowed and if so is it common. It seems to me that there
are many more gracefull ways for this to fail.

推荐答案

On 2006年2月27日16:38:01 -0800,gooch <去****** @ comcast.net>写在

comp.lang.c:
On 27 Feb 2006 16:38:01 -0800, "gooch" <go******@comcast.net> wrote in
comp.lang.c:
我最初在讨论线程的小组中发布了以下代码
但经过进一步研究后我想我有关于代码的问题。
我知道这里有一些非标准的c包含且POSIX
的东西是非标准的,但这就是我偶然发现这个问题的方法。

#include< INTEGRITY.h>
#include< stdlib.h>
#include< stdio.h>
#include< pthread.h>
#include< signal.h>
#include< time.h>

timer_t l_timer_2;
int counter = 0;

// void test(union sigval sigval)
void测试(union sigval sigval)
{
printf(" Count \ nn);;
counter ++;

返回;
}

int main()
结构itimerspec l_timer_spec1;
struct sigevent l_tevent1;
int * ptr;
int status;

l_tevent1.sigev_noti fy = SIGEV_THREAD;
l_tevent1.sigev_value.sival_ptr =& l_timer_2;
l_tevent1.sigev_notify_function =测试;
l_tevent1.sigev_notify_attributes = NULL;
l_tevent1.sigev_signo = SIGRTMIN;
l_timer_spec1.it_interval.tv_sec = 2;
l_timer_spec1.it_interval.tv_nsec = 0;
//设置后续计时器间隔
l_timer_spec1.it_value.tv_sec = 2;
l_timer_spec1。 it_value.tv_nsec = 0;

//创建计时器
if(timer_create(CLOCK_REALTIME,& l_tevent1,& l_timer_2)< 0)
{
status = 1;
}
if(status == 0)
{
if(timer_settime(l_timer_2, 0,& l_timer_spec1,NULL)< 0)
{
status = 1;
}
}

ptr = malloc(1000000);

if(ptr == NULL)
printf(" NULL PTR\ n);

计数器++;

退出(0);
}
现在我知道这个代码snipet很没用,但它是对问题进行故障排除的结果。当malloc失败时,计时器停止按预期运行。经过一些研究后,似乎当malloc
失败时,返回NULL,由于资源不足,它实际上将所有可用内存标记为已分配。因此,计时器弹出时无法启动线程。所以我的问题是,在这种情况下,
标准必须说明malloc的行为。是否允许这种行为,如果是这样,那就很常见了。在我看来,有更多优雅的方法可以让它失败。
I originally posted the following code in a group discussing threads
but after further research I think I have a c question about the code.
I know there are a couple of non standard c includes here and the POSIX
stuff is non standard but this is how I stumbled onto this question.

#include <INTEGRITY.h>
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <signal.h>
#include <time.h>

timer_t l_timer_2;
int counter =0;

//void Test(union sigval sigval)
void Test (union sigval sigval)
{
printf("Count\n");
counter++;

return;
}
int main()
{
struct itimerspec l_timer_spec1;
struct sigevent l_tevent1;
int *ptr;
int status;

l_tevent1.sigev_notify = SIGEV_THREAD;
l_tevent1.sigev_value.sival_ptr = &l_timer_2;
l_tevent1.sigev_notify_function = Test;
l_tevent1.sigev_notify_attributes = NULL;
l_tevent1.sigev_signo = SIGRTMIN;
l_timer_spec1.it_interval.tv_sec = 2;
l_timer_spec1.it_interval.tv_nsec = 0;
//set subsequent timer intervals
l_timer_spec1.it_value.tv_sec = 2;
l_timer_spec1.it_value.tv_nsec = 0;

//create timer
if (timer_create(CLOCK_REALTIME, &l_tevent1, &l_timer_2) < 0)
{
status = 1;
}

if (status == 0)
{
if (timer_settime(l_timer_2, 0, &l_timer_spec1, NULL) < 0 )
{
status = 1;
}
}

ptr = malloc(1000000);

if (ptr == NULL)
printf("NULL PTR\n");

counter++;

Exit(0);
}

Now I know this code snipet is pretty useless but it was the result of
troubleshooting a problem. When the malloc fails the timer stops
operating as expected. After some research it appears that when malloc
fails, returns NULL, due to not enough resources it actually marks all
the available memory as allocated. Due to this the timer can''t start
the thread when it pops. So my question is what, if anything, does the
standard have to say about the behavior of malloc in this situation. Is
this behavior allowed and if so is it common. It seems to me that there
are many more gracefull ways for this to fail.




你怎么知道标准C库函数malloc()是做

你说的是什么?你怎么知道这不是你b $ b专有商业操作系统的行为?


你的程序中充满了标准C中不存在的东西。

C不知道或不关心计时器弹出或者线程。


你甚至没有令人信服的证据证明失败的内存分配

是错误的,事实上你可以'在严格的符合b $ b b的程序中证明这一点是肯定的。不过,你可以试试。当尝试
分配1,000,000个字节失败时,如果你立即尝试更小的分配,比如1个字节,结果是什么?没有线程和弹出

计时器。


我建议您与Green Hills技术支持人员交流。你有一个特定的

实现问题,而不是语言问题。


-

Jack Klein

主页: http://JK-Technology.Com

常见问题解答

comp.lang.c http:// c-faq .com /

comp.lang.c ++ http://www.parashift.com/c++-faq-lite/

alt.comp.lang.learn.c-c ++
http://www.contrib.andrew.cmu.edu /〜a...FAQ-acllc.html



How do you know that the standard C library function malloc() is doing
what you say it is? How do you know it is not the behavior of your
proprietary commercial operating system?

Your program is littered with things that do not exist in standard C.
C does not know or care about "timers popping" or "threads".

You don''t even have convincing proof that the failed memory allocation
is at fault, and in fact you can''t prove that for sure in a strictly
conforming program. Nevertheless, you could try. When the attempt to
allocate 1,000,000 bytes fails, what is the result if you immediately
try a smaller allocation, say 1 byte? Without the threads and popping
timers.

I''d suggest you talk to Green Hills technical support. You have an
implementation specific issue, not a language one.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html




Jack Klein写道:

Jack Klein wrote:
2006年2月27日16:38:01 -0800,gooch <去****** @ comcast.net>写在
comp.lang.c:
On 27 Feb 2006 16:38:01 -0800, "gooch" <go******@comcast.net> wrote in
comp.lang.c:
我最初在讨论线程的小组中发布了以下代码
但经过进一步的研究后我觉得我有一个问题关于代码。
我知道这里有一些非标准的c包含,POSIX
的东西是非标准的,但这就是我偶然发现这个问题的方法。

#include < INTEGRITY.h>
#include< stdlib.h>
#include< stdio.h>
#include< pthread.h>
#include< signal.h>
#include< time.h>

timer_t l_timer_2;
int counter = 0;

// void测试(联合sigval sigval)
void测试(union sigval sigval)
{
printf(" Count \ nn);;
计数器++;

返回;
}

int main()
结构itimerspec l_timer_spec1;
struct sigevent l_tevent1;
int * ptr;
int status;

l_tevent1.sigev_notify = S. IGEV_THREAD;
l_tevent1.sigev_value.sival_ptr =& l_timer_2;
l_tevent1.sigev_notify_function =测试;
l_tevent1.sigev_notify_attributes = NULL;
l_tevent1.sigev_signo = SIGRTMIN;
l_timer_spec1.it_interval.tv_sec = 2;
l_timer_spec1.it_interval.tv_nsec = 0;
//设置后续计时器间隔
l_timer_spec1.it_value.tv_sec = 2;
l_timer_spec1.it_value。 tv_nsec = 0;

//创建计时器
if(timer_create(CLOCK_REALTIME,& l_tevent1,& l_timer_2)< 0)
{
status = 1;
}
if(status == 0)
{
if(timer_settime(l_timer_2, 0,& l_timer_spec1,NULL)< 0)
{
status = 1;
}
}

ptr = malloc(1000000);

if(ptr == NULL)
printf(" NULL PTR\ n);

计数器++;

退出(0);
}
现在我知道这个代码snipet很没用,但它是对问题进行故障排除的结果。当malloc失败时,计时器停止按预期运行。经过一些研究后,似乎当malloc
失败时,返回NULL,由于资源不足,它实际上将所有可用内存标记为已分配。因此,计时器弹出时无法启动线程。所以我的问题是,在这种情况下,
标准必须说明malloc的行为。是否允许这种行为,如果是这样,那就很常见了。在我看来,还有更多优雅的方法可以让它失败。
你怎么知道标准的C库函数malloc()正在做什么呢?你说的是什么?您怎么知道这不是您的专有商业操作系统的行为?

您的程序中充斥着标准C中不存在的内容。
C不知道或者关心定时器弹出或者线程。

你甚至没有令人信服的证据证明失败的内存分配是错误的,事实上你无法证明严格遵守程序。
I originally posted the following code in a group discussing threads
but after further research I think I have a c question about the code.
I know there are a couple of non standard c includes here and the POSIX
stuff is non standard but this is how I stumbled onto this question.

#include <INTEGRITY.h>
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <signal.h>
#include <time.h>

timer_t l_timer_2;
int counter =0;

//void Test(union sigval sigval)
void Test (union sigval sigval)
{
printf("Count\n");
counter++;

return;
}
int main()
{
struct itimerspec l_timer_spec1;
struct sigevent l_tevent1;
int *ptr;
int status;

l_tevent1.sigev_notify = SIGEV_THREAD;
l_tevent1.sigev_value.sival_ptr = &l_timer_2;
l_tevent1.sigev_notify_function = Test;
l_tevent1.sigev_notify_attributes = NULL;
l_tevent1.sigev_signo = SIGRTMIN;
l_timer_spec1.it_interval.tv_sec = 2;
l_timer_spec1.it_interval.tv_nsec = 0;
//set subsequent timer intervals
l_timer_spec1.it_value.tv_sec = 2;
l_timer_spec1.it_value.tv_nsec = 0;

//create timer
if (timer_create(CLOCK_REALTIME, &l_tevent1, &l_timer_2) < 0)
{
status = 1;
}

if (status == 0)
{
if (timer_settime(l_timer_2, 0, &l_timer_spec1, NULL) < 0 )
{
status = 1;
}
}

ptr = malloc(1000000);

if (ptr == NULL)
printf("NULL PTR\n");

counter++;

Exit(0);
}

Now I know this code snipet is pretty useless but it was the result of
troubleshooting a problem. When the malloc fails the timer stops
operating as expected. After some research it appears that when malloc
fails, returns NULL, due to not enough resources it actually marks all
the available memory as allocated. Due to this the timer can''t start
the thread when it pops. So my question is what, if anything, does the
standard have to say about the behavior of malloc in this situation. Is
this behavior allowed and if so is it common. It seems to me that there
are many more gracefull ways for this to fail.
How do you know that the standard C library function malloc() is doing
what you say it is? How do you know it is not the behavior of your
proprietary commercial operating system?

Your program is littered with things that do not exist in standard C.
C does not know or care about "timers popping" or "threads".

You don''t even have convincing proof that the failed memory allocation
is at fault, and in fact you can''t prove that for sure in a strictly
conforming program.




进入malloc后,资源分析器显示可用的内存很多。

一旦malloc失败,100%的资源被利用。足够的证明。

尽管如此,你可以试试。当尝试分配1,000,000个字节失败时,如果您立即尝试更小的分配,比如1个字节,结果是什么?没有线程和弹出定时器。

我建议您与Green Hills技术支持人员交流。你有一个特定于实现的问题,而不是语言问题。



Upon entering malloc a resource analyzer shows much memory available.
Once malloc fails 100% of resources are utilized. Proof enough for you.
Nevertheless, you could try. When the attempt to
allocate 1,000,000 bytes fails, what is the result if you immediately
try a smaller allocation, say 1 byte? Without the threads and popping
timers.

I''d suggest you talk to Green Hills technical support. You have an
implementation specific issue, not a language one.




我的问题是语言特定的。忽略代码,如果你想要
并且只是阅读问题,但问题与

实现无关。



My question is language specific. Ignore the code if you would like to
and just read the question but the question has nothing to do with the
implementation.


gooch写道:
我最初在讨论线程的小组中发布了以下代码
但经过进一步的研究后我觉得我对代码有疑问。我知道这里有一些非标准的c包含在内并且
POSIX的东西是非标准的,但这就是我偶然发现这个
的问题。

#include < INTEGRITY.h>
#include< stdlib.h>
#include< stdio.h>
#include< pthread.h>
#include< signal.h>
#include< time.h>

timer_t l_timer_2;
int counter = 0;

// void测试(联合sigval sigval)
void测试(union sigval sigval)
{
printf(" Count \ nn);;
计数器++;

返回;
}

int main()
{
struct itimerspec l_timer_spec1;
struct sigevent l_tevent1;
int * ptr;
int status;

l_tevent1.sigev_notify = SIGEV_THREAD;
l_tevent1.sigev_value.sival_ptr =& l_timer_2;
l_tevent1.sigev_notify_function =测试;
l_tevent1.sigev_notify_attributes = NULL;
l_tevent1.sigev_signo = SIGRTMIN;
l_timer_spec1.it_interval。 tv_sec = 2;
l_timer_spec1.it_interval.tv_nsec = 0;
//设置后续计时器间隔
l_timer_spec1.it_value.tv_sec = 2;
l_timer_spec1.it_value.tv_nsec = 0;

//创建计时器
if(timer_create(CLOCK_REALTIME,& l_tevent1,& l_timer_2)< 0)
{
status = 1;
}
if(status == 0)
{
if(timer_settime(l_timer_2, 0,& l_timer_spec1,NULL)< 0)
{
status = 1;
}
}

ptr = malloc(1000000);

if(ptr == NULL)
printf(" NULL PTR\ n);

计数器++;

退出(0);
}
现在我知道这个代码snipet很没用,但它是对问题进行故障排除的结果。当malloc失败时,计时器停止按预期运行。经过一些研究后,似乎当malloc
失败时,返回NULL,由于资源不足,它实际上将所有可用内存标记为已分配。因此,计时器弹出时无法启动线程。
所以我的问题是,如果有的话,标准必须说明在这种情况下malloc的行为。
是否允许此行为,如果是这样,这是常见的。在我看来,有更多优雅的方法可以让它失败。
I originally posted the following code in a group discussing threads
but after further research I think I have a c question about the code.
I know there are a couple of non standard c includes here and the
POSIX stuff is non standard but this is how I stumbled onto this
question.

#include <INTEGRITY.h>
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <signal.h>
#include <time.h>

timer_t l_timer_2;
int counter =0;

//void Test(union sigval sigval)
void Test (union sigval sigval)
{
printf("Count\n");
counter++;

return;
}
int main()
{
struct itimerspec l_timer_spec1;
struct sigevent l_tevent1;
int *ptr;
int status;

l_tevent1.sigev_notify = SIGEV_THREAD;
l_tevent1.sigev_value.sival_ptr = &l_timer_2;
l_tevent1.sigev_notify_function = Test;
l_tevent1.sigev_notify_attributes = NULL;
l_tevent1.sigev_signo = SIGRTMIN;
l_timer_spec1.it_interval.tv_sec = 2;
l_timer_spec1.it_interval.tv_nsec = 0;
//set subsequent timer intervals
l_timer_spec1.it_value.tv_sec = 2;
l_timer_spec1.it_value.tv_nsec = 0;

//create timer
if (timer_create(CLOCK_REALTIME, &l_tevent1, &l_timer_2) < 0)
{
status = 1;
}

if (status == 0)
{
if (timer_settime(l_timer_2, 0, &l_timer_spec1, NULL) < 0 )
{
status = 1;
}
}

ptr = malloc(1000000);

if (ptr == NULL)
printf("NULL PTR\n");

counter++;

Exit(0);
}

Now I know this code snipet is pretty useless but it was the result of
troubleshooting a problem. When the malloc fails the timer stops
operating as expected. After some research it appears that when malloc
fails, returns NULL, due to not enough resources it actually marks all
the available memory as allocated. Due to this the timer can''t start
the thread when it pops. So my question is what, if anything, does the standard have to say about
the behavior of malloc in this situation. Is this behavior allowed and if so is it common. It seems to me that
there are many more gracefull ways for this to fail.




来自malloc上的c99标准......

如果空间无法分配,则返回空指针


这就是它。


- -

==============

不是学生

======== ======



From the c99 standard on malloc ...
"If the space cannot be allocated, a null pointer is returned"

And that''s it.

--
==============
Not a pedant
==============


这篇关于malloc失败时的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆