-1

I am trying to add the sum of an array. I am using malloc function to allocate memory at first and then realloc function to reallocate memory. but the realloc function is not allocating any memory.

HERE IS MY CODE

#include<stdio.h>        
#include<conio.h>        
#include<stdlib.h>        
void main(){    
    int s,*ptr, *p,sum=0,i,*q;    
    printf("\nEnter the size of array: ");    
    scanf("%d",&s);    
    ptr=(int *)malloc(s*sizeof(int));    
    p=ptr;    
    printf("\nMemory allocated: %u",ptr);    
    if(ptr==NULL){    
        printf("\nERROR! Insuffcient memory.");    
        exit(EXIT_FAILURE);    
    }    
    else{    
        printf("\nEnter the elements of array: ");    
        for(i=1;i<=s;i++){    
            scanf("%d",ptr);   
            sum+=*ptr;    
            ptr++;    
        }    
        printf("\nThe elements of arrays are: ");    
        for(i=1;i<=s;i++){    
            printf("%d\t",*p);    
            p++;    
        }    
        printf("\nThe Sum of array elements is: %d",sum);    
        printf("\nEnter the new Size of array: ");    
        scanf("%d",&s);    
        ptr=(int *)realloc(ptr , s * sizeof(int));    
        if(ptr==NULL){    
            printf("\nERROR!! Insufficient Memory!!");    
            exit(EXIT_FAILURE);    
        }    
        else{   
 printf("\nReallocated memory: %u",ptr);    
        q=ptr;   
            printf("\nEnter the elements of array: ");    
            for(i=1;i<=s;i++){    
                scanf("%d",ptr);    
                sum+=*ptr;    
                ptr++;    
            }    
            printf("\nThe elements of arrays are: ");    
            for(i=1;i<=s;i++){    
                printf("%d\t",*q);    
                q++;    
            }    
            printf("\nThe Sum of array elements is: %d",sum);    
        }    
    }    
}    
Bimal Timilsina
  • 81
  • 2
  • 12
  • Probably your scanf fails and co sequently realloc is getting same or random value. The check it just add printing of the new size – 0___________ Jan 06 '19 at 09:51
  • 1
    @xing is right, you should preserve the starting address and use that again in `realloc`, also reinitialize `sum` to zero after `realloc`, or your new `sum` is added to first array one – hessam hedieh Jan 06 '19 at 10:07
  • 2
    Arrays in C are indexed from zero not one. Loops therefore are `for (int i = 0; i < size; i++)`. – Jonathan Leffler Jan 06 '19 at 10:14
  • 1
    @JonathanLeffler, I see nothing wrong in his `for` loops, he is not accessing index of array by `i`, its just a counter – hessam hedieh Jan 06 '19 at 10:31
  • It is still better to learn idiomatic C loops now, even if they are not currently a problem. And because the OP is not indexing, it looks like both `ptr` and `p` have been incremented and the reallocation fails because the pointer passed to `realloc()` is not one returned by `malloc()` or a previous `realloc()`. I cold still be misreading the code; it wouldn’t surprise me. But a quick glance suggests that. – Jonathan Leffler Jan 06 '19 at 10:37
  • [Don't cast the result of malloc in C](https://stackoverflow.com/q/605845/995714) – phuclv Jan 06 '19 at 10:39

1 Answers1

2

It's because you changed the value of ptr so that it no longer points to the original malloced memory. It happens here:

    for(i=1;i<=s;i++){    
        scanf("%d",ptr);   
        sum+=*ptr;    
        ptr++;     // ptr is changed
    }    

Instead of changing ptr you should do:

    for(i=0;i<s;i++){    
        scanf("%d",&ptr[i]);   // or scanf("%d", ptr + i);
        sum+=ptr[i];    
    }    

BTW: When using scanf always check that it scans the expected number of elements. Like:

    if (scanf("%d",&ptr[i]) != 1)
    {
        // add error handling here
    }
Support Ukraine
  • 42,271
  • 4
  • 38
  • 63