C言語クイズ

6 min read読了の目安(約5700字

メモリーリークとは?どうやって防ぐか?

A memory leak is a dynamically allocated block of memory that has no pointers pointing to it anywhere in the data space of the program. Such blocks are orphaned memory. Because there are no pointers pointing to the blocks, programs cannot even reference them, much less free them.

最終的に

Memory leaks result in increased virtual memory consumption and generally result in memory fragmentation. This may slow down the performance of your program and the whole system.

これはJavaでも起こる

A Memory Leak is a situation when there are objects present in the heap that are no longer used, but the garbage collector is unable to remove them from memory and, thus they are unnecessarily maintained.

A memory leak is bad because it blocks memory resources and degrades system performance over time. And if not dealt with, the application will eventually exhaust its resources, finally terminating with a fatal java.lang.OutOfMemoryError.

どうやって防ぐか?

使用されてない、動的に割り当てられたメモリを利用後に都度解放する。

Stack Overflowとは何か?

A stack is like a special buffer, or working memory, where processes are tracked. The stack is where processes or tasks “keep notes” on what they need to do the next time the processor becomes available. The stack works “last in, first out,” and tracks local variables as they get pushed onto and popped off the stack. The stack tracks function arguments, stores local variables, compiler temporaries, return addresses, and interrupt contexts. The life span of tracking a function on the stack lasts only as long as the duration of the function, so the stack contents are constantly changing.
Stack overflow is when a function or program uses more memory than is in the stack. As it grows beyond its allocated space, the dynamic stack contents begin to overwrite other things, such as critical application code and data.

https://www.microcontrollertips.com/faq-avoiding-stack-overflow-embedded-processors

compilationとinterpretationの違いは何か?

Compilers were the first sort of translator program to be written. The idea is simple: You write the program, then hand it to the compiler which translates it. Then you run the result.
An interpreter is also a program that translates a high-level language into a low-level one, but it does it at the moment the program is run. You write the program using a text editor or something similar, and then instruct the interpreter to run the program. It takes the program, one line at a time, and translates each line before running it: It translates the first line and runs it, then translates the second line and runs it etc.

arrayのindexはなぜ0から始まるか?

zero-based numberingというらしい。

https://en.wikipedia.org/wiki/Zero-based_numbering

In C, the name of an array is essentially a pointer [but see the comments], a reference to a memory location, and so the expression array[n] refers to a memory location n elements away from the starting element. This means that the index is used as an offset. The first element of the array is exactly contained in the memory location that array refers (0 elements away), so it should be denoted as array[0].

translation unitは何か?

日本語で「翻訳単位」

Translation Unit : A translation unit is a file containing source code, header files and other dependencies. All of these sources are grouped together in a file for they are used to produce one single executable object. It is important to link the sources together in a meaningful way. For example, the compiler should know that printf definition lies in stdio header file.

だんだん複雑なプログラムを開発するようになると ソーステキストが長くなり,一つのソースファイルで作成していると 全体を見通すのが難しくなってきます. またほんの一部だけを修正をしたときにも 全体をコンパイルし直さなければならないので, コンパイルに時間がかかるようになります. よって,プログラムをいくつかの部分に分けて 別々に開発したいという要求が自然に起こってきます.
このような考えからプログラミング言語には 一つのプログラムを複数に分割できる機能が提供されることが多く, そのような各部分のことを一般にはモジュールと呼びます. C言語ではモジュールは翻訳単位と呼ばれ, おおまかには一つのソースファイルを一つの翻訳単位に対応 させることで実現しています. ただしヘッダファイル(拡張子が.hのファイル)のインクルードの処理があるので, 厳密には一つのソースファイルに 前処理を行った後のプログラムテキストが翻訳単位になります.

C言語や C++ において、1つのソースファイルと、そこからインクルードされるファイル(たとえばヘッダファイル)を加えたものに、プリプロセスの処理を行ったあとのものを指します。最終的な実行可能ファイルを得るために、翻訳単位に対してコンパイルを行います。

A program consists of one or more translation units. A translation unit consists of an implementation file and all the headers that it includes directly or indirectly. Implementation files typically have a file extension of cpp or cxx. Header files typically have an extension of h or hpp. Each translation unit is compiled independently by the compiler. After the compilation is complete, the linker merges the compiled translation units into a single program. Violations of the ODR rule typically show up as linker errors. Linker errors occur when the same name has two different definitions in different translation units.

Arrayに同じ型しか保存できない理由は?

https://www.quora.com/Why-are-all-the-elements-of-an-array-of-the-same-type