From ada29e7cb307215cacad7647f2594bb31794c19b Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Tue, 14 Jul 2026 00:57:30 +0000 Subject: [PATCH] fix: Replace non-standard VLAs with std::vector in C++ Huffman code The huffmanCodes.cpp script previously used C-style Variable Length Arrays (VLAs), which are non-standard in C++ and can cause segfaults or undefined behavior. This commit replaces those VLAs with memory-safe std::vector implementations. Function signatures were updated to take std::vector references instead of pointers, and outdated size calculations have been removed. Additionally, resolved signed/unsigned comparison warnings in the output loops. Co-authored-by: tsainez <13399044+tsainez@users.noreply.github.com> --- c++/huffmanCodes.cpp | 25 ++++++------------------- 1 file changed, 6 insertions(+), 19 deletions(-) diff --git a/c++/huffmanCodes.cpp b/c++/huffmanCodes.cpp index 0f85a1a..08e092d 100644 --- a/c++/huffmanCodes.cpp +++ b/c++/huffmanCodes.cpp @@ -46,7 +46,7 @@ struct Min { } }; -void printBits(int* b, int location, int tmp) { +void printBits(const vector& b, int location, int tmp) { globalBits.push_back(-1); // '-1' is a placeholder, see below // The algorithm actively searches for '-1' tokens globalBits.push_back(tmp); @@ -55,7 +55,7 @@ void printBits(int* b, int location, int tmp) { globalBits.push_back(b[i]); } -void traverse(Node* root, int* arr, int location) { +void traverse(Node* root, vector& arr, int location) { // This function places 0 or 1 into the array depending on if you are // travelling left or right on the tree, and will print out the binary // code if the node is a leaf. @@ -80,7 +80,7 @@ void traverse(Node* root, int* arr, int location) { } } -Node* huffmanEncode(int* arr, int n) { +Node* huffmanEncode(const vector& arr, int n) { priority_queue, Min> minPriorityQueue; for(int i = 0; i < n; i++) @@ -109,11 +109,7 @@ int main() { int n; cin >> n; - // You cannot declare the array in any other way, - // for some reason it causes a segfault or it will - // simply not yield the correct test results. Not sure - // as to why this is the case. Really odd bug. - int arr[n]; + vector arr(n); int counter = 0; for(int i = 0; i < n; i++) { @@ -121,17 +117,8 @@ int main() { counter++; } - int size = sizeof(arr) / sizeof(arr[0]); - - /* - if (size != n) { - cout << "Something went wrong. Dying!"; - return 0; - } - */ - Node* root = huffmanEncode(arr, n); - int bits[n]; + vector bits(n); int leaf = 0; // We gotta fill the tree, so we traverse it. @@ -153,7 +140,7 @@ int main() { // Now we can ascertain to where the exact match is. match = distance(globalBits.begin(), it) + 1; - for(int j = match; j < globalBits.size(); j++) { + for(size_t j = match; j < globalBits.size(); j++) { if(globalBits[j] == -1) { end = j; globalBits.erase(globalBits.begin() + match - 2,