“程序中出现访问冲突(分段错误)。”

Posted

技术标签:

【中文标题】“程序中出现访问冲突(分段错误)。”【英文标题】:“An access violation (Segmentation Fault) raised in your program.” 【发布时间】:2013-08-23 04:55:24 【问题描述】:

我是编程新手,我遇到了一个我从未听说过的错误,如果我按任何超过 A1 的东西就会崩溃 我收到错误“您的程序中出现访问冲突(分段错误)”。 ive almsot 完成了该计划 这阻止了我 为什么我可以输入A1但不能输入A2等

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <stdlib.h>
#include <sstream>
using namespace std;

//function prototype
void fill_array();//fill the array using data from the text file
string Candy_selection[16][4] = "";//checks array for valid data

int main()

string code = "";
string name = "";
double price = 0.0;
double paid = 0.0;
int remaining = 3;
double change = 0.0;
bool item_bought = false;

string convertInt(int number);//converts string to int
fill_array();//fills array


for(int x = 0; x<16; x += 1)

    cout << "The codes are "<<Candy_selection[x][0]<<endl;


while(code != "E5")

    cout << "Enter the candy code: ";//ask for code for candy
    cin >> code;
    int x = 0;
   while(x < 16 && item_bought == false )
    //checks if code matches up with a valid code
        if(code == Candy_selection[x][0])//if code found
        
            name = Candy_selection[x][1];//assigns name of candy to name
            price = (double)atof(Candy_selection[x][2].c_str()); //convert to double 
            remaining = (int)atof(Candy_selection[x][3].c_str());//convert to int

            if(remaining != 0)//if there is any of that candy left continue processing
            
                cout << "You are buying a "<<name<<" The Cost is $"<<price<<endl;//tells how much the candy bar is
                cout << "Please insert money $";
                cin >> paid; 
                if(paid >= price) //makes sure the amount paid is atleast = to the candy bar
                
                  if(paid > price)
                    
                        change = paid - price;//calculates change if any
                     
                    remaining -= 1; //since candy was bought lowers teh amount remaining by one

                    Candy_selection[x][3] = convertInt(remaining);//converts to string and stores updated value in array

                    cout << fixed <<setprecision(2);
                    cout <<"You have bought a " <<name <<" for $"<<price <<" your change is $"<<change<<endl; 

                else cout << "you didnt have enough money "<<endl;//not enough paid
            else cout << "This treat is sold out, please select another"<<endl;//remainder == 0
        else if(code == Candy_selection[16][0])//if code is not found in array
        
            cout << "Code not found "<<endl;//code not found
        
        item_bought = true;//acts as a sentinal value to get out of loop if item is bought
        x+= 1;//moves to next array location

    
    //reseteting values
    code = "";
    item_bought = false;
    name = "";
    price = 0.0;
    paid = 0.0;
    change = 0.0;
    x = 0;

system("pause");


void fill_array()//fills array with data from text file

    string candy_code = "";
    string candy_name = "";
    string candy_price = "";
    string remaining_candy = "";
    ifstream Candy;     
    Candy.open("Candy.txt");
        if(Candy.is_open() == true)
        
            while(Candy.eof() == false)
            

                for(int y = 0;y < 16; y += 1)//puts the data in array then loops on next line then repeats
                
                    getline(Candy, candy_code, '#');//picking the code name from first field
                    getline(Candy, candy_name, '#');//picking the candy name from second field 
                    getline(Candy, candy_price, '#');//picking the candy price from third field 
                    getline(Candy, remaining_candy, '\n');//picking the candy limit in stalk from 4th field 
                    //puts each entry in the array
                    Candy_selection[y][0] = candy_code;
                    Candy_selection[y][1] = candy_name;
                    Candy_selection[y][2] = candy_price;
                    Candy_selection[y][3] = remaining_candy;

                
            
        Candy.close();
        else cout <<"cannot open data file to sync to array"<<endl;


string convertInt(int number)

   stringstream ss;//create a stringstream
   ss << number;//add number to the stream
   return ss.str();//return a string with the contents of the stream

文本文件如下

A1#Jelly Beans#1.00#3
A2#Penut butter cup#1.00#3
A3#PB&J#1.00#3
A4#100 Grand bar#1.00#3
B1#Crackers#1.00#3
B2#gum#1.00#3
B3#gummy worm#1.00#3
B4#chocolot bar#1.00#3
C1#Honey bun#1.00#3
C2#Pretzels#1.00#3
C3#Cinnamin bun#1.00#3
C4#gobstocker#1.00#3
D1#pop corn#1.00#3
D2#krabby paddy#1.00#3
D3#chocolot frog#1.00#3
D4#mint#.25#3

【问题讨论】:

对于初学者来说,while(Candy.eof() == false) 不会像你想象的那样做。 它在开始调试信息时使用 for 循环显示数组的内容,这是 fill_array() 所做的,这就是它只使用一次的原因 【参考方案1】:
code == Candy_selection[16][0]

可能是这样,您正在访问第 17 个元素,但您只有 16 个元素。

如果不是,您需要搜索类似的溢出、空访问等。使用调试器、gdb 或 Visual Studio 运行它

【讨论】:

以上是关于“程序中出现访问冲突(分段错误)。”的主要内容,如果未能解决你的问题,请参考以下文章

linux段错误问题

关于Linux下的段错误

linux下的段错误

linux c 段错误如何定位

C语言调用函数发生段错误,发生段错误的函数是没有执行就段错误了

递归 段错误 习题