struct.h
#ifndef __STRUCT_H
#define __STRUCT_H
#define SIZE 20
typedef struct {
char m_data1[64];
char m_data2[64];
}Node;
typedef struct MyStruct {
Node **m_ptrToArr;
long m_size;
}List;
List* MyMalloc(int size);
#endif
action.cpp
#include "struct.h"
#include <iostream>
#include <stdlib.h>
using namespace std;
List* MyMalloc(int size) {
size = SIZE;
Node **arr = (Node **)malloc(sizeof(Node *) * size );
for (int i = 0; i < size; i++) {
arr[i] = (Node *)malloc(sizeof(Node));
cout << "malloc : " << arr[i] << endl;
if (!arr[i]) {
cout << __FILE__ << __LINE__ << "error : Space full" << endl;
exit(1);
}
}
List *ptr = (List*)malloc(sizeof(List));
if (!ptr) {
cout << __FILE__ << __LINE__ << "error : Space full" << endl;
exit(1);
}
ptr->m_ptrToArr = arr;
ptr->m_size = size;
return ptr;
}
main.cpp
#include <vld.h>
#include <iostream>
#include "struct.h"
using namespace std;
int main() {
List *ptr = MyMalloc(SIZE);
printf("size : %d \n", ptr->m_size);
for (int i = 0; i < SIZE; i++) {
printf("will free : %p \n", *(ptr->m_ptrToArr + i));
free(*(ptr->m_ptrToArr + i));
}
free(ptr->m_ptrToArr);
free(ptr);
system("pause");
return 0;
}
|