WAR 2
///// MAIN
#include "my_tab.h"
#include "VERT.h"
#include <stdio.h>
#include <stdlib.h>
ARR_ELEM* add() {
ARR_ELEM* elem = NULL;
elem = (ARR_ELEM*)malloc(sizeof(ARR_ELEM));
if (!elem)
exit(-1);
VERT* vert = InitVert();
elem->ptr = vert;
elem->print_funct = PrintVert;
elem->free_funct = FreeVert;
}
int main() {
ARR_ELEM* KONTENER = InitArray(2);
for(int i=0; i< 3; i++)
AddElem(KONTENER, add());
if (KONTENER[ARR_ELEM::last_index - 1].ptr)
PrintVert(KONTENER[ARR_ELEM::last_index - 1].ptr);
PrintArr(KONTENER);
FreeArr(KONTENER);
}
///// VERT.cpp
#pragma warning (disable : 4996)
#include "VERT.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
VERT* InitVert()
{
VERT* vert = NULL;
vert = (VERT*)malloc(sizeof(VERT));
if (!vert)
exit(-1);
printf("Input name: ");
scanf("%s", &vert->name);
vert->xy = (double*)malloc(2 * sizeof(double));
if (!vert->xy)
exit(-1);
memset(vert->xy, 0, 2 * sizeof(double));
printf("Input x: ");
scanf("%lf", &vert->xy[0]);
printf("Input y: ");
scanf("%lf", &vert->xy[1]);
return vert;
}
void PrintVert(void* ptr)
{
VERT* vert = (VERT *) ptr;
printf("name: %s\tx: %lf\ty: %lf\n", vert->name, vert->xy[0], vert->xy[1]);
}
void FreeVert(void* ptr)
{
VERT* vert = (VERT*)ptr;
if (vert) {
if (vert->xy) {
free(vert->xy);
vert->xy = NULL;
}
free(vert);
vert = NULL;
}
}
////// VERT.h
#pragma once
struct VERT {
char name[512];
double* xy;
};
VERT* InitVert();
void PrintVert(void *ptr);
void FreeVert(void* ptr);
////// my_tab.cpp
#include "my_tab.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
size_t ARR_ELEM::last_index = 0;
ARR_ELEM* InitArray(size_t size)
{
ARR_ELEM* arr = NULL;
arr = (ARR_ELEM*)malloc(size*sizeof(ARR_ELEM));
if (!arr)
exit(-1);
memset(arr, 0, size * sizeof(ARR_ELEM));
return arr;
}
void AddElem(ARR_ELEM* arr, ARR_ELEM* element)
{
if (!arr)
exit(-1);
size_t last = ARR_ELEM::last_index, total = _msize(arr) / sizeof(ARR_ELEM);
if (last >= total) {
arr = (ARR_ELEM*)realloc(arr, 2*total * sizeof(ARR_ELEM));
if (!arr)
exit(-1);
memset((void*)(arr + total), 0, total * sizeof(ARR_ELEM));
}
arr[last].ptr = element->ptr;
arr[last].print_funct = element->print_funct;
arr[last].free_funct = element->free_funct;
ARR_ELEM::last_index++;
}
void PrintArr(ARR_ELEM* arr)
{
if (!arr)
exit(-1);
for (size_t i = 0; i < ARR_ELEM::last_index; i++) {
(*arr[i].print_funct)(arr[i].ptr);
}
}
void FreeArr(ARR_ELEM* arr)
{
if (!arr)
exit(-1);
for (size_t i = 0; i < ARR_ELEM::last_index; i++) {
(*arr[i].free_funct)(arr[i].ptr);
}
free(arr);
arr = NULL;
}
////////// my_tab.h
#pragma once
typedef void(*PrintObj)(void* ptr);
typedef void(*FreeObj)(void* ptr);
struct ARR_ELEM {
void* ptr;
PrintObj print_funct;
FreeObj free_funct;
static size_t last_index;
};
ARR_ELEM* InitArray(size_t size);
void AddElem(ARR_ELEM* arr, ARR_ELEM* element);
void PrintArr(ARR_ELEM* arr);
void FreeArr(ARR_ELEM* arr);#pragma once
typedef void(*PrintObj)(void* ptr);
typedef void(*FreeObj)(void* ptr);
struct ARR_ELEM {
void* ptr;
PrintObj print_funct;
FreeObj free_funct;
static size_t last_index;
};
ARR_ELEM* InitArray(size_t size);
void AddElem(ARR_ELEM* arr, ARR_ELEM* element);
void PrintArr(ARR_ELEM* arr);
void FreeArr(ARR_ELEM* arr);
Paste Hosted With By Wklejamy.pl