简体   繁体   中英

Memory allocation for vectors of pointers in C

I'm trying to reallocate memory for an pointers vector, originally the vector is:

Album* albuns

Where Album is a struct;

I created a function passing the adress of type albuns and the total number of albuns as arguments:

void AddAlbum (int* n_albuns, Album** albuns);

I wanted to reallocate memory for albuns so it could receive another pointer to album, so i did:

int aux = (*n_albuns) + 1;
albuns = (Album**) realloc (albuns,(sizeof(Album*) * aux));
albuns[*n_albuns] = (Album*) malloc (sizeof(Album));
(*n_albuns)++; 

but the function returns me a SegFault in this line:

albuns[*n_albuns] = (Album*) malloc (sizeof(Album));

Any ideias? I'm relatively new to memory allocation

There are several issues with the question code. See impeded comments.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM