尝试为指针运行排序算法时出现分段错误(内核已转储)

问题描述 投票:0回答:1

我正在尝试根据与它们关联的字符串为三个指针变量创建一种排序算法。但是,每次尝试运行程序时,经过第一个用户数据入口点后,都会引发分段错误(核心转储)错误。我在许多站点上进行了研究,但未能找到有效的答案。我相信这是我的指针变量的内存分配错误,但是我不知道如何解决它或它在哪里。我应该怎么做才能缓解这种情况?这是一些示例代码:

//new data type
struct Balloon{
string message=""; //give the balloon object a message and a color
string color="";
};

/// main program
int main (void) {

//Instantiate three Balloon objects.
Balloon *front, *middle, *end, *spare;
front, middle, end, spare = new Balloon; //allocate storage for these variables.

//Ask the user what the messages and colors of the balloons are, and set those values to the pointer variables.

//first balloon
cout << "First balloon text: ";
cin >> front->message; //here is when the error is thrown
cout << "Color: ";
cin >> front->color;

我是Ubuntu和C ++的新手,以前只使用过Java,因此对于我可能不经意间犯下的任何严重错误表示歉意。预先感谢您的帮助!

c++ sorting ubuntu segmentation-fault g++
1个回答
0
投票
front, middle, end, spare = new Balloon; //allocate storage for these variables.
© www.soinside.com 2019 - 2024. All rights reserved.