有多少个字符可以在字符数组? [英] How many chars can be in a char array?

查看:137
本文介绍了有多少个字符可以在字符数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#define HUGE_NUMBER ???

char string[HUGE_NUMBER];
do_something_with_the_string(string);

我不知道会是什么,我可以添加到一个字符数组,而不用担心任何潜在的内存问题,缓冲区溢出或类似的最大数量。我想获取用户输入进去,可能是最大的可能。

I was wondering what would be the maximum number that I could add to a char array without risking any potential memory problems, buffer overflows or the like. I wanted to get user input into it, and possibly the maximum possible.

推荐答案

见杰克·克莱恩这种反应(见的原帖):

See this response by Jack Klein (see original post):

原来的C标准(ANSI 1989年/ ISO
  1990)要求编译器
  成功转换至少一个
  含有至少一个程序
  例如一套环境的
  限制。其中的一个界限正在被
  能够创建至少一个对象
  32,767字节。

The original C standard (ANSI 1989/ISO 1990) required that a compiler successfully translate at least one program containing at least one example of a set of environmental limits. One of those limits was being able to create an object of at least 32,767 bytes.

这最低限度是在提高
  1999年更新了C标准是在
  至少65,535字节。

This minimum limit was raised in the 1999 update to the C standard to be at least 65,535 bytes.

没有C实现的需要
  提供对象大于
  大小,这意味着它们不需要
  以允许整数更大的阵列
  比(INT)(65535 /的sizeof(int)的)。

No C implementation is required to provide for objects greater than that size, which means that they don't need to allow for an array of ints greater than (int)(65535 / sizeof(int)).

在非常具体而言,现代
  计算机,这是不可能的说
  提前多大的数组可以
  创建。它可以依靠的东西像
  的物理内存量
  安装在计算机中,所述量
  的由OS提供的虚拟存储器,
  其它任务,驱动器的数量,
  和方案已在运行,以及如何
  多的内存使用。所以,你的
  程序可以是能够使用更多或
  更少的内存运行比今天
  昨天可以使用,或者它可以
  用明天。

In very practical terms, on modern computers, it is not possible to say in advance how large an array can be created. It can depend on things like the amount of physical memory installed in the computer, the amount of virtual memory provided by the OS, the number of other tasks, drivers, and programs already running and how much memory that are using. So your program may be able to use more or less memory running today than it could use yesterday or it will be able to use tomorrow.

许多平台将他们的严格
  自动对象的限制,这是
  一个函数的内部定义的那些
  不使用的静态
  关键词。在一些平台上,你可以
  创造更大的阵列,如果他们
  静态或动态分配。

Many platforms place their strictest limits on automatic objects, that is those defined inside of a function without the use of the 'static' keyword. On some platforms you can create larger arrays if they are static or by dynamic allocation.

现在,提供一个稍微更有针对性的回答,不声明巨大的数组来避免缓冲区溢出。这是接近人能想到在C.而最糟糕的做法,花一些时间编写好的code,并小心地确保不发生缓冲区溢出。另外,如果你不知道你的阵列提前大小,看看的malloc ,它可能会派上用场:P

Now, to provide a slightly more tailored answer, DO NOT DECLARE HUGE ARRAYS TO AVOID BUFFER OVERFLOWS. That's close to the worst practice one can think of in C. Rather, spend some time writing good code, and carefully make sure that no buffer overflow will occur. Also, if you do not know the size of your array in advance, look at malloc, it might come in handy :P

这篇关于有多少个字符可以在字符数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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