[英]Why does gets work even when I have not allocated memory to it?
I recently answered a question here in SO where the problem was to input a few sentences of code and print it.我最近在 SO 中回答了一个问题,问题是输入几句代码并打印出来。 My code is this:
我的代码是这样的:
#include<stdio.h>
int main() {
clrscr();
int line, i;
char (*string)[100];
printf("How many line?\n");
scanf("%d",&line);
getchar();
for(i=0;i<line;i++) {
gets(string[i]);
}
printf("You entered:\n");
for(i=0;i<line;i++) {
puts(string[i]);
}
return 0;
}
As you see, I haven't allocated any memory to individual strings ie, s[0]
, s[1]
,....... but surprisingly my compiler didn't give me any warnings or errors for it and it works perfectly.如您所见,我没有为单个字符串分配任何内存,即
s[0]
, s[1]
,......但令人惊讶的是,我的编译器没有给我任何警告或错误,它完美地工作。
So I posted this code and frankly got quite a few of downvotes for it (I know I deserved it).所以我发布了这段代码,坦率地说,它得到了很多反对(我知道我应得的)。 Can you please explain why this code works and not give a segmentation error?
你能解释一下为什么这段代码有效而不给出分段错误吗?
My output is as shown:我的输出如图所示:
char (*string)[100];
OK, string
represents a pointer to 100 char
, but it's not initialized.好的,
string
代表一个指向 100 char
的指针,但它没有被初始化。 Therefore, it represents an arbitrary address.因此,它代表一个任意地址。 (Even worse, it doesn't necessarily even represent the same arbitrary address when accessed on subsequent occasions.)
(更糟糕的是,在后续访问时,它甚至不一定代表相同的任意地址。)
gets(string[i]);
Hmm, this reads data from standard input to a block of memory starting at the random address, plus i*100
, until a NUL byte is read.嗯,这将数据从标准输入读取到从随机地址开始的内存块,加上
i*100
,直到读取 NUL 字节。
Not very robust.不是很健壮。 Not guaranteed to produce an error, either.
也不保证会产生错误。 If you didn't get one, the random address must have been mapped to writable bytes.
如果您没有得到,则随机地址必须已映射到可写字节。
As you see, I haven't allocated any memory to individual strings ie,
s[0]
,s[1]
but surprisingly my compiler didn't give me any warnings or errors for it and it works perfectly.如您所见,我没有为单个字符串分配任何内存,即
s[0]
、s[1]
但令人惊讶的是我的编译器没有给我任何警告或错误,并且它完美地工作。
Time to reduce your expectations, or increase the diagnostic level of the compiler.是时候降低您的期望,或者提高编译器的诊断级别。 Try
-Wall -Wextra
, if it takes options in that style.尝试
-Wall -Wextra
,如果它采用那种风格的选项。 It should be warning you that string
is used without being initialized first.应该警告您使用
string
没有先初始化。 A warning that gets
has been deprecated would also be a good idea, although my compiler doesn't mention it.该警告
gets
已被弃用,也将是一个不错的主意,但我的编译器并没有提到它。
C and C++ allow you to manage memory yourself. C 和 C++ 允许您自己管理内存。 You are responsible for validating pointers.
您负责验证指针。 In C, this is a major source of problems.
在 C 中,这是问题的主要来源。
In C++, the solution is to use alternatives to pointers, such as references (which cannot be uninitialized) and smart pointers (which manage memory for you).在 C++ 中,解决方案是使用指针的替代方法,例如引用(不能未初始化)和智能指针(为您管理内存)。 Although the question is tagged C++ and compiles as a C++ program, it's not written in a style that would be called C++.
尽管该问题被标记为 C++ 并编译为 C++ 程序,但它并不是以称为 C++ 的风格编写的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.