简体   繁体   English

为什么我使用 calloc() function 为 5 个元素动态分配空间时却能给出 6 个元素?

[英]Why am I able to give 6 elements when I am dynamically allocating space for only 5 elements using calloc() function?

#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?它应该只为 5 个元素分配空间,对吗? I am unable to understand why is it allocating 6 integers.. Plzz helpp me..我不明白为什么要分配 6 个整数.. Plzz helpp me..

You go to a store.你go到店。 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.这种缺乏保证并不构成对入店行窃的认可,也不是 promise 永远不会抓到任何入店行窃者。

When you break the C language rules, the language does not always guarantee you will get caught.当你违反 C 语言规则时,语言并不总是保证你会被抓到。 This absence of a guarantee is not an endorsement to break the rules, nor it is a promise that you will never get caught.这种没有保证不是背书违规,也不是promise永远不会被抓到。

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 .对于这种缺乏保证,有一个技术术语(或者可能是 C 行话),称为未定义行为 Any C programmer must be familiar with it. C 的程序员一定对它不陌生。

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. for 循环的范围是从 0-5 意味着您正在访问 6(0,1,2,3,4,5) 个地址,但您只动态分配了 5 个。因此,将循环范围从 0-5 更改为 0 -4 就可以了。 Also, you must free the dynamically allocated memory at the end of the program.此外,您必须在程序结束时释放动态分配的 memory。

#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: 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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