免费的()上堆栈存储器 [英] free() on stack memory

查看:94
本文介绍了免费的()上堆栈存储器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我支持Solaris上的一些C code,我已经看到一些奇怪的,至少我认为是:

I'm supporting some c code on Solaris, and I've seen something weird at least I think it is:

char new_login[64];
...
strcpy(new_login, (char *)login);
...
free(new_login);

我的理解是,既然变量是一个局部数组的内存来自堆栈,并不需要被释放,而且因为没有任何的malloc /释放calloc / realloc的使用行为是不确定的。

My understanding is that since the variable is a local array the memory comes from the stack and does not need to be freed, and moreover since no malloc/calloc/realloc was used the behaviour is undefined.

这是一个实时系统,所以我认为这是周期的浪费。我失去了一些东西明显?

This is a real-time system so I think it is a waste of cycles. Am I missing something obvious?

推荐答案

您只能free()的东西(),释放calloc()或realloc()函数。堆栈收益不确定的行为释放的东西,你很幸运,这不会导致程序崩溃,甚至更糟。

You can only free() something you got from malloc(),calloc() or realloc() function. freeing something on the stack yields undefined behaviour, you're lucky this doesn't cause your program to crash, or worse.

认为这是一个严重的错误,并删除该行尽快。

Consider that a serious bug, and delete that line asap.

这篇关于免费的()上堆栈存储器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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