简体   繁体   中英

Array of pointers using malloc : The code runs into a segmentation fault. Help! Thanks

it seems I'm a bit confused here. As with the use of a pointer to an array of pointers. if I could fetch more info regarding the error, I would have. All I received is a code for a segmentation fault. Thanks :)

a program to find the longest input string.

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>

int main()
{
 char **array;
 int i, n, *L, maxlen;


 printf("Enter the number of strings that you wish to store : ");
 scanf("%d", &n);

 array=malloc(n*sizeof(char*)); //acquire storage for an array of pointes of size n//
 L=malloc(sizeof(int)*n);// for the length array//
 for(i=0;i<n;i++)
 {
    printf("Enter string %d : ", i+1); //sometimes, prints upto this//
    gets(array[i]); //sometimes skips the first string input and jumps to the second and stops at the third//
    L[i]=strlength(array[i]);
 }
 maxlen=0;
 for(i=0;i<n;i++)
 {
    if(L[i]>maxlen)
        maxlen=L[i];
 }

  printf("The string(s) with the maximum length with length %d are : ", maxlen);
 for(i=0;i<n;i++)
 {
     if(L[i]==maxlen)
    {
        printf("\n%s.", array[i]);
    }
 }
  return 0;
 }

int strlength(char *array)
{
int j=0;
while(array[j]!='\0')
  {

    j++;
  }
return j;
}

you allocated memory for pointer to pointer, but forgot to allocate for individual pointers( ie the strings).

it can be modified as,

array=malloc(n*sizeof(char*));

to allocate memory for storing all the pointers for the strings.

and

for( i = 0; i<n; i++)
{
     array[i] = malloc(<max length of string>);
}

then only you will have memory space allocated to store the strings

Your memory allocation for:

char **array;
array=malloc(n*sizeof(char*)); //acquire storage for an array of pointes of size n//

only allocates memory for a set of pointers, but none for the space that is needed for the strings the pointers will point to. A function can be defined to encapsulate these steps:

char **array = Create2DStr(numberOfStrings, maxLengthOfString);

Where Create2DStr can be implemented as follows:

char ** Create2DStr(ssize_t numStrings, ssize_t maxStrLen)
{
    int i;
    char **a = {0};
    a = calloc(numStrings, sizeof(char *));// Creates a set of pointers.
    for(i=0;i<numStrings; i++)
    {
      a[i] = calloc(maxStrLen + 1, 1);//creates space for strings pointed to.
    }
    return a;
}

Caution:
- If creating memory in a loop using the same pointer, you should be familiar with realloc()
- maxStrLen in example should contain 1 byte more than the length of the longest string you need to accommodate.
- Do not forget to free everything you created in the reverse order they were created, ie free space for strings first ( maxStrLen calls to free() ), then free the pointers to each string last (1 call to free() )). See following example:

free2DStr(&array,numStrings);

The implementation of free2DStr can be done many ways, see below for an example.

void free2DStr(char *** a, ssize_t numStrings)
{
    int i;
    if(!(*a)) return;
    for(i=0;i<numStrings; i++)
    {
        if((*a)[i]) 
        {
            free((*a)[i]);
            (*a)[i] = NULL;
        }
    }
    free((*a));
    (*a) = NULL;
}

Note that in this implementation the address of the object is used in the first argument to provide access to free the pointers residing at that location.

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