简体   繁体   English

指针数组的分段错误

[英]Segmentation Fault with pointers arrays

I have a Segmentation fault according to my debugger but I don't know why. 根据我的调试器,我遇到了Segmentation fault ,但我不知道为什么。 Here is the code involved. 这是涉及的代码。 Did I miss something ? 我错过了什么 ?

typedef struct Process Process;
struct Process {
    unsigned int m_id;
    unsigned int m_size;
    int m_startAddr;
};

    unsigned int** memory = calloc(MEMORY_SIZE, sizeof(unsigned int*));
    Process** processList = calloc(MEMORY_SIZE, sizeof(Process*));
    unsigned int itr;
    unsigned int nb_process = 0;
    int previous_process = -1;



    for(itr = 0; itr < MEMORY_SIZE; ++itr)
    {
      if(memory[itr] != NULL)
      {  
        previous_process = *memory[itr]; // works but risk of integer overflow
        processList[nb_process]->m_id = *memory[itr]; // segfault
        processList[nb_process]->m_size = 1;         
        processList[nb_process]->m_startAddr = itr;

        nb_process++;
     }

    }
 }

EDIT : I tried to make the following changes : 编辑:我试图进行以下更改:

Process* processList = calloc(MEMORY_SIZE,sizeof(Process));
unsigned int** memory = calloc(MEMORY_SIZE, sizeof(unsigned int*));
unsigned int nb_process = 0;
int previous_process = -1;
unsigned int itr;
Process temp;


for(itr = 0; itr < MEMORY_SIZE; ++itr)
    {
        /* if memory unit is not occupied */
        if(memory[itr] != NULL)
        {
            /* if process is not known yet */
            if(*memory[itr] != previous_process)
            {
                previous_process = *memory[itr];
                printf("previous_process %u \n", previous_process);
                temp.m_id = *memory[itr];
                temp.m_size = 1;
                temp.m_startAddr = itr;

                processList[nb_process] = temp;

                nb_process++;

            }
            /* if the process is already known */
            else
            {
                printf("size %u \n", processList[nb_process].m_size);
                processList[nb_process].m_size++;
            }
        }
    }

The output of previous_process is correct. previous_process的输出正确。 However, the output of size got a problem. 但是,大小的输出出现了问题。 First, it has always two values below what it should (14 instead of 16, 30 instead of 32; etc...) at the end of the for loop. 首先,在for循环的末尾,它总是有两个值低于应有的值(14而不是16,30而不是32;等等...)。 Worse, the count start at 0, while it should start at 1, since i initialize temp.m_size with one before copying it into processList. 更糟糕的是,计数从0开始,而应从1开始,因为我在将temp.m_size初始化为1之前将其复制到processList中。 So the copy doesn't work... Why ? 所以该副本不起作用...为什么? Should i use memcpy ? 我应该使用memcpy吗?

You do this: 你做这个:

unsigned int** memory = calloc(MEMORY_SIZE, sizeof(unsigned int*));
*memory[0]

You allocated space for an array of pointers, but then you dereference those pointers. 您为指针数组分配了空间,但是随后取消了对这些指针的引用。 But they are all zero (which on most systems is NULL, but in any case is not a valid pointer because it came from nowhere). 但是它们全为零(在大多数系统上为NULL,但无论如何都不是有效的指针,因为它来自任何地方)。 That can't work. 那行不通。

In addition to John's answer above you are also allocating an array of pointers 除了以上约翰的回答,您还分配了一个指针数组

Process** processList = calloc(MEMORY_SIZE, sizeof(Process*));
unsigned int nb_process = 0;

You must ensure that each of that array of pointers is initialized with something valid before de-referencing them as they are. 您必须确保使用正确的某种初始化指针数组,然后再按原样取消引用。

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

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