指向指向字符串的动态指针数组的动态指针数组的指针

时间:2015-01-14 10:35:34

标签: c pointers dynamic-allocation

我在第一个动态指针数组

中初始化值时遇到问题
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char*** GetIndexes()
{
int n = 0;
char ***index;
printf("please insert the number of words you want to add to dictionary\n");
scanf("%d", &n);
index = (char***)calloc(n, sizeof(char));
if (index == NULL)
{
    printf("allocation Failed");
    return;
}
return index;
}
char** GetDefinitions()
{
int n = 0;
char **definition;
printf("please insert the number of defintions you want to add to the word\n");
scanf("%d", &n);
definition = (char**)calloc(n+1, sizeof(char));
if (definition == NULL)
{
    printf("allocation failed");
    return;
}
return definition;
}
int main()
{
char *** dptr = GetIndexes();
if (dptr == NULL)
{
    printf("memory Allocation failed");
}
int indexcount = sizeof(dptr) / sizeof(char),i;
for (i = 0; i < indexcount; i++)
{
    printf("word number %d\n", i + 1);
    *dptr[i] = GetDefinitions();
}
printf("%p",dptr);

}

我尝试在VS2013中运行调试器,然后输入我希望它与此消息一起崩溃的定义数量:

ConsoleApplication1.exe中0x01103FB0处的未处理异常:0xC0000005:访问冲突写入位置0x00000000。

我错过了分配的东西,但我无法弄清楚我错过了什么,

提前致谢

2 个答案:

答案 0 :(得分:4)

您的计划非常糟糕

  1. 您分配了n char ***个但仅n char的请求空间,并为char **执行此操作,以防止此类情况你可以用这种方式使用sizeof运算符

    char ***index;
    index = calloc(n, sizeof(*index));
    

    char **definition;
    definition = calloc(n, sizeof(*definition));
    

    正如您所见,施放calloc会使其变得更难,而且没有必要。

  2. 您的return语句不会返回GetIndexes()以及GetDefinitions中的任何内容。

    如果你想处理调用函数中的失败,他们应该返回NULL

    return NULL;
    
  3. 您错误地使用sizeof运算符来确定

    中分配的char ***指针的数量
    int indexcount = sizeof(dptr) / sizeof(char)
    

    这将是48,具体取决于架构,即指针的大小始终除以1 sizeof(char) == 1

    您无法计算该值,只需跟踪它即可。大小

  4. 您取消引用三次指针并尝试为其指定一个双指针

    *dptr[i] = GetDefinitions();
    

    这里运算符优先级也是一个问题,但无论如何,这都是错误的,可能就是你的意思

    dptr[i] = GetDefinitions();
    
  5. 这不会让您的程序崩溃,但在退出程序之前free所有malloc指针肯定很重要。

  6. 以下是您的代码工作的建议,忽略了它的目的,因为它不清楚您要执行的操作

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    char ***GetIndexes(unsigned int *count)
    {
        char ***index;
    
        printf("please insert the number of words you want to add to dictionary > ");
        scanf("%u", count);
    
        index = calloc(*count, sizeof(*index));
        if (index == NULL)
        {
            printf("allocation Failed");
            return NULL;
        }
    
        return index;
    }
    
    char **GetDefinitions(unsigned int *count)
    {
        char **definition;
    
        printf("please insert the number of defintions you want to add to the word > ");
        scanf("%u", count);
    
        definition = calloc(*count + 1, sizeof(*definition));
        if (definition == NULL)
        {
            printf("allocation failed");
            return NULL;
        }
        return definition;
    }
    
    int main()
    {
        unsigned int indexCount, i;
        char      ***dptr = GetIndexes(&indexCount);
    
        if (dptr == NULL)
        {
            printf("memory Allocation failed");
        }
    
        for (i = 0; i < indexCount; i++)
        {
            unsigned int definitionsCount;
    
            printf("Word number %u\n", i + 1);
            dptr[i] = GetDefinitions(&definitionsCount);
            if (dptr[i] != NULL)
            {
                /* use dptr[i] here or maybe somewhere else, but when you finish */
                free(dptr[i]);
            }
        }
        printf("%p", dptr);
        /* now if you are done using dptr */
        free(dptr);
    
        return 0;
    }
    

答案 1 :(得分:0)

正如评论中已经提到的,这是一个非常糟糕的主意,只需使用双指针就可以了。但是如果你想使用指针分配内存

,应该完成下面的修复
index = calloc(n, sizeof(char));

应该是

index = calloc(n, sizeof(char **));

definition = calloc(n+1, sizeof(char));

应该是

definition = calloc(n+1, sizeof(char *));