向vector<vector<int>>中push vector<int>时触发SIGABRT错误求助
free(): invalid next size (fast) in Permutation Recursion Hey there! That free(): invalid next size (fast) error is a classic memory corruption red flag—it usually means your code is either writing outside the bounds of a memory block, or messing with memory that's already been freed. Let's dig into the most likely issues with your permutation-generating recursive function, and fix them up.
Common Culprits in Permutation Recursion
1. Out-of-Bounds Vector Access
This is the #1 suspect here. When generating permutations recursively, it's easy to slip up with index values—like using an index that's equal to or larger than the vector's size, or accidentally using a hardcoded number instead of nums.size().
For example, if your loop condition is i <= nums.size() instead of i < nums.size(), you'll end up accessing nums[nums.size()], which is way outside the valid memory range. This corrupts the vector's internal metadata, leading to that free() error when you try to push_back later.
2. Accidental Modification via Reference Parameters
If your recursive function takes a vector<int>& (pass-by-reference) instead of a pass-by-value copy, you might be modifying the same vector across multiple recursive branches without realizing it.
Say you swap two elements in one branch, don't swap them back before moving to the next iteration, and then the next branch uses the altered vector. This can scramble the vector's internal structure, causing memory corruption when you try to push it to the result.
3. Unintended Memory Manipulation
Less likely, but possible: if you're mixing manual memory management (like new/delete) with vector operations, you might be freeing memory that's still in use by the vector. Or, if you're modifying the vector's underlying buffer directly (which you should never do!), that can break its internal state.
Example of a Correct Recursive Permutation Implementation
Let's compare to a working version, so you can spot where your code might differ:
#include <vector> #include <algorithm> using namespace std; vector<vector<int>> permute(vector<int>& nums) { vector<vector<int>> result; backtrack(nums, 0, result); return result; } void backtrack(vector<int> nums, int start, vector<vector<int>>& result) { // Base case: if we've fixed all elements, add to result if (start == nums.size()) { result.push_back(nums); return; } // Swap each element from start onwards into the current position for (int i = start; i < nums.size(); ++i) { swap(nums[start], nums[i]); // Recurse with the next position to fix backtrack(nums, start + 1, result); // No need to swap back here—we're using a copy of nums for each branch } }
Key notes here:
- The
backtrackfunction takesnumsby value, so each recursive branch gets its own copy—no cross-branch interference. - The loop uses
i < nums.size()to avoid out-of-bounds access. - The base case checks
start == nums.size()to ensure we only push complete permutations.
Debugging Steps to Fix Your Code
- Check Index Boundaries: Go through every place you access a vector element (like
nums[i]) and confirm the index is always between0andnums.size() - 1. - Verify Parameter Passing: If you're using pass-by-reference for
numsin the recursive function, make sure you swap elements back after the recursive call (to undo the change for the next iteration):swap(nums[start], nums[i]); backtrack(nums, start + 1, result); swap(nums[start], nums[i]); // Restore original state - Use a Debugger: Fire up
gdb(or your IDE's debugger) to catch the error in action. When the SIGABRT triggers, look at the call stack to see exactly which line caused the crash—this will point you straight to the problematic code. - Print Intermediate States: Add
coutstatements to print the currentnumsvector before pushing it to the result. If the vector looks garbled or has unexpected values, you'll know the corruption is happening earlier in the recursion.
内容的提问来源于stack exchange,提问作者ccpak

