调试断言失败。 BIG_ALLOCATION_ALLIGNMENT

Posted

技术标签:

【中文标题】调试断言失败。 BIG_ALLOCATION_ALLIGNMENT【英文标题】:Debug Assertion Failed. BIG_ALLOCATION_ALLIGNMENT 【发布时间】:2017-01-11 13:58:04 【问题描述】:

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

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 的函数都没有这个问题。

【问题讨论】:

【参考方案1】:

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

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

将循环限制替换为for (int i = 0; i &lt; numNotes; i++) 或类似名称。

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

(我也希望看到constexpr int numNotes = 4;:谷歌了解更多详情。)

【讨论】:

以上是关于调试断言失败。 BIG_ALLOCATION_ALLIGNMENT的主要内容,如果未能解决你的问题,请参考以下文章

调试断言失败:_CrtIsValidHeapPointer(pUserData)

调试断言失败,向量下标超出范围

c++错误:调试断言失败

调试断言失败

没有堆栈时如何调试iOS断言失败崩溃

调试断言失败