C语言中指针向量的内存分配
我试图为指针向量重新分配内存,最初该向量是:C语言中指针向量的内存分配,c,pointers,memory,vector,C,Pointers,Memory,Vector,我试图为指针向量重新分配内存,最初该向量是: Album* albuns 其中相册是一个结构 我创建了一个函数,将albuns类型的地址和albuns的总数作为参数传递: void AddAlbum (int* n_albuns, Album** albuns); 我想为albuns重新分配内存,这样它就可以接收另一个指向唱片集的指针,所以我做到了: int aux = (*n_albuns) + 1; albuns = (Album**) realloc (albuns,(sizeof(A
Album* albuns
其中相册是一个结构
我创建了一个函数,将albuns类型的地址和albuns的总数作为参数传递:
void AddAlbum (int* n_albuns, Album** albuns);
我想为albuns重新分配内存,这样它就可以接收另一个指向唱片集的指针,所以我做到了:
int aux = (*n_albuns) + 1;
albuns = (Album**) realloc (albuns,(sizeof(Album*) * aux));
albuns[*n_albuns] = (Album*) malloc (sizeof(Album));
(*n_albuns)++;
但是函数在这一行中返回一个SegFault:
albuns[*n_albuns] = (Album*) malloc (sizeof(Album));
有什么想法吗?我对内存分配比较陌生问题代码有几个问题。见受阻评论
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Question code should include a model of the structure. */
typedef struct Album_s
{
int a;
char b;
int *c;
char *d;
struct Album_s *e;
} Album;
/* Question indicates: void AddAlbum (int* n_albuns, Album** albuns);
** However, in order to change the size of the array, the address of the
** array must be supplied. Hence, the change from **albuns to ***albuns.
*/
int AddAlbum(int *n_albuns, Album ***albums)
{
int rCode = EXIT_SUCCESS;
void *newMem = NULL;
/* Attempt to increase the size of the array by one (pointer) element.
** The question code casts (Album**) the value returned by realloc(),
** which is not necessary. It is also important to verify that the call
** to realloc() actually succeeded. The realloc code below is fairly
** customary in order to check for this.
*/
newMem = realloc(*albums, sizeof(Album *) * (*n_albuns + 1));
if(!newMem)
{
rCode=errno;
goto CLEANUP;
}
*albums = newMem;
/* Again, the question code casts (Album*) the value returned by malloc(),
** which is not necessary. Being that the albums parameter is a pointer
** to a pointer to a pointer, the following assignment is correct. Again,
** it is important to verify that the call to malloc() actually succeeded.
*/
(*albums)[*n_albuns] = malloc(sizeof(Album));
if(!(*albums)[*n_albuns])
{
rCode=errno;
goto CLEANUP;
}
(*n_albuns)++;
CLEANUP:
return(rCode);
}
/* Example of how AddAlbum() might be called. */
int main(void)
{
int rCode = EXIT_SUCCESS;
Album **albunArray = NULL;
int albunCnt = 0;
int desiredCnt = 10;
while(albunCnt < desiredCnt)
{
rCode=AddAlbum(&albunCnt, &albunArray);
if(rCode)
{
fprintf(stderr, "AddAlbum() failed. errno: %d \"%s\"\n", rCode, strerror(rCode));
goto CLEANUP;
}
printf("Array element %d of %d added.\n", albunCnt, desiredCnt);
}
CLEANUP:
return(rCode);
}
#包括
#包括
#包括
#包括
/*问题代码应包括结构的模型*/
typedef结构相册
{
INTA;
字符b;
int*c;
char*d;
结构相册;
}相册;
/*问题表明:void AddAlbum(int*n_albuns,albuns**albuns);
**但是,为了更改数组的大小
**必须提供数组。因此,从**阿邦变为***阿邦。
*/
int AddAlbum(int*n_albuns,Album***albums)
{
int rCode=退出成功;
void*newMem=NULL;
/*尝试将数组的大小增加一个(指针)元素。
**问题代码强制转换(Album**)realloc()返回的值,
**这是不必要的。验证呼叫
**到realloc()的操作实际上成功了。下面的realloc代码相当简单
**为了检查这一点。
*/
newMem=realloc(*相册,大小(相册*)*(*n_albuns+1));
如果(!newMem)
{
rCode=errno;
去清理;
}
*相册=新成员;
/*同样,问题代码强制转换(Album*)malloc()返回的值,
**这是不必要的。因为albums参数是指针
**对于指向指针的指针,以下赋值是正确的。同样,
**验证对malloc()的调用是否确实成功非常重要。
*/
(*唱片)[*n_albuns]=malloc(sizeof(唱片));
如果(!(*相册)[*n_albuns])
{
rCode=errno;
去清理;
}
(*n_albuns)+;
清理:
返回(rCode);
}
/*如何调用AddAlbum()的示例*/
内部主(空)
{
int rCode=退出成功;
相册**albunArray=NULL;
int-albunCnt=0;
int desiredCnt=10;
而(albunCnt
问题代码有几个问题。见受阻评论
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Question code should include a model of the structure. */
typedef struct Album_s
{
int a;
char b;
int *c;
char *d;
struct Album_s *e;
} Album;
/* Question indicates: void AddAlbum (int* n_albuns, Album** albuns);
** However, in order to change the size of the array, the address of the
** array must be supplied. Hence, the change from **albuns to ***albuns.
*/
int AddAlbum(int *n_albuns, Album ***albums)
{
int rCode = EXIT_SUCCESS;
void *newMem = NULL;
/* Attempt to increase the size of the array by one (pointer) element.
** The question code casts (Album**) the value returned by realloc(),
** which is not necessary. It is also important to verify that the call
** to realloc() actually succeeded. The realloc code below is fairly
** customary in order to check for this.
*/
newMem = realloc(*albums, sizeof(Album *) * (*n_albuns + 1));
if(!newMem)
{
rCode=errno;
goto CLEANUP;
}
*albums = newMem;
/* Again, the question code casts (Album*) the value returned by malloc(),
** which is not necessary. Being that the albums parameter is a pointer
** to a pointer to a pointer, the following assignment is correct. Again,
** it is important to verify that the call to malloc() actually succeeded.
*/
(*albums)[*n_albuns] = malloc(sizeof(Album));
if(!(*albums)[*n_albuns])
{
rCode=errno;
goto CLEANUP;
}
(*n_albuns)++;
CLEANUP:
return(rCode);
}
/* Example of how AddAlbum() might be called. */
int main(void)
{
int rCode = EXIT_SUCCESS;
Album **albunArray = NULL;
int albunCnt = 0;
int desiredCnt = 10;
while(albunCnt < desiredCnt)
{
rCode=AddAlbum(&albunCnt, &albunArray);
if(rCode)
{
fprintf(stderr, "AddAlbum() failed. errno: %d \"%s\"\n", rCode, strerror(rCode));
goto CLEANUP;
}
printf("Array element %d of %d added.\n", albunCnt, desiredCnt);
}
CLEANUP:
return(rCode);
}
#包括
#包括
#包括
#包括
/*问题代码应包括结构的模型*/
typedef结构相册
{
INTA;
字符b;
int*c;
char*d;
结构相册;
}相册;
/*问题表明:void AddAlbum(int*n_albuns,albuns**albuns);
**但是,为了更改数组的大小
**必须提供数组。因此,从**阿邦变为***阿邦。
*/
int AddAlbum(int*n_albuns,Album***albums)
{
int rCode=退出成功;
void*newMem=NULL;
/*尝试将数组的大小增加一个(指针)元素。
**问题代码强制转换(Album**)realloc()返回的值,
**这是不必要的。验证呼叫
**到realloc()的操作实际上成功了。下面的realloc代码相当简单
**为了检查这一点。
*/
newMem=realloc(*相册,大小(相册*)*(*n_albuns+1));
如果(!newMem)
{
rCode=errno;
去清理;
}
*相册=新成员;
/*同样,问题代码强制转换(Album*)malloc()返回的值,
**这是不必要的。因为albums参数是指针
**对于指向指针的指针,以下赋值是正确的。同样,
**验证对malloc()的调用是否确实成功非常重要。
*/
(*唱片)[*n_albuns]=malloc(sizeof(唱片));
如果(!(*相册)[*n_albuns])
{
rCode=errno;
去清理;
}
(*n_albuns)+;
清理:
返回(rCode);
}
/*如何调用AddAlbum()的示例*/
内部主(空)
{
int rCode=退出成功;
相册**albunArray=NULL;
int-albunCnt=0;
int desiredCnt=10;
而(albunCnt
album[*n_albuns]超出了album的大小,因此您会遇到一个错误。为什么?只有您知道,我们需要查看完整的AddAlbum以及'b'变量是什么。此外,此行中的b
是什么:album=(album**)realloc(album,(sizeof(album*)*b))代码>为什么要在这里重新分配?为什么不将这种重新分配包装在函数中,以便您可以干净地完成它?请记住,如果这是一个多级结构,则需要将重新分配的指针推回到原始数组中。album[*n_albuns]超出了album的大小,因此您会得到一个segfault。为什么?只有您知道,我们需要查看完整的AddAlbum以及'b'变量是什么。此外,此行中的b
是什么:album=(album**)realloc(album,(sizeof(album*)*b))代码>为什么要在这里重新分配?为什么不将这种重新分配包装在函数中,以便您可以干净地完成它?记住,你需要推动rea