gpt4 book ai didi

c++ - 调试断言失败。 BIG_ALLOCATION_ALLIGNMENT

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:11:01 25 4
gpt4 key购买 nike

我正在尝试记录用户输入的笔记并将它们存储在一个数组中。验证工作正常,但是当我在循环中输入最后一个值时,我得到:

Debug Assertion Failed!
Expression: "(_Ptr_user & (_BIG_ALLOCATION_ALIGNMENT - 1))==0"&&0
An invalid parameter was passed to a function that considers invalid parameters fatal.

我很难理解问题出在哪里以及如何解决。

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

typedef string noteName;

noteName getNoteName(int i)
{
bool flag = true;
noteName Namein;

do
{
cout << "Please enter note name no. " << i + 1 << ": ";
cin >> Namein;
cout << "------------------------------------\n";

if (Namein.length() > 3 || Namein.length() < 2)
{
cout << "Sorry, a note name must be 2 or 3 characters long. Please try again.\n";
flag = false;
}
else if (Namein.length() == 3 && Namein[1] != '#')
{
cout << "Sorry, the second character of a sharp note name must be #. Please try again.\n";
flag = false;
}
else if ((Namein[0] < 'a' || Namein[0] > 'g') && (Namein[0] < 'A' || Namein[0] > 'G'))
{
cout << "Sorry, the first character of a note name must be a letter between A and G. Please try again.\n";
flag = false;
}
else if (isdigit(Namein.back()) == false)
{
cout << "Sorry, the last character of a note name must be a number. Please try again.\n";
flag = false;
}
else
{
flag = true;
}
} while (flag == false);

return Namein;
}

int main()
{
const int numNotes = 4;

noteName NoteNames[numNotes];

cout << "Hello\n";

for (int i = 0; i <= numNotes; i++)
{
NoteNames[i] = getNoteName(i);
}

cout << "Thank you, the note names and lengths you entered were: \n\n";

for (int i = 0; i <= numNotes; i++)
{
cout << i << ". " << NoteNames[i] << "\n";
}

cout << "Done!";

return 0;
}

我想说这与 getNoteName() 具有 string 返回类型有关,因为我的任何其他返回函数都没有这个问题int.

最佳答案

noteName NoteNames[numNotes];定义一个数组,其中 NoteNames[numNotes - 1]是您可以访问的最大元素。

你比这更进一步。这样做的行为是未定义,它表现为您观察到的崩溃。

将循环限制替换为 for (int i = 0; i < numNotes; i++) ,或类似的。

(您的类名和变量名的 CamelCase 约定也与正常情况不同,这使您的代码难以阅读。)

(我还想看看 constexpr int numNotes = 4; :Google 以获取更多详细信息。)

关于c++ - 调试断言失败。 BIG_ALLOCATION_ALLIGNMENT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41592845/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com