简体   繁体   English

在C中声明数组大小

[英]Declaring the array size in C

Its quite embarrassing but I really want to know... So I needed to make a conversion program that converts decimal(base 10) to binary and hex. 它非常令人尴尬,但我真的想知道...所以我需要制作一个转换程序,将十进制(以10为底)转换为二进制和十六进制。 I used arrays to store values and everything worked out fine, but i declared the array as int arr[1000]; 我使用数组存储值,并且一切正常,但是我将数组声明为int arr[1000]; because i thought 1000 was just an ok number, not too big, not to small...someone in class said " why would you declare an array of 1000? Integers are 32 bits". 因为我认为1000只是一个确定的数字,不是太大,也不是很小...在课堂上有人说“为什么要声明一个1000的数组?整数是32位”。 I was too embarrased to ask what that meant so i didnt say anything. 我太不好意思问这意味着什么,所以我什么也没说。 But does this mean that i can just declare the array as int arr[32]; 但这是否意味着我可以将数组声明为int arr[32]; instead? 代替? Im using C btw 我正在使用C btw

No, the int type has tipically a 32 bit size, but when you declare 不, int类型的大小通常为32位,但是当您声明

int arr[1000];

you are reserving space for 1000 integers, ie 32'000 bits, while with 您正在为1000个整数(即32'000位)保留空间,而

int arr[32];

you can store up to 32 integers. 您最多可以存储32个整数。

You are practically asking yourself a question like this: if an apple weighs 32 grams, I want to my bag to contain 1000 apples or 32 apples? 您实际上是在问自己一个这样的问题: 如果一个苹果重32克,我要在我的书包中装1000个苹果还是32个苹果?

Don't be embarrassed. 不要不好意思。 Fear is your enemy and in the end you will be perceived based on contexts that you have no hope of significantly influencing. 恐惧是您的敌人,最终,您将基于没有希望显着影响的环境而被感知。 Anyway, to answer your question, your approach is incorrect. 无论如何,要回答您的问题,您的方法是不正确的。 You should declare the array with a size completely determined by the number of positions used. 您应声明数组的大小完全取决于所使用的位置数。

Concretely, if you access the array at 87 distinct positions (from 0 to 86) then you need a size of 87. 具体来说,如果您在87个不同的位置(从0到86)访问数组,则需要大小为87。

0 to 4,294,967,295 is the maximum possible range of numbers you can store in 32 bits.If your number is outside this range you cannot store your number in 32 bits.Since each bit will occupy one index location of your array if you number falls in that range array size of 32 will do fine.for example consider number 9 it will be stored in array as a[]={1,0,0,1} . 04,294,967,295是您可以在32位中存储的最大数字范围。如果您的数字超出此范围,则不能以32位存储您的数字,因为如果您的数字落在该位中,则每个位将占据数组的一个索引位置range数组大小为32会很好。例如,考虑数字9,它将以a[]={1,0,0,1}形式存储在数组中。

In order to know the know range of numbers, your formula is 0 to (2^n -1) where n is the number of bits in binary. 为了知道数字的已知范围,您的公式是0 to (2^n -1) ,其中n是二进制数的位数。 means in the array size of 4 or 4 bits you can just store number from range 0 to 15 . 意味着在4或4位的数组中,您可以存储0 to 15

In C , integer datatype can store typically up to 2,147,483,647 and 4,294,967,295 if you are using unsigned integer. 在C中,如果您使用无符号整数,则整数数据类型通常最多可以存储2,147,483,6474,294,967,295 Since the maximum value, an integer data type can store in C is within the range of maximum possible number which can be expressed using 32 bits. 由于最大值,可以在C中存储的整数数据类型在可以使用32位表示的最大可能数的范围内。 It is safe to say that array size of 32 is the best size for defining an array.Sice you will never require more than 32 bits to express any number using an int. 可以肯定地说,数组大小为32是定义数组的最佳大小。使用int表示任何数字都不会超过32位。

I will use 我会用

int a = 42;
char bin[sizeof a * CHAR_BIT + 1];
char hex[sizeof a * CHAR_BIT / 4 + 1]

I think this include all possibility. 我认为这包括所有可能性。

Consider that also the 'int' type is ambiguous. 考虑到“ int”类型也是模棱两可的。 Generally it depends on the machine you're working on and at minimum its ranges are: -32767,+32767: 通常,它取决于您正在使用的计算机,并且其最小范围是:-32767,+ 32767:

https://en.wikipedia.org/wiki/C_data_types https://zh.wikipedia.org/wiki/C_data_types

Can I suggest to use the stdint types? 我可以建议使用stdint类型吗?

int32_t/uint32_t int32_t / uint32_t

What you did is okay. 你做的还好。 If that is precisely what you want to do. 如果那正是您想要做的。 C is a language that lets you do whatever you want. C是一种语言,可以让您做任何想做的事情。 Whenever you want. 无论你什么时候想要。 The reason you were berated on the declaration is because of 'hogging' memory. 之所以对您的声明rated之以鼻,是因为记忆力过大。 The thought being, how DARE YOU take up space that is possibly never used... it is inefficient. 这种想法是,您如何敢于占用可能从未使用过的空间...效率低下。

And it is. 是的。 But who cares if you just want to run a program that has a simple purpose? 但是谁在乎您是否只想运行目的简单的程序? A 1000 16 or 32 bit block of memory is weeeeeensy teeeeny tiny compared to computers from the way back times when it was necessary to watch over how much RAM you were taking up. 与从计算机回溯到需要注意要占用多少RAM的计算机相比,一个1000 16或32位的内存块太小了。 So - go ahead. 所以请继续。

But what they should have said next is how to avoid that. 但是他们接下来应该说的是如何避免这种情况。 More on that at the end - but first a thing about built in data types in C. 最后有更多内容-但首先要讲的是使用C内置数据类型。

An int can be 16 or 32 bits depending on how you declare it. 一个int可以是16或32位,具体取决于您声明的方式。 And your compiler's settings... 还有编译器的设置...

A LONG int is 32. LONG整数是32。

consider: 考虑:

short int x = 10;    // declares an integer that is 16 bits 
signed int x = 10;   // 32 bit integer with negative and positive range
unsigned int x = 10  // same 32 bit integer - but only 0 to positive values

To specifically code a 32 bit integer you declare it 'long' 要专门编码一个32位整数,请声明为“ long”

long int = 10;           // 32 bit
unsigned long int = 10;  // 32 bit 0 to positive values

Typical nomenclature is to call a 16 bit value a WORD and a 32 bit value a DWORD - (double word). 典型的术语是将16位值称为WORD,将32位值称为DWORD-(双字)。 But why would you want to type in: 但是,为什么要输入:

long int x = 10;

instead of: 代替:

int x = 10;

?? ?? For a few reasons. 由于一些原因。 Some compilers may handle the int as a 16 bit WORD if keeping up with older standards. 如果符合较早的标准,某些编译器可能会将int作为16位WORD处理。 But the only real reason is to maintain a convention of strongly typecasted code. 但是,唯一真正的原因是要维护强类型转换代码的约定。 Make it read directly what you intend it to do. 使它直接读取您打算做什么。 This also helps in readability. 这也有助于提高可读性。 You will KNOW when you see it = what size it is for sure, and be reminded whilst coding. 当您看到它时,您将知道=肯定是什么大小,并且在编码时会被提醒。 Many many code mishaps happen for lack of attention to code practices and naming things well. 由于缺乏对代码实践的关注和对事物的良好命名而发生了许多代码不幸事故。 Save yourself hours of headache later on by learning good habits now. 通过现在学习良好的习惯,可以节省以后的头痛时间。 Create YOUR OWN style of coding. 创建您自己的编码风格。 Take a look at other styles just to get an idea on what the industry may expect. 看一下其他样式,以了解该行业的期望。 But in the end you will find you own way in it. 但最终,您会发现自己的方式。

On to the array issue ---> So, I expect you know that the array takes up memory right when the program runs. 关于数组问题--->因此,我希望您知道程序运行时数组会占用内存。 Right then, wham - the RAM for that array is set aside just for your program. 此时,wham-该阵列的RAM仅留给您的程序使用。 It is locked out from use by any other resource, service, etc the operating system is handling. 操作系统正在处理的任何其他资源,服务等都无法使用它。

But wouldn't it be neat if you could just use the memory you needed when you wanted, and then let it go when done? 但是,如果您可以仅在需要时使用所需的内存,然后在完成后将其释放,那岂不是整洁的呢? Inside the program - as it runs. 在程序内部-运行时。 So when your program first started, the array (so to speak) would be zero. 因此,当您的程序首次启动时,该数组(可以说)为零。 And when you needed a 'slot' in the array, you could just add one.... use it, and then let it go - or add another - or ten more... etc. 当您在阵列中需要一个“插槽”时,您可以只添加一个...使用它,然后放开-或添加另一个-或再添加十个...等等。

That is called dynamic memory allocation. 这就是所谓的动态内存分配。 And it requires the use of a data type that you may not have encountered yet. 并且它需要使用您可能尚未遇到的数据类型。 Look up "Pointers in C" to get an intro. 查找“ C语言中的指针”以获取简介。

If you are coding in regular C there are a few functions that assist in performing dynamic allocation of memory: 如果您使用常规C进行编码,则有一些函数可帮助执行内存的动态分配:

 malloc and free      ~ in the alloc.h library routines

in C++ they are implemented differently. 在C ++中,它们的实现方式有所不同。 Look for: 寻找:

 new and delete

A common construct for handling dynamic 'arrays' is called a "linked-list." 用于处理动态“数组”的常见构造称为“链接列表”。 Look that up too... 看起来也...

Don't let someone get your flustered with code concepts. 不要让别人为代码概念而烦恼。 Next time just say your program is designed to handle exactly what you have intended. 下次再说一次,您的程序旨在处理您的预期目标。 That usually stops the discussion. 这通常会停止讨论。

  • Atomkey 原子键

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM