基于元音/辅音对的字符串排序实现异常排查求助
Hey there! Let's break down why your program is crashing with a segmentation fault and not producing any output—these are common pitfalls for new C++ developers, and we can fix them step by step.
1. Critical: Uninitialized Vector Access in data()
Your main issue causing the segmentation fault is right here in the data() function. When you declare vect myvec; in main(), the vector is empty (size = 0). But in your loop, you try to access v[i].name immediately—there's no memory allocated for those elements yet, so you're writing to random memory, which triggers a segfault.
Fix:
Either resize the vector first to hold n elements, or use push_back to add new elements safely:
Option 1 (Resize first):
void data(int& n, vect& v) { cin>>n; v.resize(n); // Allocate space for n stud objects upfront for(int i = 0; i < n; ++i) { cin>>v[i].name; } }
Option 2 (Push elements one by one, safer for dynamic cases):
void data(int& n, vect& v) { cin>>n; v.clear(); // Reset vector in case it had old data for(int i = 0; i < n; ++i) { stud new_stud; cin>>new_stud.name; v.push_back(new_stud); } }
2. Out-of-Bounds String Access in getValue()
Another segfault risk: your loop runs for i < len, but when i reaches len-1, name[i+1] tries to access a character beyond the end of the string (strings are 0-indexed, so valid indices go from 0 to len-1). This is undefined behavior, and almost always causes crashes.
Plus, your logic is meant to check adjacent character pairs—so you only need to loop up to the second-to-last character.
Fix:
Adjust the loop condition and make the vowel checks more explicit:
int getValue(string name) { int value = 0; int len = name.length(); for(int i = 0; i < len - 1; ++i) { // Stop at the second-to-last character bool is_first_vowel = (strchr(voc, name[i]) != nullptr); bool is_second_vowel = (strchr(voc, name[i+1]) != nullptr); if(is_first_vowel && is_second_vowel) { value++; } else if(!is_first_vowel && !is_second_vowel) { value--; } // Vowel-consonant pairs do nothing, which matches your requirement } return value; }
3. Missing Input/Output Header
You're using cout in the display() function, but you never included <iostream>! This will cause a compilation error (the compiler doesn't know what cout is), which might lead to no output even if you fix the segfault.
Fix:
Add this at the top of your code:
#include <iostream>
4. Efficiency Improvement (Non-Critical but Good Practice)
Your display() function takes the vector by value, which means it makes a full copy of the entire vector every time it's called. For small n this isn't a problem, but it's inefficient for larger datasets.
Fix:
Pass the vector by const reference instead:
void display(const vect& v) { // const ensures we don't modify the vector vector<stud>::size_type sz = v.size(); for(unsigned i = 0; i < sz; ++i) { cout<<v[i].name<<'\n'; } }
General Debugging Tips for Beginners
- Check container bounds first: Always make sure you're not accessing a vector/array index that's out of range (0 to size-1). Empty vectors can't be accessed directly!
- Use compiler warnings: Compile with
g++ -Wall -Wextra your_code.cpp—the compiler will flag many potential issues (like uninitialized variables or out-of-bounds risks) before you run the program. - Try a debugger: Tools like
gdbcan help you pinpoint exactly where the crash happens. Rungdb ./your_program, typerun, and when it crashes, usebacktraceto see the call stack and find the problematic line.
内容的提问来源于stack exchange,提问作者Razvan Berbece

