在c语言中为全局变量和局部变量分配的内存是不同的。

时间:2022-11-10 16:50:07

在c语言中为全局变量和局部变量分配的内存是不同的。在c语言中为全局变量和局部变量分配的内存是不同的。(GLOBAL DECLARATION OF CHAR ARRAY)

(全球CHAR数组声明)

#include<stdio.h>
char name[10];  /* though allocated memory is 10 bytes still it accepts more then 10 chars*/
void main()
{
     printf("\n ENter name :\t");
     scanf("%s",name);
}

Second case:(LOCAL DECLARATION OF CHAR array)

第二种情况:(CHAR数组的本地声明)

#include<stdio.h>
void main()
{
     char name[10];/* Now it will only accepts 10 chars NOT MORE */ 
     printf("\n ENter name :\t");
     scanf("%s",name);
}

why there is difference in acceptance of the chars in 1st case it accepts more than 10 but in 2nd exactly 10 but not more.I dont know why,but it happens???

为什么在第1个案例中,对chars的接受程度有差异,它接受10个以上,但在2个恰好10个,而不是更多。我不知道为什么,但它会发生???

3 个解决方案

#1


2  

In both case entering string more than 10 chars (including \0) will invoke undefined behavior because you are writing past to array bound (allocated memory).
In this case, saying that first worked and second doesn't for more than 10 chars is meaningless. Any expected or unexpected behavior can be seen.

在这两种情况下,输入字符串超过10个字符(包括\0)将调用未定义的行为,因为您正在将过去写入数组绑定(分配内存)。在这种情况下,说第一个工作和第二个不超过10个字符是没有意义的。任何期望或意想不到的行为都可以被看到。

#2


1  

C does not do bounds checking on array accesses, so it won't automatically raise an exception if you write past the end of the array. The behavior of writing past the end of the array is undefined; the compiler is not required to handle that error in any particular way. A really smart compiler may issue a diagnostic and halt translation. Or it may compile the code as though nothing's wrong (which is the usual behavior). When run, your code may crash immediately, or it may continue to run in a bad state and crash later (been in that movie before; not fun), or it may continue to run without any issues at all.

C没有对数组访问进行边界检查,因此如果您在数组的末尾写入,它不会自动抛出异常。在数组结束时写入的行为是未定义的;编译器不需要以任何特定的方式处理该错误。一个真正聪明的编译器可能会发出诊断和停止翻译。或者它可以编译代码,好像什么都没有错(这是通常的行为)。当运行时,您的代码可能会立即崩溃,或者它可能会继续运行在一个糟糕的状态,然后崩溃(在之前的电影中);没有乐趣),或者它可能继续运行,没有任何问题。

The C language assumes you know how big your arrays are, and that you are smart enough to not wander outside of their boundaries. It won't prevent you from doing so, but at that point it makes no guaratees about your program's behavior.

C语言假设你知道你的数组有多大,并且你足够聪明,不会在他们的边界之外徘徊。它不会阻止你这样做,但在这一点上,它对你的程序的行为没有任何保护。

#3


0  

When you write more than 10 characters into name, you are stepping on unauthorized memory.

当您将超过10个字符写入名称时,您将进入未授权的内存。

When n is defined in the global namespace you are corrupting some memory locations but bad side effects are not visible right away in your particular case. Since the behavior is undefined, for a different use case, you might see the bad side effects right away or in a delayed fashion.

当n在全局名称空间中定义时,您正在损坏一些内存位置,但在您的特定情况下,不可见不良的副作用。由于该行为是未定义的,对于不同的用例,您可能会立即看到不好的副作用或延迟的方式。

When n is defined in the function, you are corrupting local stack memory and the bad side effects are visible right away in your particular case.

当在函数中定义n时,您正在破坏本地堆栈内存,并且在您的特定情况下,不良的副作用是可见的。

#1


2  

In both case entering string more than 10 chars (including \0) will invoke undefined behavior because you are writing past to array bound (allocated memory).
In this case, saying that first worked and second doesn't for more than 10 chars is meaningless. Any expected or unexpected behavior can be seen.

在这两种情况下,输入字符串超过10个字符(包括\0)将调用未定义的行为,因为您正在将过去写入数组绑定(分配内存)。在这种情况下,说第一个工作和第二个不超过10个字符是没有意义的。任何期望或意想不到的行为都可以被看到。

#2


1  

C does not do bounds checking on array accesses, so it won't automatically raise an exception if you write past the end of the array. The behavior of writing past the end of the array is undefined; the compiler is not required to handle that error in any particular way. A really smart compiler may issue a diagnostic and halt translation. Or it may compile the code as though nothing's wrong (which is the usual behavior). When run, your code may crash immediately, or it may continue to run in a bad state and crash later (been in that movie before; not fun), or it may continue to run without any issues at all.

C没有对数组访问进行边界检查,因此如果您在数组的末尾写入,它不会自动抛出异常。在数组结束时写入的行为是未定义的;编译器不需要以任何特定的方式处理该错误。一个真正聪明的编译器可能会发出诊断和停止翻译。或者它可以编译代码,好像什么都没有错(这是通常的行为)。当运行时,您的代码可能会立即崩溃,或者它可能会继续运行在一个糟糕的状态,然后崩溃(在之前的电影中);没有乐趣),或者它可能继续运行,没有任何问题。

The C language assumes you know how big your arrays are, and that you are smart enough to not wander outside of their boundaries. It won't prevent you from doing so, but at that point it makes no guaratees about your program's behavior.

C语言假设你知道你的数组有多大,并且你足够聪明,不会在他们的边界之外徘徊。它不会阻止你这样做,但在这一点上,它对你的程序的行为没有任何保护。

#3


0  

When you write more than 10 characters into name, you are stepping on unauthorized memory.

当您将超过10个字符写入名称时,您将进入未授权的内存。

When n is defined in the global namespace you are corrupting some memory locations but bad side effects are not visible right away in your particular case. Since the behavior is undefined, for a different use case, you might see the bad side effects right away or in a delayed fashion.

当n在全局名称空间中定义时,您正在损坏一些内存位置,但在您的特定情况下,不可见不良的副作用。由于该行为是未定义的,对于不同的用例,您可能会立即看到不好的副作用或延迟的方式。

When n is defined in the function, you are corrupting local stack memory and the bad side effects are visible right away in your particular case.

当在函数中定义n时,您正在破坏本地堆栈内存,并且在您的特定情况下,不良的副作用是可见的。