简体   繁体   English

C中的动态内存分配:为什么会出现错误?

[英]Dynamic memory allocation in C: why do I get an error?

Below there is a unfinished code for my program, at the current stage, however, I am getting errors (Xcode log: Subscripted value is not array, pointer or vector ). 在当前阶段,下面是我的程序的未完成代码,但是,我遇到了错误(Xcode日志:下Subscripted value is not array, pointer or vector )。 I suppose that it has to do with memory allocation. 我想这与内存分配有关。 This error occurs in the if statement when I try to assign value of 1 to (*map[x2][y2]).exist and in map[x1][y1] = NULL; 当我尝试将值1分配给(*map[x2][y2]).existmap[x1][y1] = NULL;时,会在if语句中发生此错误map[x1][y1] = NULL; . Could you please show the proper way of assigning values to such variables. 您能否说明为这些变量分配值的正确方法。

Thank you in advance! 先感谢您!

#include <stdio.h>
#include <stdlib.h>

typedef struct{
    int num;
    _Bool exist;
}name;

int main(void){

name* map[10][10];
name* guy;

guy = (name*)malloc(10*sizeof(name));

int x1, y1, x2, y2;
int m, n, o;

for(m = 0; m < 10; m++){
    for(n = 0; n < 10; n++){
        map[m][n] = NULL;
    }
}

for(o = 0; o < 10; o++){

    (*(guy+o)).num = rand() % 4;
    (*(guy+o)).exist = 1;

    do{
        m = rand() % 10;
        n = rand() % 10;
    }while (map[m][n] != NULL);
    map[m][n] = guy + o;
}

if(map[x2][y2] == NULL){

    name *map = malloc(10*10*sizeof(name));
    (*map[x2][y2]).exist = 1;
    map[x1][y1] = NULL;

}

return 0;
}

Besides the x2 and y2 not having a defined value - they do not default to 0, and the map variable in your if statement hiding the map outside your if statement, you seem to be having problems with understanding how to allocate memory as well as accessing it after it is allocated. 除了x2和y2没有定义的值-它们没有默认为0,并且if语句中的map变量将映射隐藏在if语句之外,您似乎在理解如何分配内存以及访问方面遇到问题分配之后。

I suggest fixing the guy variable first and then using that experience to apply to the map variable. 我建议先修复guy变量,然后再利用经验将其应用于map变量。

The guy variable as you have it written, points to a block of memory that can hold 10 name structures. 您编写的guy变量指向一个内存块,该内存块可以容纳10个名称结构。 That doesn't mean that there are 10 name structures there, it just means it is a block of memory large enough to hold 10 name structures. 这并不意味着那里有10个名称结构,而是意味着它是一个足以容纳10个名称结构的内存块。 Note that the block is not sized to hold 10 pointers to name structures but 10 actual name structures. 注意,该块的大小不能容纳10个指向名称结构的指针,而是10个实际名称结构。

When you do arithmetic on the guy variable like (guy+o) you are not moving o name structures into the memory block since guy is of size pointer to name, not size of name. 当对(guy+o)类的guy变量进行算术运算时,您不会将o名称结构移入内存块,因为guy是指向名称的大小指针,而不是名称的大小。 If pointers on your system are 4-bytes in size and the name structure is maybe 8-bytes in size then you are not moving the correct number of bytes into the block, so you are not pointed at the right place. 如果系统上的指针大小为4个字节,并且名称结构的大小可能为8个字节,则您没有将正确数量的字节移入块中,因此未将指针指向正确的位置。

If you want to initialize the 10 names in guy then do yourself a favor until you are really good with pointers - create the guy variable as an array of name pointers. 如果要初始化Guy中的10个名称,请帮自己一个忙,直到您对指针真的很满意为止-将guy变量创建为名称指针数组。

So the guy definition becomes 所以家伙的定义变成

   name* guy[10];

and the initialization of 10 names in guy becomes 并在Guy中初始化10个名称

   for ( m = 0; m < 10; m++ ) {
      guy[m] = malloc( sizeof( name ) );
      if ( guy[m] ) {
         guy[m]->num = rand() % 4;
         guy[m]->exist = 1;
      }
   }

instead of 代替

guy = (name*)malloc(10*sizeof(name));

Since guy will now be recognized as a pointer to an array of name pointers, when you do arithmetic like (guy + o) it will use a pointer to a different name structure. 由于Guy现在将被识别为指向名称指针数组的指针,因此当您执行类似(guy + o)算术运算时,它将使用指向不同名称结构的指针。 But I would avoid that to make your life easier. 但我会避免这样做,以使您的生活更轻松。 Just use guy[o] to access the pointer at that location in the guy array. 只需使用guy[o]访问在guy数组中该位置的指针。

You should be able to apply the same rules to map to make your code work. 您应该能够应用相同的规则进行映射,以使代码正常工作。

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

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