在C中没有垃圾收集器吗? [英] Isn't there anything like garbage collector in C?

查看:68
本文介绍了在C中没有垃圾收集器吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个程序来保存数字而不需要任何

重复。

我以为我可以从存储数组中的第一个数字开始

,大小为1,

如果下一个数字不在我创建的数组中,我保存

第一个数字和新输入数字

在一个大小为2的新数组中从内存中删除第一个数组。

这一直持续到程序输入总共20个数字。 />

但是你可以自己从内存中删除东西吗?

C中没有垃圾收集器吗?

I''m trying to write a program that saves numbers without any
duplications.
I was thinking I could start by storing the first number in an array
with size one,
and if the next number is not in the array I''ve created, I save the
first number and the new input number
in a new array with size two and delete the first array from memory.
This goes on until total of twenty numbers are input into the program.

But is it possible to delete things from memory yourself?
Isn''t there anything like garbage collector in C?

推荐答案

xi*****@gmail.com 写道:

我正在尝试编写一个程序来保存数字而不需要任何

重复。

我我以为我可以先将第一个数字存储在一个数组中,然后用
存储大小一,

如果下一个数字不在我创建的数组中,我保存了

第一个数字和新输入数字

在一个大小为2的新数组中,从内存中删除第一个数组。

这一直持续到程序输入总共20个数字。


但是你可以自己从内存中删除东西吗?

C中是否有类似垃圾收集器的东西?
I''m trying to write a program that saves numbers without any
duplications.
I was thinking I could start by storing the first number in an array
with size one,
and if the next number is not in the array I''ve created, I save the
first number and the new input number
in a new array with size two and delete the first array from memory.
This goes on until total of twenty numbers are input into the program.

But is it possible to delete things from memory yourself?
Isn''t there anything like garbage collector in C?



这听起来像是几个不同的问题。


是的,你可以自己从记忆中删除东西,但只有你自己创造了

他们自己先。在教科书中查找malloc()和free()[以及可能还有

realloc()]。


不,标准中没有垃圾收集器C.只是要明确

我们正在谈论同样的事情,垃圾收集器*自动*

清理你已经停止使用后分配的内存

- 虽然它的概念是一旦你停止使用它,可以和你的b $ b不同,并且经常犯错误的是保留东西

分配的时间比必要的长一些。


至于你的程序设计,如果你知道你要输入

二十个数字,为​​什么不为二十个数字分配空间来开始

?使用另一个变量来存储到目前为止输入了多少个数字。


-

Philip Potter pgp< atdoc.ic.ac.uk

This sounds like a few different questions.

Yes, you can delete things from memory yourself, but only if you created
them yourself first. Look up malloc() and free() [and possibly also
realloc()] in your textbook.

No, there is no garbage collector in standard C. Just to be clear that
we are talking about the same thing, a garbage collector *automatically*
cleans up memory which you have allocated once you have stopped using it
-- although its idea of "once you have stopped using it" can be
different from yours, and often errs on the side of keeping things
allocated a bit longer than necessary.

As for your program design, if you know that you are going to input
twenty numbers, why don''t you allocate space for twenty numbers to begin
with? Use another variable to store how many numbers have been input so far.

--
Philip Potter pgp <atdoc.ic.ac.uk


10月8日,6月12日,Philip Potter< p ... @ see.sig.invalidwrote:
On 10 8 , 6 12 , Philip Potter <p...@see.sig.invalidwrote:

xicl ... @ gmail.com写道:
xicl...@gmail.com wrote:

我正在尝试编写一个程序来保存数字而不需要任何重复的b
重复。

我以为我可以先将第一个数字存储在数组中

,大小为1,

如果下一个数字不是在我创建的数组中,我在第2个大小的新数组中保存了

第一个数字和新输入数字

,并从内存中删除了第一个数组。

这种情况​​一直持续到程序中总共输入了20个数字。
I''m trying to write a program that saves numbers without any
duplications.
I was thinking I could start by storing the first number in an array
with size one,
and if the next number is not in the array I''ve created, I save the
first number and the new input number
in a new array with size two and delete the first array from memory.
This goes on until total of twenty numbers are input into the program.


但是你可以自己从内存中删除东西吗?

C中的垃圾收集器是不是有什么东西? ?
But is it possible to delete things from memory yourself?
Isn''t there anything like garbage collector in C?



这听起来像是几个不同的问题。


是的,你可以自己从记忆中删除东西,但只有你自己创造了

他们自己先。在教科书中查找malloc()和free()[以及可能还有

realloc()]。


不,标准中没有垃圾收集器C.只是要明确

我们正在谈论同样的事情,垃圾收集器*自动*

清理你已经停止使用后分配的内存

- 虽然它的概念是一旦你停止使用它,可以和你的b $ b不同,并且经常犯错误的是保留东西

分配的时间比必要的长一些。


至于你的程序设计,如果你知道你要输入

二十个数字,为​​什么不为二十个数字分配空间来开始

?使用另一个变量来存储到目前为止输入的数量。


-

Philip Potter pgp< atdoc.ic.ac.uk


This sounds like a few different questions.

Yes, you can delete things from memory yourself, but only if you created
them yourself first. Look up malloc() and free() [and possibly also
realloc()] in your textbook.

No, there is no garbage collector in standard C. Just to be clear that
we are talking about the same thing, a garbage collector *automatically*
cleans up memory which you have allocated once you have stopped using it
-- although its idea of "once you have stopped using it" can be
different from yours, and often errs on the side of keeping things
allocated a bit longer than necessary.

As for your program design, if you know that you are going to input
twenty numbers, why don''t you allocate space for twenty numbers to begin
with? Use another variable to store how many numbers have been input so far.

--
Philip Potter pgp <atdoc.ic.ac.uk




实际上,这是我正在使用的教科书中的问题之一

学习C.

要求是我需要使用尽可能小的数组,我认为
意味着我从一开始就不能使用20号数组。

正如其中一个回复中提到的那样,保持创建和销毁数组的效果似乎效率低下,我正试图找到另一种方式。

谢谢所有的帮助!



Actually, this is one of the problems in the textbook I''m using to
study C.
The requirement is that I need to use smallest possible array, which I
took to mean that I can''t use an array of size 20 from the start.
As mentioned in one of the repies, it does seem inefficient to keep
creating and destroying arrays, and I''m trying to find another way.
Thanks for all the help!


xi ***** @ gmail.com 写道:
xi*****@gmail.com wrote:

10 8,6 12,Philip Potter< p ... @ see.sig.invalidwrote:
On 10 8 , 6 12 , Philip Potter <p...@see.sig.invalidwrote:

> xicl ... @ gmail.com写道:
>xicl...@gmail.com wrote:

我正在尝试wri这是一个节省数字的程序,没有任何重复的b
。我原以为我可以将第一个

编号存储在一个大小为1的数组中,如果下一个数字不在
我创建的数组中,我将第一个数字和新输入

数保存在一个大小为2的新数组中,并从

内存中删除第一个数组。这种情况一直持续到程序中输入总共二十个数字为


I''m trying to write a program that saves numbers without any
duplications. I was thinking I could start by storing the first
number in an array with size one, and if the next number is not in
the array I''ve created, I save the first number and the new input
number in a new array with size two and delete the first array from
memory. This goes on until total of twenty numbers are input into
the program.



< snip>

<snip>


>至于你的程序设计,如果你知道你要输入二十个数字,为​​什么不为二十个数字分配空间来开始?使用另一个变量来存储到目前为止输入的数量。
>As for your program design, if you know that you are going to input
twenty numbers, why don''t you allocate space for twenty numbers to
begin with? Use another variable to store how many numbers have been
input so far.


实际上,这是我正在使用的教科书中的问题之一

研究C.

要求是我需要使用尽可能小的数组,我用b $ b来表示我不能从一开始就使用20号数组。

正如其中一个回复中提到的那样,保持创建和销毁数组的效果似乎效率低下,而我正试图找到另一种方式。
Actually, this is one of the problems in the textbook I''m using to
study C.
The requirement is that I need to use smallest possible array, which I
took to mean that I can''t use an array of size 20 from the start.
As mentioned in one of the repies, it does seem inefficient to keep
creating and destroying arrays, and I''m trying to find another way.



您可以一次性使用malloc创建一个20元素数组,程序

启动,然后将唯一值填入数组,当你收到它们b / b $ b $时,跟踪所用元素的数量。输入

完成后,只需调用realloc,就可以将数组重新调整为只使用

元素的数量。

这种方式最多有两个分配调用,在20个

唯一值的情况下,只有一个。


请注意尽管如此对于小到二十的数字,重新大小是指可能

实际上并没有释放不需要的元素。这取决于你对malloc和realloc实现的
实现。

You could create a 20 element array using malloc in one go, at program
start-up, and then fill in unique values into the array, as you receive
them, keeping track of the number of used elements. Then after input
has finished, you can re-size the array to just the number of used
elements with a single call to realloc.

This way there are at most two allocation calls and in the case of 20
unique values, just one.

Be aware though that for numbers as small as twenty the "re-size" may
not actually deallocate the unneeded elements. It depends on your
implementation of malloc and realloc.


这篇关于在C中没有垃圾收集器吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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