简体   繁体   English

为什么即使我没有为它分配内存也能工作?

[英]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.

相关问题 即使我使用 malloca 分配了内存,为什么仍然调用析构函数? - Why destructor getting called even though I have allocated memory using malloca? 即使我没有提到数组的大小,为什么在编译和运行代码时仍能正常工作? - Why does the code work fine when I compile and run it even though I have not mentioned size of array? 何时将内存分配给预定义的流对象? - When memory gets allocated to predefined stream objects? 即使分配了 memory,也会出现 Memcpy 分段错误 - Memcpy segmentation fault even when memory is allocated 分配的内存被回收 - allocated memory gets reclaimed 在C ++中,即使调用了delete,进程何时保留已分配的内存? - In C++, when does a process retain allocated memory even though delete is called? 为什么重载 new 和 delete 运算符时释放的 memory 小于分配的 memory? - Why does the freed memory is less than allocated memory when overloading new and delete operators? 为什么分配的二维int数组的存储地址序列与普通的数组不同? - Why does an allocated 2-dimensional int array have a different sequence of memory addresses as a normal one? 为什么不使用时分配堆栈内存? - Why is stack memory allocated when it is not used? 为什么即使我打破了关于长度计入NUL终止的规定,RegSetValueEx仍能正常工作? - Why does RegSetValueEx work even when I break the rule about accounting for NUL termination in the length?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM