Why am I getting this segmentation error?

+1 vote
asked Jun 9 by Bobberooni Tooni (220 points)
#include <stdio.h>
#include <stdlib.h>

int allocate(int*, int);
int enterValues(int*, int);

int main()
{
    int *array, size = -1;
    
    array = allocate(&array[0], 100);
    printf("Address of the memory space:\n%p\n\n", &*array);
    if(array == NULL)                     
    {
        printf("Error! memory not allocated.");
        exit(0);
    }
    printf("Enter the size of the array:\n");
    scanf("%d", &size);
    enterValues(&array, size);
    
    for (int i = 0; i < size; i++) {
        printf("%d", *(array + i));
    }
    
    
    return 0;
}

int allocate(int* array, int size)
{
    array = (int*) calloc(size, sizeof(int));
    
    return &array[0];
}

int enterValues(int* array, int size)
{
    printf("Enter values:\n");
    
    for (int i = 0; i < size; i++) {
        scanf("%d", &*(array + i));
        printf("%d", *(array + i)); <-- I think this is where the error is coming from
    }
    return 0;
}

I am new to these dynamic memory allocation things and pointers, can someone tell me what the issue is here, and why I get segmentation error when trying to print the last element of the array?

1 Answer

+2 votes
answered Jun 9 by xDELLx (2,430 points)
selected Jun 9 by Bobberooni Tooni
 
Best answer

few corrections:

line 4,30 : int* allocate(int* array, int size)

explanation: Since you are returning an array after you have allocated enough heap memory,you should return the base address of the array.

Also line 11 :array = allocate(&array[0], 100);

can be wriiten as : array = allocate(array, 100); // Since &(array[0]) is always same as array(i.e base address of  array is always same as adress of the 0th element of array)

Line 12 :enterValues(&array, size);

should be corrected to : enterValues(array, size);

Since type of "&array" is int** and its not what is supposed be passed to entervalues () function Enter values expects a int*

Corrected prog:

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

int* allocate(int*, int);
int enterValues(int*, int);

int main()
{
    int *array, size = -1;
    
    array = allocate(&array[0], 100);
    printf("Address of the memory space:\n%p\n\n", &*array);
    if(array == NULL)                     
    {
        printf("Error! memory not allocated.");
        exit(0);
    }
    printf("Enter the size of the array:\n");
    scanf("%d", &size);
    enterValues(array, size);
    
    for (int i = 0; i < size; i++) {
        printf("%d", *(array + i));
    }
    
    
    return 0;
}

int* allocate(int* array, int size)
{
    array = (int*) calloc(size, sizeof(int));
    
    return &array[0];
}

int enterValues(int* array, int size)
{
    printf("Enter values:\n");
    
    for (int i = 0; i < size; i++) {
        scanf("%d", &*(array + i));
        printf("%d", *(array + i) ); //<-- I think this is where the error is coming from
    }
    return 0;
}

commented Jun 10 by Bobberooni Tooni (220 points)
Thanks, this really helped!
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...