From 1c4694a87af6a63794c0d7d13f0cb9bbf1e48981 Mon Sep 17 00:00:00 2001 From: Mehrdad Shirvani Date: Wed, 3 Dec 2025 15:48:24 +0330 Subject: [PATCH] vault backup: 2025-12-03 15:48:24 --- src/content/01-topics/05-arrays/3-Q&A.md | 421 +++++++++++++++++++++++ 1 file changed, 421 insertions(+) create mode 100644 src/content/01-topics/05-arrays/3-Q&A.md diff --git a/src/content/01-topics/05-arrays/3-Q&A.md b/src/content/01-topics/05-arrays/3-Q&A.md new file mode 100644 index 0000000..d42d5a1 --- /dev/null +++ b/src/content/01-topics/05-arrays/3-Q&A.md @@ -0,0 +1,421 @@ + + +## ❓ Question 1 + +> Question: _“Where are Variable Length Arrays (arrays sized using a runtime variable) allowed in C++ and where are they not?”_ + +> [!info] **Answer** +> Variable Length Arrays (**VLAs**) are **not allowed** in standard C++. +> C++ requires the size of a built-in array to be a **constant expression** known at compile time. + +- ❌ **Not acceptable in standard C++** (C++11/14/17/20/23/…): + ```cpp + int n = 5; + int arr[n]; // Not valid C++ + ``` +- ✔️ **Acceptable in standard C** (since C99): C allows VLAs. +- ✔️ **Allowed in GCC as an extension**, but **not portable** and not standard. +Reference: +- GCC documentation: [https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html](https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html) +- C++ restriction explanation: [https://www.geeksforgeeks.org/why-variable-length-array-were-removed-in-cpp/](https://www.geeksforgeeks.org/why-variable-length-array-were-removed-in-cpp/) + + +--- + +## ❓ Question 2 + +> Question: _“Is it valid to use a Variable Length Array when compiling with GCC? In which situations is it not valid?”_ + +> [!info] **Answer** +> GCC **does allow** VLAs in C++ mode, but **only as a non-standard compiler extension**. + +✔️ **OK in GCC when:** + +- Used inside functions (automatic storage): + + ```cpp + int n = 10; + int arr[n]; // GCC accepts, but non-standard + ``` + + +❌ **Not OK when:** + +- Writing portable or standards-compliant C++ + +- Using MSVC, Clang with strict mode, or compilers that reject VLAs + +- Using strict flags: + + - `-std=c++20 -pedantic` + + - `-Wvla` + + - `-Werror` + + +GCC reference: +[https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html](https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html) + +--- + +## ❓ Question 3 + +> Question: _“Why are Variable Length Arrays considered unacceptable or non-standard in C++?”_ + +> [!info] **Answer** +> VLAs are rejected by the C++ standard for several reasons: + +1. **Compile-time determinism** + C++ requires array sizes to be known at compile time for built-in arrays. + +2. **Portability issues** + Many compilers (MSVC, strict Clang modes) do not support VLAs at all. + +3. **Safety considerations** + Runtime-sized stack arrays risk unpredictable stack usage and stack overflow. + +4. **C++ philosophy** + C++ encourages using **RAII containers** (`std::vector`, `std::array`) instead of raw arrays with runtime sizes. + +Reference: +[https://www.geeksforgeeks.org/why-variable-length-array-were-removed-in-cpp/](https://www.geeksforgeeks.org/why-variable-length-array-were-removed-in-cpp/) + +--- + +## ❓ Question 4 + +> Question: _“Can `const` or `constexpr` be used to provide a size for arrays in C++ when the goal is to avoid Variable Length Arrays?”_ + +> [!info] **Answer** +> Yes — **but only if the value is a compile-time constant expression**. + +✔️ Allowed: + +```cpp +constexpr int N = 10; +int arr[N]; // Valid C++ +``` + +⚠️ Not allowed: + +```cpp +int n = get_input(); +const int x = n; +int arr[x]; // Still NOT valid — not a constant expression +``` + +Summary: +- `constexpr` → always compile-time constant → valid for array sizes +- `const` → **not enough** unless initialized with a compile-time constant +- For real runtime sizes, use `std::vector`. + +Reference: +[https://en.cppreference.com/w/cpp/language/array](https://en.cppreference.com/w/cpp/language/array) + + +## ❓ Question + +> Question: _“How are arrays passed to functions in C++? Is it allowed?”_ + +> [!info] **Answer** +> Yes, it is allowed. +> In C++, when you pass a built-in array to a function, the array **decays into a pointer** to its first element. +> Example: + +```cpp +void foo(int arr[]) { } // arr becomes int* +void foo(int* arr) { } // same thing +``` + +So even though an array looks like it's being passed, **the function receives only a pointer**, not the entire array. + +Reference: [https://en.cppreference.com/w/cpp/language/array](https://en.cppreference.com/w/cpp/language/array) + +--- + +## ❓ Question 2 + +> Question: _“Are arrays passed by reference or by value?”_ + +> [!info] **Answer** +> A raw array parameter is **never** passed by value. +> The array **decays** to a pointer → this behaves like **pass-by-pointer**, not value. + +- ❌ Not pass-by-value (copying an entire array is not what happens) + +- ✔️ Effectively pass-by-reference (because the pointer can modify the original array) + + +If you _do_ want pass-by-value semantics, you must use something like: + +```cpp +void foo(std::array arr); // Copies the entire array +``` + +Reference: [https://en.cppreference.com/w/cpp/language/array](https://en.cppreference.com/w/cpp/language/array) + +--- + +## ❓ Question 3 + +> Question: _“Can you control this behavior? Can you pass arrays by value or by reference explicitly?”_ + +> [!info] **Answer** +> Yes — but **only using references or std::array**. + +✔️ **Pass entire array by reference** (preserves size): + +```cpp +void foo(int (&arr)[5]); // Reference to array of 5 ints +``` + +✔️ **Pass entire array by value** (copies the whole array): + +```cpp +void foo(std::array arr); // Copy +``` + +✔️ **Pass entire array by reference (modern way)**: + +```cpp +void foo(const std::array& arr); +``` + +❌ You **cannot** pass a built-in array by value directly. +The syntax does not exist in C++. + +Reference: [https://en.cppreference.com/w/cpp/language/references](https://en.cppreference.com/w/cpp/language/references) + +--- + +## ❓ Question 4 + +> Question: _“Should you specify the size of arrays when passing them? For 1D, 2D, 3D, etc.?”_ + +> [!info] **Answer** +> It depends on the declaration style. + +### ⭐ 1D arrays + +You do **not** specify the size: + +```cpp +void foo(int arr[]); // OK +void foo(int* arr); // Same +``` + +### ⭐ Multi-dimensional (2D, 3D, …) arrays + +All **inner dimensions must be known**: + +```cpp +void foo(int arr[][5]); // OK +void foo(int arr[3][5]); // OK +void foo(int arr[][5][10]); // Higher dimensions OK +``` + +But the **first dimension** may be left unspecified because it becomes a pointer: + +``` +arr → pointer to an array of 5 ints +``` + +Reference: [https://en.cppreference.com/w/cpp/language/array](https://en.cppreference.com/w/cpp/language/array) + +--- + +## ❓ Question 5 + +> Question: _“Can you use variables as sizes in function parameters? In standard C++ and GCC?”_ + +> [!info] **Answer** + +### ✔️ Standard C++ + +- **NOT allowed**: + Inner dimensions must be compile-time constants. + + +```cpp +void foo(int arr[][n]); // ❌ Not standard C++ +``` + +### ✔️ GCC (as extension) + +- GCC allows **Variable Length Arrays (VLA)** in function parameters. + + +```cpp +void foo(int n, int arr[][n]); // ✔️ GCC extension +``` + +But this is **non-portable** and not valid standard C++. + +References: + +- Standard rule: [https://en.cppreference.com/w/cpp/language/array](https://en.cppreference.com/w/cpp/language/array) +- GCC VLA extension: [https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html](https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html) + +--- + +## ❓ Question 6 + +> Question: _“What are the different notations for passing arrays to functions?”_ + +> [!info] **Answer** +> C++ supports several styles: + +### 1️⃣ Pointer style + +```cpp +void foo(int* arr); +``` + +### 2️⃣ Array style (decays to pointer) + +```cpp +void foo(int arr[]); +void foo(int arr[10]); // Size ignored by compiler +``` + +### 3️⃣ Multi-dimensional + +```cpp +void foo(int arr[][5]); +``` + +### 4️⃣ Reference to array (preserves actual size) + +```cpp +void foo(int (&arr)[10]); +``` + +### 5️⃣ Using std::array (recommended for fixed sizes) + +```cpp +void foo(std::array& arr); +``` + +### 6️⃣ Using std::vector (recommended for dynamic sizes) + +```cpp +void foo(std::vector& arr); +``` + +Reference: [https://en.cppreference.com/w/cpp/container](https://en.cppreference.com/w/cpp/container) + +## ❓ Question: Can you somehow preserve an array’s size and use it in functions? + +> **Answer** +> Yes — but only if you pass the array in a way that preserves its compile-time size. By default, when you pass a built-in C-style array to a function, it “decays” into a pointer (so size information is lost). ([GeeksforGeeks](https://www.geeksforgeeks.org/cpp/what-is-array-decay-in-c-how-can-it-be-prevented/?utm_source=chatgpt.com "What is Array Decay in C++? How can it be prevented? - GeeksforGeeks")) + +If you want to preserve the size, you can pass by reference to an array. Example: + +```cpp +void func(int (&arr)[10]) { + // Here, sizeof(arr)/sizeof(arr[0]) works: size = 10. +} +``` + +Because `arr` is a reference to an array of 10 ints, the function knows the array’s size at compile time. ([GeeksforGeeks](https://www.geeksforgeeks.org/cpp/pass-array-to-functions-in-cpp/?utm_source=chatgpt.com "Pass Array to Functions in C++ - GeeksforGeeks")) + +If you need a function that works for arrays of _any_ compile-time-known size, you can use a template: + +```cpp +template +void func(int (&arr)[N]) { + // N is deduced; you can use it inside the function +} +``` + +This way the function “remembers” the array size. This pattern avoids the “decay to pointer” problem. ([CodeArchPedia.com](https://openillumi.com/en/en-cpp-c-array-decay-type-size-basics/?utm_source=chatgpt.com "C++ Array Decay: Types, Sizes, and Fixing Pointer Conversion Bugs - CodeArchPedia.com")) + +Alternatively, many modern C++ codebases avoid raw arrays and instead use container types (e.g. `std::array`, `std::vector`) which carry size information or allow querying their size. ([xeverous.github.io](https://xeverous.github.io/cpp/tutorials/beginner/08_arrays/03_std_array/?utm_source=chatgpt.com "modern C++ by Xeverous - 03 - std::array")) + +--- + +## ❓ Question: How to initialize an array to all zeros? — in 1D, 2D, 3D, etc. + +> **Answer** + +- For a 1D built-in array in C++, you can zero-initialize like: + ```cpp + int arr[5] = {0}; // all 5 elements become 0 + ``` + + ([GitLab](https://fintechpython.pages.oit.duke.edu/jupyternotebooks/3-CPlusCPlus/14-Built-InArrays/answers/rq-14-answers.html?utm_source=chatgpt.com "CPlusPlus / Built-In Arrays — Programming for Financial Technology")) + +- For multi-dimensional (2D, 3D, ...) built-in arrays, you can do nested brace initialization (or rely on partial zero initialization). For example: + + ```cpp + int mat[3][4] = {0}; // all elements become 0 + ``` + + Or for 3D: + + ```cpp + int cube[2][3][4] = {0}; + ``` + + The `{0}` initializes the first element to zero, and all other elements are zero-initialized by default (aggregate initialization rules). ([GitLab](https://fintechpython.pages.oit.duke.edu/jupyternotebooks/3-CPlusCPlus/14-Built-InArrays/answers/rq-14-answers.html?utm_source=chatgpt.com "CPlusPlus / Built-In Arrays — Programming for Financial Technology")) + +- Similarly, if you use `std::array`, you can zero-initialize: + + ```cpp + std::array arr = {0}; + ``` + + All members will be zero-initialized. ([magodo's blog](https://magodo.github.io/array-string-pointer-reference/?utm_source=chatgpt.com "C++ Array, String, Pointer and Reference")) + + +So yes — you _can_ initialize multi-dimensional arrays (1D, 2D, 3D, etc.) to zero with a simple initializer (or nested braces). + +--- + +## ❓ Question: Why is there no compile-time error when you access out-of-bounds in a built-in array (i.e. “out of index”)? + +> **Answer** +> Because built-in (C-style) arrays in C++ do **not** perform any bounds checking. The language simply allows `arr[i]` for any `i`, and does not verify at runtime whether `i` is valid. Accessing outside the allocated bounds is undefined behavior. ([GitLab](https://fintechpython.pages.oit.duke.edu/jupyternotebooks/3-CPlusCPlus/14-Built-InArrays/answers/rq-14-answers.html?utm_source=chatgpt.com "CPlusPlus / Built-In Arrays — Programming for Financial Technology")) + +That means the compiler does not generate an error or warning (in general) if you index beyond the array’s size — it's up to the programmer to ensure correctness. This is a known risk of raw arrays. ([Stack Overflow](https://stackoverflow.com/questions/33319739/why-arent-built-in-arrays-safe?utm_source=chatgpt.com "c++ - Why aren't built-in arrays safe? - Stack Overflow")) + +Because of this inherent unsafety (lack of bounds checking, size information lost when passing arrays, no easy way to return raw arrays, etc.), many C++ developers prefer safer alternatives (see below). ([Stack Overflow](https://stackoverflow.com/questions/33319739/why-arent-built-in-arrays-safe?utm_source=chatgpt.com "c++ - Why aren't built-in arrays safe? - Stack Overflow")) + +--- + +## ❓ Question: What is the “standard array” (i.e. `std::array`)? What are the differences between `std::array` and built-in arrays? When is it best to use it? + +> **Answer** +> `std::array` is a template class in C++ standard library representing a fixed-size array of `N` elements of type `T`. It behaves like a thin wrapper over a built-in array, but with advantages. ([xeverous.github.io](https://xeverous.github.io/cpp/tutorials/beginner/08_arrays/03_std_array/?utm_source=chatgpt.com "modern C++ by Xeverous - 03 - std::array")) + +**Differences / Advantages compared to built-in arrays:** + +- **No “decay to pointer” when passed to functions**: `std::array` keeps size information. If you pass `std::array` to a function by value or by reference, the size is known and preserved. ([xeverous.github.io](https://xeverous.github.io/cpp/tutorials/beginner/08_arrays/03_std_array/?utm_source=chatgpt.com "modern C++ by Xeverous - 03 - std::array")) + +- **Supports assignment, copy, move semantics**: Unlike built-in arrays (which are not assignable or copyable as a whole), `std::array` behaves like a regular object. ([Stack Overflow](https://stackoverflow.com/questions/33319739/why-arent-built-in-arrays-safe?utm_source=chatgpt.com "c++ - Why aren't built-in arrays safe? - Stack Overflow")) + +- **Has member functions & safer access**: For example, `.at()` performs bounds checking (throws exception on invalid index), while operator `[]` still gives raw access (no bounds check) — unlike built-in arrays where you only get `[]`. ([magodo's blog](https://magodo.github.io/array-string-pointer-reference/?utm_source=chatgpt.com "C++ Array, String, Pointer and Reference")) + +- **Interoperability with standard library algorithms**: `std::array` supports iterators, `std::begin()`, `std::end()`, which makes it easier to integrate with STL algorithms. ([xeverous.github.io](https://xeverous.github.io/cpp/tutorials/beginner/08_arrays/03_std_array/?utm_source=chatgpt.com "modern C++ by Xeverous - 03 - std::array")) + + +**When is `std::array` best to use:** + +- When the array size is known at compile time and fixed. + +- When you want safer semantics — e.g. ability to copy/assign arrays, pass around by value or reference, avoid the “array decay” problems, and optionally get bounds-checked access via `.at()`. + +- When you want to use standard library features (iterators, algorithms) with array data. + + +For dynamic or runtime-determined sizes (or if size can change), other types like `std::vector`, or (since C++20) `std::span` or dynamic containers are more appropriate. ([xeverous.github.io](https://xeverous.github.io/cpp/tutorials/beginner/08_arrays/03_std_array/?utm_source=chatgpt.com "modern C++ by Xeverous - 03 - std::array")) + +--- + + +## What is string in c++? object? class? +## Are strings passed by ref ? +## Are strnigs objects that are in stack? in heap? +## Do strings use arrays in themselves? +## Are strings immutable? are they not? \ No newline at end of file