free()中提示使用未初始化内存

问题遇到的现象和发生背景

32行 提示使用未初始化的内存“*b.a”,请问是什么原因,如何解决

用代码块功能插入代码,请勿粘贴截图
#include 
#include

struct A {
    double ax;
};

struct B {
    double bx;
    A* a;
};

int main() {
    using namespace std;
    B* b = (B*)malloc(sizeof(B) * 2);
    if (!b) {
        cout << "malloc fail" << endl;
        system("pause");
        exit(1);
    }
    for (int i = 0; i < 2; i++) {
        b[i].a = (A*)malloc(sizeof(A) * 2);
        if (!b[i].a) {
            cout << "malloc fail" << endl;
            system("pause");
            exit(1);
        }
    }
    b[0].a[1].ax = 2.;
    cout << b[0].a[1].ax << endl;
    for (int i = 0; i < 2; i++) {
        free(b[i].a);
    }
    free(b);
}

没有任何问题:


#include <stdlib.h>
#include <malloc.h>
#include <iostream>
using namespace std;
struct A {
    double ax;
};
struct B {
    double bx;
    A* a;
};
int main() {
    B* b = (B*)malloc(sizeof(B) * 2);
    if (!b) {
        cout << "malloc fail" << endl;
        system("pause");
        exit(1);
    }
    for (int i = 0; i < 2; i++) {
        b[i].a = (A*)malloc(sizeof(A) * 2);
        if (!b[i].a) {
            cout << "malloc fail" << endl;
            system("pause");
            exit(1);
        }
    }
    b[0].a[1].ax = 2.;
    cout << b[0].a[1].ax << endl;
    for (int i = 0; i < 2; i++) {
        free(b[i].a);
    }
    free(b);
    return 0;
}