gpt4 book ai didi

c - 在 C 中使用指针数组方法为 3D 数组动态分配内存

转载 作者:太空宇宙 更新时间:2023-11-04 08:11:29 26 4
gpt4 key购买 nike

我遇到了段错误无法纠正......................

如果使用函数(顶部多行注释)我发现分割错误使用目前的代码,我无法猜测段错误。

如果使用固定大小的字符串,如 malloc,那么它工作得很好。

//Array of pointers method
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int count=0;
char *** input(char ***);
void print(char ***);
/*
void *allocate(char *p,int j)
{
int i=0;
do
{
p=realloc(p,i+1);
// printf("p[count][j]= %u\n",p);
p[i]=getchar();
}while(p[i++]!=10);
p[i-1]='\0';
return p;
}*/
void save(char ***p)
{
int j,i;
FILE *fp;
fp=fopen("Secret.c","w");
for(i=0;i<count;i++)
for(j=0;j<3;j++)
{
fprintf(fp,"%s\n",p[i][j]);

}
fclose(fp);

}
void main()
{
char ***a=0;
char ch;
// printf("a = %u\n ",a);
while(1)
{
if(count>0)
{
printf("a = %lu\n",a);
printf("*a = %lu\n",sizeof(*a));
printf("**a = %lu\n",sizeof(**a));
printf("***a = %lu\n",***a);
}
printf("\n\n\t\t\tMenu\n\n\n");
printf("i . input\ns . save\np . print\nq . quit");
printf("\n\nEnter your choice : ");
scanf(" %c",&ch);
getchar();
switch(ch)
{
case 'i': a=input(a);break;
case 's': save(a);
case 'p': print(a);break;
case 'q': return;
default : printf("\n\n\t\t!!! Invalid choice \n\n");
}
}
}
char *** input(char ***p)
{
int i=0,j;

// printf("p = %u\n ",p);
p=realloc(p,sizeof(p)*(count+1));
// printf("p = %u\n ",p);
// printf("sizeof(p) = %u\n",sizeof(p));
// printf("*p = %u\n ",*p);
for(j=0;j<3;j++)
{
p[count]=realloc(p[count],sizeof(p)*(j+1));
// printf("*p = %u\n ",*p);
// printf("**p = %u\n ",**p);
i=0;
if(j==0)
printf("Enter the %d name : ",count+1);
else if (j==1)
printf("Enter the %d mobile no : ",count+1);
else
printf("Enter the %d E-mail : ",count+1);
// p[count][j]=malloc(20);
// gets(p[count][j]);

// p[count][j]=allocate(p[count][j],j);
do
{
p[count][j]=realloc(p[count][j],i+1);
p[count][j][i]=getchar();
}while(p[count][j][i++]!=10);
p[count][j][i-1]='\0';
// printf("***p = %u %s\n ",***p,***p);
}
count++;
return p;

}
void print(char ***p)
{
int j,i;
for(i=0;i<count;i++,printf("\n"))
for( j=0;j<3;j++)
printf("p[%d][%d] : %s\t",i,j,p[i][j]);

}

最佳答案

一些一般准则:

您想(重新)分配 p 指向 类型的 N 个实例,而不是 p 类型的 N 个实例,所以 sizeof 的操作数应该是 *p:

p = malloc( sizeof *p * N ); // operand of sizeof is *p, not p
^^

如果p是一个指针数组,使用

p[i] = malloc( sizeof *p[i] * N );

此外,请记住,如果内存无法扩展,realloc 调用将返回 NULL,因此您不希望将结果直接分配回 p,否则你可能会丢失指向你已经分配的内存的指针,从而导致内存泄漏。将结果分配回与 p 相同类型的临时对象:

T *tmp = realloc( p, sizeof *p * M );
if ( tmp )
{
p = tmp;
}

realloc 是一个潜在的昂贵调用,许多小的 realloc 会导致内部内存碎片。分配更大的 block 比一次扩展一个元素要好。要读取单个输入,您可以这样做:

char *getNextLine( FILE *inputStream )
{
char staticBuffer[SOME_BUFFER_SIZE] = {0};
char *dynamicBuffer = calloc( SOME_BUFFER_SIZE, sizeof *dynamicBuffer );
size_t dynamicBufferSize = SOME_BUFFER_SIZE;
bool done = false;

/**
* If the initial allocation fails, we have real problems.
*/
assert( dynamicBuffer != NULL );

while ( !done && fgets( staticBuffer, sizeof staticBuffer, stdin ) )
{
/**
* Check for the newline; if it's there, remove it and set the
* done flag to exit the loop.
*/
char *newline = strchr( staticBuffer, '\n' );
if ( (done = (newline != NULL)) )
{
*newline = 0;
}

/**
* Do we need to extend our dynamic buffer?
*/
if ( strlen( dynamicBuffer ) + strlen( staticBuffer ) > dynamicBufferSize )
{
/**
* Yes. Double the size of the dynamic buffer.
*/
char *tmp = realloc( dynamicBuffer, sizeof *dynamicBuffer * ( dynamicBufferSize * 2 ) );
if ( tmp )
{
dynamicBuffer = tmp;
dynamicBufferSize *= 2;
}
else
{
// could not extend buffer, print an error and return what we have
fprintf( stderr, "ERR: Could not extend dynamic input buffer\n" );
return dynamicBuffer;
}
}

strcat( dynamicBuffer, staticBuffer );
}

return dynamicBuffer;
}

关于c - 在 C 中使用指针数组方法为 3D 数组动态分配内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39040827/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com