是否有可能“摧毁”一个局部变量? [英] Is it possible to "destroy" a local variable ?

查看:82
本文介绍了是否有可能“摧毁”一个局部变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




在某个C程序中我需要移植到某个架构,我发送到

函数参数char [50000]预定义值。在

函数中,读取此数据并计算某些内容。但是对于一些

的原因,我无法在这里解释(太长时间)内存真的很小,而且我需要使用这个char使用的空间[50000] 。然后

我想知道它是否可以以某种方式删除或销毁。之后我

需要使用malloc并且免费,我想如果我可以删除,那么我会有更多的记忆。这个。任何提示?

Hi,

in some C program I need to port to some architecture, I send to a
function the parameter char[50000] with predefined values. Inside the
function, this data is read and something is calculated. But for some
reasons that I can not explain here (too long) the memory is really
small and I would need to use the space used by this char[50000]. Then
I wonder if it can be deleted or destroyed in some way. Afterwards I
need to use malloc and free and I think I would have more memory if I
could "delete" this. Any hint ?

推荐答案

5月30日凌晨1点44分,Horacius ReX< horacius .... @ gmail.comwrote :
On May 30, 1:44 am, Horacius ReX <horacius....@gmail.comwrote:




在某个C程序中我需要移植到某个架构,我发送给

使用预定义值运行参数char [50000]。在

函数中,读取此数据并计算某些内容。但是对于一些

的原因,我无法在这里解释(太长时间)内存真的很小,而且我需要使用这个char使用的空间[50000] 。然后

我想知道它是否可以以某种方式删除或销毁。之后我

需要使用malloc并且免费,我想如果我可以删除,那么我会有更多的记忆。这个。任何提示?
Hi,

in some C program I need to port to some architecture, I send to a
function the parameter char[50000] with predefined values. Inside the
function, this data is read and something is calculated. But for some
reasons that I can not explain here (too long) the memory is really
small and I would need to use the space used by this char[50000]. Then
I wonder if it can be deleted or destroyed in some way. Afterwards I
need to use malloc and free and I think I would have more memory if I
could "delete" this. Any hint ?



char [50000]不需要存在于C89实现中。

C99保证对象的大小至少为65536字节。

一种删除的方式这样的对象是:


{char array [N]; / *做数组的东西* /}

/ *数组不再存在* /

我的建议是:

将你的功能分成两个功能。第一个应该做你所说的

计算,例如:


{char array [50000]; F1(数组); }

/ * f2(...)* /


这可能会,也可能不会奏效。标准C不保证任何东西。

A char[50000] needs not to exist in a C89 implementation.
C99 guarantees that objects can be at least 65536 bytes in size.
A way to "delete" such object is:

{ char array[N]; /* do stuff with array */ }
/* array no longer exists */
My suggestion is:
Split your function to two functions. The first should do the
calculations you speak of, such as:

{ char array[50000]; f1(array); }
/* f2(...) */

This may, or may not work. Standard C doesn''t guarantee anything.




" Horacius ReX" < ho ********** @ gmail.comwrote in message

news:52 ******************** ************** @ f63g2000 hsf.googlegroups.com ...

"Horacius ReX" <ho**********@gmail.comwrote in message
news:52**********************************@f63g2000 hsf.googlegroups.com...



在一些C程序中
我需要移植到某个架构,我发送一个带有预定义值的参数char [50000]的函数
。在

函数中,读取此数据并计算某些内容。但是对于一些

的原因,我无法在这里解释(太长时间)内存真的很小,而且我需要使用这个char使用的空间[50000] 。然后

我想知道它是否可以以某种方式删除或销毁。之后我

需要使用malloc并且免费,我想如果我可以删除,那么我会有更多的记忆。这个。任何提示?
Hi,

in some C program I need to port to some architecture, I send to a
function the parameter char[50000] with predefined values. Inside the
function, this data is read and something is calculated. But for some
reasons that I can not explain here (too long) the memory is really
small and I would need to use the space used by this char[50000]. Then
I wonder if it can be deleted or destroyed in some way. Afterwards I
need to use malloc and free and I think I would have more memory if I
could "delete" this. Any hint ?



这个50KB存在于何处:作为初始化(静态)数据,作为你建议的本地

变量(这是一个很大的变量),或者在堆上?


所以你用这个50KB的数组调用一个函数,那么你不再需要那个

数组而想要使用它是为了别的吗?


如果是初始化的数据,可能你会用

覆盖新的东西,但你不能轻松将其添加到malloc的内存池中;你需要明确地使用内存,比如设置一个数组

指向它的开头(或创建特殊版本的malloc / free到

使用该块)。


本地变量(或堆栈)数据也是如此 - 如果你留在那里

功能。如果从函数返回,将恢复内存。但是

即使那时堆栈和堆(malloc)内存也可能无法共享,因此它可能只是作为备用堆栈内存存在而不是扩展堆。


最好的解决方案是为这个50KB的块使用malloc-ed内存,提供

它可以在没有初始化代码/数据本身的情况下进行初始化服用

50KB。


-

Bartc

Where does this 50KB exist: as initialised (static) data, as a local
variable as you suggest (that''s a big variable), or on the heap?

So you call a function with this 50KB array, then you no longer need that
array and would like to use it for something else?

If it''s initialised data, probably you will be able to overwrite with
something new, but you can''t easily add it to the memory pool of malloc; you
would have to make use of the memory explicitly, like setting an array
pointer to the start of it (or creating special versions of malloc/free to
use that block).

The same goes for local variable (or ''stack'') data - if you stay in that
function. The memory will be recovered if you return from the function. But
even then ''stack'' and ''heap'' (malloc) memory may not be shared so it could
just exist as spare stack memory and not extend the heap.

Best solution would be to use malloc-ed memory for this 50KB block, provided
it can be initialised without the initialisation code/data itself taking
50KB.

--
Bartc


Horacius ReX写道:
Horacius ReX wrote:




在某些C程序中我需要移植到某个架构,我发送给

使用预定义值运行参数char [50000]。在

函数中,读取此数据并计算某些内容。但是对于一些

的原因,我无法在这里解释(太长时间)内存真的很小,而且我需要使用这个char使用的空间[50000] 。
Hi,

in some C program I need to port to some architecture, I send to a
function the parameter char[50000] with predefined values. Inside the
function, this data is read and something is calculated. But for some
reasons that I can not explain here (too long) the memory is really
small and I would need to use the space used by this char[50000].



这就是工会的用途。


-

pete

That''s what a union is for.

--
pete


这篇关于是否有可能“摧毁”一个局部变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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