简体   繁体   English

C 数组/指针问题

[英]C Array/Pointer problem

Going through K&R I too a look at the following code:通过 K&R,我也查看了以下代码:

#define ALLOCSIZE 1000
static char allocbuf[MAXLINE];
static char *allocp = allocbuf

char *alloc(int n){
       if (allocbuf+ALLOCSIZE-allocp>=n){
               allocp+=n;
               return allocp-n;
       }
       else { ... }

I'm afraid my question is very simple, but I can't get my head round the "if" line.恐怕我的问题很简单,但我无法理解“如果”这一行。 What value is allocbuf taking? allocbuf 取什么值? It is a char array, right?它是一个字符数组,对吧? I looked back at the array stuff in the book, but it didn't help.我回头看了看书中的数组内容,但没有帮助。 allocp initially points to the zeroth element of the array, right? allocp 最初指向数组的第 0 个元素,对吗?

allocbuf is an array of type char [] , but in many contexts the identifier itself decays to a pointer of type char * , holding the starting address of the array. allocbufchar []类型的数组,但在许多情况下,标识符本身会衰减为char *类型的指针,其中包含数组的起始地址。 Note that this doesn't mean that allocbuf is a pointer, it is still an array.请注意,这并不意味着allocbuf是一个指针,它仍然是一个数组。

So, the condition of the if statement performs some pointer arithmetic.因此, if语句的条件执行一些指针运算。

While they're declared in different ways, allocp and allocbuf are both char arrays (char*) and allocp effectively points on the first char of the buffer after initializing, and after getting through the "if" body, to the same adress + the number of bytes allocated, and this number increases with each new cycle in the "if" body.虽然它们以不同的方式声明,但 allocp 和 allocbuf 都是 char arrays (char*) 并且 allocp 在初始化之后有效地指向缓冲区的第一个字符,并且在通过“if”主体之后,指向相同的地址 +分配的字节数,并且这个数字随着“if”正文中的每个新周期而增加。 To sum up, it points on the first free char in the buffer.总而言之,它指向缓冲区中的第一个空闲字符。 The "if" line you're stuck with is aimed to verify if there's enough place for allocating n chars in the allocbuf, the static buffer.您遇到的“if”行旨在验证是否有足够的空间在 allocbuf、static 缓冲区中分配 n 个字符。 This line could be discomposed as follows:这条线可以分解如下:

char* static_buffer_beginning = allocbuf;
char* static_buffer_ending = static_buffer_beginning + MAXLINE;
int   nb_chars_still_available = static_buffer_ending - allocp;
if (nb_chars_still_available >= n) {

I'm just a little confused by the "ALLOCSIZE" which appears in your code: what's his value, where does it come from?, I assume it's a typo or something like that and that its value is equal to MAXLINE.我对您的代码中出现的“ALLOCSIZE”有点困惑:他的价值是什么,它来自哪里?,我认为这是一个错字或类似的东西,它的价值等于 MAXLINE。 but would like to be sure for not to give you a wrong answer.但想确保不会给你一个错误的答案。

Think of allocbuf as the pointer to the beginning of your RAM, say 0. Then allocbuf+ALLOCSIZE will be pointing to the end of your RAM.将 allocbuf 视为指向 RAM 开头的指针,例如 0。然后 allocbuf+ALLOCSIZE 将指向 RAM 的末尾。 allocp is pointing to the end of the allocated region, somewhere in the middle of your RAM. allocp 指向分配区域的末尾,位于 RAM 中间的某个位置。 So allocbuf+ALLOCSIZE-allocp will give you the free memory size.所以 allocbuf+ALLOCSIZE-allocp 会给你免费的 memory 大小。 The if statement checks if your requested allocation size (n) is less than the free memory available. if 语句检查您请求的分配大小 (n) 是否小于可用的免费 memory。

allocbuf is a static array, actually it points to first element of contiguous set of chars (the array). allocbuf 是一个 static 数组,实际上它指向连续字符集(数组)的第一个元素。 allocp is another pointer to the contiguous array and you can change its value to point to the array elements. allocp 是另一个指向连续数组的指针,您可以将其值更改为指向数组元素。

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

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