简体   繁体   English

运行时检查失败 #2 - 变量“IDNumber”周围的堆栈已损坏

[英]Run-Time Check Failure #2 - Stack around the variable 'IDNumber' was corrupted

#include <iostream>
#include <string.h>
#include <time.h> 

using namespace std;

struct MyID
{
    char FirstName[10]; // array for lenight of the word.
    char LastName[10]; // array for lenight of the word.
    int IdNumber;
};

void InitializeArray(MyID IDNumber[], int Size);
//void SortTheArray(MyID IDNumber[], int Size);
int main(){
    const int Size = 100;
    MyID IDNumber[Size];

    strcpy_s(IDNumber[Size].FirstName, "Aziz");
    strcpy_s(IDNumber[Size].LastName, "LEGEND");
    // I believe the error is around here. 

    InitializeArray(IDNumber, Size);
    //SortTheArray(IDNumber, Size);
}

void InitializeArray(MyID IDNumber[], int Size){

    //srand(time(0));
    for (int i = 0; i < Size; i++){
        //IDNumber[i].IdNumber = rand() %100 ;

        cout<<IDNumber[i].FirstName<<endl;
        IDNumber[i].LastName;
    }
}

在此处输入图片说明

I have this problem, every time I want to test my function and struct, this error will prompt.我有这个问题,每次我想测试我的函数和结构时,都会提示这个错误。 Also, I want to see if my name will print correctly before continue to write rest program.另外,在继续编写rest程序之前,我想看看我的名字是否打印正确。 The idea is I want to print same name every time without ask user to print name every time.这个想法是我想每次都打印相同的名字而不要求用户每次都打印名字。 Also, I have upload the picture of result if you want to see it.另外,如果你想看的话,我已经上传了结果的图片。

Because you are using arrays, you are experiencing buffer overrun error:因为您使用的是数组,所以您遇到缓冲区溢出错误:

const int Size = 100;
MyID IDNumber[Size];

strcpy_s(IDNumber[Size].FirstName, "Aziz");
strcpy_s(IDNumber[Size].LastName, "LEGEND");

The expression IDNumber[Size] is equivalent to IDNumber[100] .表达式IDNumber[Size]等价于IDNumber[100]

In C++, array slot indices go from 0 to Size - 1 .在 C++ 中,数组槽索引从0Size - 1 You are accessing one past the end of the array.您正在访问数组末尾的一个。

Edit 1: Initializing an array编辑 1:初始化数组
Based on your comment, you can use a loop to initialize the slots in an array (vector):根据您的评论,您可以使用循环来初始化数组(向量)中的插槽:

struct Person
{
  std::string first_name;
  std::string last_name;
};

const unsigned int CAPACITY = 100;

int main()
{
  std::vector<Person> database(CAPACITY);
  Person p;
  std::ostringstream name_stream;
  for (unsigned int i = 0; i < CAPACITY; ++i)
  {
    name_stream << "Aziz" << i;
    database[i].first_name = name_stream.str();
    database[i].last_name = "LEGEND";
  }
  return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 运行时检查失败#2-变量&#39;indices&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'indices' was corrupted 运行时检查失败#2-变量&#39;result&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'result' was corrupted 运行时检查失败#2-变量&#39;numberchoices&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'numberchoices' was corrupted 运行时检查失败#2-变量&#39;ex&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'ex' was corrupted 运行时检查失败#2-变量&#39;NearID&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'NearID' was corrupted 运行时检查失败-变量周围的堆栈已损坏 - Run-Time Check Failure - Stack around variable was corrupted 运行时检查失败#2-变量&#39;s&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 's' was corrupted 运行时检查失败#2 - 变量&#39;B&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'B' was corrupted 运行时检查失败#2-变量周围的堆栈-已损坏 - Run-Time Check Failure #2 - Stack around the variable — was corrupted 运行时检查失败 #2 - 变量“newRow”周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'newRow' was corrupted
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM