【问题标题】:pointer to dynamic array of pointers to dynamic array of pointer that points to strings指向动态指针数组的指针 指向指向字符串的动态指针数组
【发布时间】:2015-01-14 10:35:33
【问题描述】:

我在初始化第一个动态指针数组中的值时遇到问题

#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。

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

提前致谢

【问题讨论】:

  • 不管什么原因你有char ***这很可能是个坏主意,而且空间不足,应该是calloc(n, sizeof(char **));甚至calloc(n, sizeof(char *)); 或calloc(n, sizeof(void *)); 即n 指针,但不是calloc(sizeof(char));。另外,不要投射calloc/malloc,它可能会隐藏程序中的错误。
  • 你认为这个*dptr[i] = GetDefinitions(); 会做什么?
  • 你认为int indexcount = sizeof(dptr) / sizeof(char)的结果是什么?

标签: c pointers dynamic-allocation


【解决方案1】:

你的程序很糟糕

  1. 你分配nchar ***s,但只为nchars申请空间,也为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)
    

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

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

  4. 您取消引用三重指针两次并尝试为其分配双指针

    *dptr[i] = GetDefinitions();
    

    这里的运算符优先级也是一个问题,但不管怎样,这是错误的,可能是你的意思

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

这是您的代码工作的建议,忽略它的目的,因为不清楚您要做什么

#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;
}

【讨论】:

  • 如果分配失败,return 只会丢弃程序,有错吗?我真的不认为我可以在不是 main() 的函数中执行 exit(1)
  • "如果分配失败,return 只会丢弃程序,有错吗?" 没关系,但不是很好。您可以随时致电exit(),但在信号处理程序中。 @Puloko
  • 1+ 以获得完整答案。仍然所有ints 应为size_ts。
  • @alk 我没有注意到,因为还有很多其他问题,但这是一个很好的建议,我会解决它。
  • "... 是 size_ts." 或至少是 unsigned。
【解决方案2】:

正如评论中已经提到的,这是一个非常糟糕的主意,在这里只使用双指针是好的。但是如果您想使用指针分配内存,则应该完成以下修复

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

应该是

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

和

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

应该是

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

【讨论】:

  • 三度指针的要点是制作字典dictionaryPTR>>Dynamic Array of Pointers To another dynamic Array of pointers>>动态指针数组指向动态的单词或字符串数​​组的地址分配
  • @Puloko 你不认为双指针就可以达到你的目的吗?
  • 说实话我不知道
  • @Puloko 摆脱三重指针并使用双指针重新设计代码,我认为这是一个更好的主意,而且比这更容易。我认为这里不需要三指针
  • 我将不得不对单词进行 qsort,所以我认为三重指针仍然是必要的,
猜你喜欢
  • 2017-02-07
  • 2012-03-09
  • 2021-02-07
  • 1970-01-01
  • 2016-07-30
  • 1970-01-01
相关资源
最近更新 更多