简体   繁体   English

我应该得到这个错误:可变大小的对象可能未初始化吗?

[英]Should I be getting this error : variable-sized object may not be initialized?

I'm making a hash table with an array of 9,997 buckets. 我正在创建一个带有9,997个存储桶的数组的哈希表。 I am just currently trying to initialize the entire hash table array with the collisionCount to 0 and the pointers inside the bucket to create the linked list to NULL. 我现在只是在尝试将整个冲突表数组初始化为将冲突计数设为0,并将存储桶中的指针创建为空,以创建链接列表。 The supplied "data.txt" file provides the bucket count on the first line in the text file, so that is where I am getting the bucket count from. 提供的“ data.txt”文件在文本文件的第一行中提供了存储桶计数,因此这是我从中获取存储桶计数的地方。

Anyhow, here is some code : 无论如何,这是一些代码:

Hashing .h : 散列.h:

#ifndef _Hashing_h
#define _Hashing_h

#include <stdio.h>

typedef struct bucket *bucketP;
typedef struct key *keyP;

/*
 * Creates the initial empty bucket array
 * from parameters 
 */
bucketP createBucketArray(FILE *);

#endif

Hashing.c : Hashing.c:

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

#include "Hashing.h"

struct bucket
{
    int collisionCount;
    keyP *firstKey;
};

struct key
{
    int thisKey;
    keyP *nextKey;
};

int main()
{
    FILE *fptr;
    int bucketCount, keyCount;
    fptr = fopen("data.txt", "r");
    fscanf(fptr, "%d %d", &bucketCount, &keyCount);
    bucketP thisArray = createBucketArray(fptr);
    fclose(fptr);

    return 0;
}

bucketP createBucketArray(FILE *fp)
{
    int thisBucketCount;
    int i = 0;
    fscanf(fp, "%d", &thisBucketCount);
    bucketP thisBucketArray[thisBucketCount] = (bucketP) malloc(sizeof(struct bucket));
    for (i = 0 ; i < thisBucketCount ; i++)
    {
        thisBucketArray[i]->collisionCount = 0;
        thisBucketArray[i]->firstKey = 0x00;
    }
    return thisBucketArray;
}

The error reads as follows : 错误内容如下:

error : variable-sized object may not be initialized
...thisBucketArray[thisBucketCount] = (bucketP) malloc(sizeof(struct bucket));
   ^

Since this was the first time I got the error message, I researched it and found that the above error message is usually thrown when you try and declare something too large for the stack. 由于这是我第一次收到错误消息,因此我对其进行了研究,发现当您尝试为堆栈声明太大的内容时,通常会抛出上述错误消息。 However, the value of 'thisBucketCount' is only 9,997. 但是,“ thisBucketCount”的值仅为9,997。 That creates an array of 9,997 pretty simple structs (only two parameters in struct bucket , one being an int and another being a pointer. 这样就创建了一个由9,997个非常简单的结构组成的数组( struct bucket只有两个参数,一个是int,另一个是指针。

Am I really creating an array that is too large for my stack? 我真的在创建一个对我的堆栈来说太大的数组吗? I feel like I would have to make a WAY BIGGER array for that. 我觉得我必须为此制作一个WAY BIGGER阵列。

Thanks in advance for any advice, thanks thanks thanks! 在此先感谢您的任何建议,谢谢谢谢谢谢!

Yes. 是。 The error is expected: VLAs (variable length arrays) can't be initialized. 预计将出现错误:VLA(可变长度数组)无法初始化。

But real problem seems to be that you want thisBucketCount number of buckets which is not what this code does: 但是真正的问题似乎是您想要thisBucketCount桶的thisBucketCount数量,而不是这段代码的作用:

bucketP thisBucketArray[thisBucketCount]=(bucketP)malloc(sizeof(struct bucket));

You probably want to allocate thisBucketCount number of bucket objects. 您可能要分配thisBucketCountbucket对象。 So you need do: 因此,您需要执行以下操作:

 bucketP thisBucketArray = malloc(thisBucketCount * sizeof(struct bucket));

 if (thisBucketArray == 0) 
 { 
 /*handle error */
 }

 for (i = 0 ; i < thisBucketCount ; i++)
 {
    thisBucketArray[i].collisionCount = 0;
    thisBucketArray[i].firstKey = 0x00;
 }

Notice that I have removed the cast. 请注意,我已删除演员表。 casting the result of malloc() is error-prone and dangerous. 强制转换malloc()的结果容易出错并且很危险。

You should also check if malloc() failed by checking if it returns NULL . 您还应该通过检查malloc()是否返回NULL来检查它是否失败。

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

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