双指针realloc错误

双指针realloc错误,c,pointers,memory,double,allocation,C,Pointers,Memory,Double,Allocation,朋友你好:)我正在练习C编程。在这个程序中,我有一个任务,使字符串数组。我不知道这里出了什么问题…可能是关于realloc的,我得到的错误是_crtisvalidheappointer #define _CRT_SECURE_NO_WARNINGS #define MAX 100 #include <stdio.h> #include <stdlib.h> #include <string.h> void readString(char **s) { int

朋友你好:)我正在练习C编程。在这个程序中,我有一个任务,使字符串数组。我不知道这里出了什么问题…可能是关于realloc的,我得到的错误是_crtisvalidheappointer

#define _CRT_SECURE_NO_WARNINGS
#define MAX 100
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void readString(char **s)
{
int i = 0;
char c;
printf("\nInput string:     ");
while ((c = getchar()) != '\n')
{
    i++;
    *s = realloc(*s, i*sizeof(char*));
    if (*s == NULL) { printf("Memory allocation failed!"); exit(1); }
    (*s)[i - 1] = c;
}
*s = realloc(*s, (i + 1)*sizeof(char));
if (*s == NULL) { printf("Memory allocation failed!"); exit(1); }
(*s)[i] = '\0';
}


char **load_words()
{
int cnt=0,wordcnt=0,i=0;
char **words = NULL, *input = NULL; 
readString(&input);
while (input[cnt] != '\0' && cnt < strlen(input))
{
    words = realloc(words, ++wordcnt);//errors in second repeat of the loop
    words[wordcnt] = malloc(MAX);
    i = 0;
    while (input[cnt] != ' ')
    {
        words[wordcnt][i++] = input[cnt++];
    }
    words[wordcnt][i] = '\0';
    realloc(words[wordcnt], (i + 1)*sizeof(char));
}
realloc(words, wordcnt);
free(input);
return words;
}

void main()
{
 int i;
 char **words = NULL;
 words = load_words();
 scanf("%d", &i);
}
\define\u CRT\u SECURE\u NO\u警告
#定义最大值100
#包括
#包括
#包括
无效读取字符串(字符**s)
{
int i=0;
字符c;
printf(“\n输入字符串:”);
而((c=getchar())!='\n')
{
i++;
*s=realloc(*s,i*sizeof(char*);
如果(*s==NULL){printf(“内存分配失败!”);退出(1);}
(*s)[i-1]=c;
}
*s=realloc(*s,(i+1)*sizeof(char));
如果(*s==NULL){printf(“内存分配失败!”);退出(1);}
(*s)[i]='\0';
}
字符**加载单词()
{
int cnt=0,wordcnt=0,i=0;
字符**字=NULL,*输入=NULL;
readString(&input);
while(输入[cnt]!='\0'和&cnt

谁能帮帮我,告诉我我做错了什么?此函数应返回字符串数组,但数组应为双指针(字符串矩阵)

您使用的是
realloc
,但未将其返回值保存在任何位置。这意味着您的指针仍然指向已释放的内存,并且新分配的内存泄漏

查看工作函数,您将了解如何正确使用它。

您需要更改

words = realloc(words, ++wordcnt);

否则,您没有分配足够的内存



这也是不正确的,您应该访问
words[wordcnt-1]

重新分配双指针时要意识到的一点是,
realloc
的类型大小始终是(指针)的大小。在任何给定的系统上,无论数据类型如何,它都是相同的。您可以按如下方式重新分配双指针:

/** realloc array of pointers ('memptr') to twice current
 *  number of pointer ('*nptrs'). Note: 'nptrs' is a pointer
 *  to the current number so that its updated value is preserved.
 *  no pointer size is required as it is known (simply the size
 *  of a pointer)
 */
void *xrealloc_dp (void *ptr, size_t *n)
{
    void **p = ptr;
    void *tmp = realloc (p, 2 * *n * sizeof tmp);
    if (!tmp) {
        fprintf (stderr, "xrealloc_dp() error: virtual memory exhausted.\n");
        exit (EXIT_FAILURE);    /* or break; to use existing data */
    }
    p = tmp;
    memset (p + *n, 0, *n * sizeof tmp); /* set new pointers NULL */
    *n *= 2;
    return p;
}
注意:调用
memset
是可选的,但如果您已将所有未分配的指针初始化为
NULL
(例如,当使用
NULL
作为哨兵时),则该调用非常有用

注2:您可以根据代码的需要自由传递参数,设置要增加的指针(要添加的元素)的确切数量,或更改当前分配的乘数。

void readString(char**s)正常工作
*s=realloc(*s,i*sizeof(char*)
*s=realloc(*s,(i+1)*sizeof(char))
char*
vs
char
,可疑。
words[wordcnt] = malloc(MAX);
/** realloc array of pointers ('memptr') to twice current
 *  number of pointer ('*nptrs'). Note: 'nptrs' is a pointer
 *  to the current number so that its updated value is preserved.
 *  no pointer size is required as it is known (simply the size
 *  of a pointer)
 */
void *xrealloc_dp (void *ptr, size_t *n)
{
    void **p = ptr;
    void *tmp = realloc (p, 2 * *n * sizeof tmp);
    if (!tmp) {
        fprintf (stderr, "xrealloc_dp() error: virtual memory exhausted.\n");
        exit (EXIT_FAILURE);    /* or break; to use existing data */
    }
    p = tmp;
    memset (p + *n, 0, *n * sizeof tmp); /* set new pointers NULL */
    *n *= 2;
    return p;
}