Home > Mobile >  Why am I able to give 6 elements when I am dynamically allocating space for only 5 elements using ca
Why am I able to give 6 elements when I am dynamically allocating space for only 5 elements using ca

Time:12-11

#include <stdio.h>
#include <stdlib.h>
int main(){
    int *ptr;
    ptr=(int*)calloc(5,sizeof(int));
    for(int i=0;i<6;i  ){
        printf("Enter the %d value:",i 1);
        scanf("%d",&ptr[i]);
    }
    printf("\n Elements in allocated memory\n\n");
    for(int i=0;i<6;i  ){
        printf("The %d element is: %d\n",i 1,ptr[i]);

    }

    return 0;
}
OUTPUT:
Enter the 1 value:1

Enter the 2 value:2

Enter the 3 value:3

Enter the 4 value:4

Enter the 5 value:5

Enter the 6 value:6


 Elements in allocated memory

The 1 element is: 1

The 2 element is: 2

The 3 element is: 3

The 4 element is: 4

The 5 element is: 5

The 6 element is: 6

It should allocate space for only 5 elements right? I am unable to understand why is it allocating 6 integers.. Plzz helpp me..

CodePudding user response:

You go to a store. You pick up six candy bars. You tell the cashier you have five candy bars, pay for five, and leave.

Does this story imply people are allowed to take six candy bars and only pay for five? No it doesn't.

There is no guarantee every shoplifter will be caught. This absence of a guarantee does not constitute an endorsement to shoplift, nor it is a promise that no shoplifter will ever be caught.

When you break the C language rules, the language does not always guarantee you will get caught. This absence of a guarantee is not an endorsement to break the rules, nor it is a promise that you will never get caught.

Your sixth array element is the shoplifted candy bar. You didn't get caught this time. Doesn't mean you won't get caught next time, or some other time when you expect it the least.

Live demo.

There is a technical term (or perhaps C-jargon) for this absence of guarantee, it is called undefined behavior. Any C programmer must be familiar with it.

CodePudding user response:

The range of for loop was from 0-5 means you are accessing 6(0,1,2,3,4,5) addresses but you have dynamically allocated just 5. So, changing the range of loop from 0-5 to 0-4 will do the trick. Also, you must free the dynamically allocated memory at the end of the program.

#include <stdio.h>
#include <stdlib.h>
int main(){
    int *ptr;
    ptr=(int*)calloc(4,sizeof(int));
    for(int i=0;i<5;i  ){
        printf("Enter the %d value:",i 1);
        scanf("%d",&ptr[i]);
    }
    printf("\n Elements in allocated memory\n\n");
    for(int i=0;i<5;i  ){
        printf("The %d element is: %d\n",i 1,ptr[i]);

    }
    free(ptr);

    return 0;
}

OUTPUT:

Enter the 1 value:1
Enter the 2 value:2
Enter the 3 value:3
Enter the 4 value:4
Enter the 5 value:5

Elements in allocated memory

The 1 element is: 1
The 2 element is: 2
The 3 element is: 3
The 4 element is: 4
The 5 element is: 5

Process returned 0 (0x0)   execution time : 5.662 s
  •  Tags:  
  • c
  • Related