在程序结束时检测到堆栈粉碎

时间:2015-10-05 22:41:18

标签: c arrays struct dynamic-memory-allocation

我目前正在测试较小规模的程序,以便在我尝试通过主函数末尾的Dim list As Range Dim list_readthru As Range Set list = Range("A2", Range("A50").End(xlUp)) For Each list_readthru In list If list_readthru >= 20 And list_readthru <=40 Then Cells(list_readthru.row, 2) = "McFly" End If Next list_readthru End Sub 退出程序时区分问题。

MAIN.C

return 0;

Header.h

#include <stdio.h>
#include <stdlib.h>
#include "Header.h"

int main (void)
{
        int i;
        int Fin = 0;


        Student sStu;
        Array aAry;
        Student *Stu = &sStu;
        Array *Ary = &aAry;

        InitArray(Ary, 1);

        while(Fin != 2)
        {
                printf("Please choose a selection.\n");
                printf("1. Add Student\n");
                printf("2. Print Students\n");
                printf("3. Exit\n");
                scanf("%d", &i);
                switch(i)
                {
                        case 1:
                        {
                                AddStudent(Stu, Ary);
                                break;
                        }
                        case 2:
                        {
                                for(i = 0; i < Ary->Size; i++)
                                {
                                        printf("%s %d\n", Stu[i].Name, Stu[i].IDn);
                                }
                                break;
                        }
                        case 3:
                        {
                                return 0;
                        }
                }
        }
}

Grade.c

#ifndef HEADER_H_
#define HEADER_H_

typedef struct student
{
        char Name[30];
        int IDn;
}Student;

typedef struct array
{
        Student *Student;
        size_t Used;
        size_t Size;
}Array;


void InitArray(Array *Ary, int InitSize);
void AddArray(Array *Ary);
Student AddStudent(Student *Stu, Array *Ary);

#endif

在文件末尾,当我尝试#include <stdio.h> #include <stdlib.h> #include <string.h> #include "Header.h" void InitArray(Array *Ary, int InitSize) { Ary->Student = malloc(InitSize * sizeof(Student)); Ary->Used = 0; Ary->Size = InitSize; memset(&Ary->Student[0], 0 , sizeof(Student) * InitSize); } void AddArray(Array *Ary) { Student Stud; if(Ary->Used == Ary->Size) { Ary->Size *= 2; Ary->Student = realloc(Ary->Student, Ary->Size * sizeof(Student)); } strcpy(Ary->Student[Ary->Used].Name, Stud.Name); Ary->Student[Ary->Used].IDn = Stud.IDn; Ary->Used++; } Student AddStudent(Student *Stu, Array *Ary) { int i; printf("\nStudent ID numbers cannot be the same!\n"); printf("Please enter the student's name: "); scanf("%s", Stu[Ary->Used].Name); printf("Please enter the student's ID Number: "); scanf("%d", &Stu[Ary->Used].IDn); AddArray(Ary); printf("\n"); return; } 时,我收到此错误:

  

*检测到堆栈粉碎* :./a.out已终止   分段错误(核心转储)

当我使用valgrind时,我得到了这个输出:

return 0;

valgrind在我尝试退出程序时由于致命错误而终止。

它只发生在最后,我可以正确打印struct数组的内容。我已经研究过==9966== Invalid write of size 1 ==9966== at 0x402C6C3: strcpy (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so) ==9966== by 0x8048687: AddArray (in /home/ctj0033/CSCE_1040/Homework/2_Homework/2_Test/a.out) ==9966== by 0x804874B: AddStudent (in /home/ctj0033/CSCE_1040/Homework/2_Homework/2_Test/a.out) ==9966== by 0x804881C: main (in /home/ctj0033/CSCE_1040/Homework/2_Homework/2_Test/a.out) ==9966== Address 0x41f804c is 0 bytes after a block of size 36 alloc'd ,因为我很确定错误在于其中,但我不确定我做错了什么。我试图改变我在realloc();中分配内存的方式以及我使用InitArray();的方式,但无济于事。

我做错了什么?

1 个答案:

答案 0 :(得分:2)

您当前的问题是您尝试使用&#34;无效指针&#34;。 (实际上,它是一个完全有效的指针,它只是指向一个错误的位置)

sStuStudentStu是指向它的指针。但是当你添加第二个学生时,你写信至Stu[1] - 这是sStu之后的空格。很可能在这个空间里还有别的东西,所以当你写Stu[1]时,你会覆盖别的东西。 (它完全不可预测 将在那个空间中。)

您似乎希望在Ary->Student处使用Stu

(如果你这样做,你可能会发现更多问题 - 例如你在写入下一个元素后检查是否需要扩展数组