Hmm er lidt bekymret over at hukommelsen ikke bliver frigjort.
Har en macro:
#define __FREE_NOT_NULL(variable) if (variable != NULL) free(variable);
bmp_file er en struktur med allokerede pointere:
struct bmp_file {
struct bmp_magic* magic;
struct bmp_header* bmpHeader;
struct dib_header* dibHeader;
uint8_t* data;
};
Jeg frigører sådan en struktur med:
void bmp_freeFile(struct bmp_file* file) {
__FREE_NOT_NULL(file->magic);
__FREE_NOT_NULL(file->bmpHeader);
__FREE_NOT_NULL(file->dibHeader);
__FREE_NOT_NULL(file->data);
__FREE_NOT_NULL(file);
};
Jeg har denne her main hvor jeg laver 5000 af disse strukturer:
int main(int argc, char** argv) {
int n = 5000;
int i=0;
struct bmp_file* bmpfiles[n];
char dummy;
FILE* pfile = fopen("C:\\test.bmp", "r");
printf("Before allocating\n");
scanf("%c", &dummy);
for(i=0; i<n; i++) {
rewind(pfile);
bmpfiles[i] = bmp_readFile(pfile);
};
fclose(pfile); pfile = NULL;
printf("Before freeing\n");
scanf("%c", &dummy);
for (i=0; i<n; i++) {
bmp_freeFile(bmpfiles[i]);
bmpfiles[i] = NULL;
};
printf("After freeing\n");
printf("Press enter to exit!\n");
scanf("%c", &dummy);
return 0;
};
Før den har allokeret alle
n strukturer bruger programmet:
Ved n=100
Start: 348KB
Efter allokering: 1256KB
Efter frigørelse: 1256KB
Ved n=500
Start: 348KB
Efter allokering: 4104KB
Efter frigørelse: 2460KB
Ved n=1000
Start: 352KB
Efter allokering: 7704KB
Efter frigørelse: 2484KB
Ved n=5000
Start: 372KB
Efter allokering: 35576KB
Efter frigørelse: 4412KB
Hvorfor kan det være at den svinger sådan?? Og alle tallene kan svinge med et par kilobytes forskellig ved hver kørsel?
Her er hele koden hvis den ønskes set:
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#define __FREE_NOT_NULL(variable) if (variable != NULL) free(variable);
struct bmp_magic {
unsigned char magic[2];
};
struct bmp_header {
uint32_t file_size;
uint16_t reserved1;
uint16_t reserved2;
uint32_t offset;
};
struct dib_header {
uint32_t header_size;
uint32_t width;
uint32_t height;
uint16_t color_planes;
uint16_t bpp;
uint32_t compress_type;
uint32_t image_size;
uint32_t hres;
uint32_t vres;
uint32_t colors;
uint32_t important_colors;
};
struct bmp_file {
struct bmp_magic* magic;
struct bmp_header* bmpHeader;
struct dib_header* dibHeader;
uint8_t* data;
};
struct bmp_magic* bmp_readMagic(FILE* pfile) {
struct bmp_magic* pMagic = calloc(1, sizeof(struct bmp_magic));
if (pMagic == NULL) {
fprintf(stderr, "Error allocating memory!\n");
}
else
{
fread(pMagic->magic, sizeof(unsigned char), 2, pfile);
};
return pMagic;
};
struct bmp_header* bmp_readBmpHeader(FILE* pfile) {
struct bmp_header* pHeader = calloc(1, sizeof(struct bmp_header));
if (pHeader == NULL) {
fprintf(stderr, "Error allocation memory!\n");
}
else
{
size_t word = sizeof(uint16_t);
size_t dword = sizeof(uint32_t);
fread(&pHeader->file_size, dword, 1, pfile);
fread(&pHeader->reserved1, word, 1, pfile);
fread(&pHeader->reserved2, word, 1, pfile);
fread(&pHeader->offset, dword, 1, pfile);
};
return pHeader;
};
struct dib_header* bmp_readDibHeader(FILE* pfile) {
struct dib_header* pHeader = calloc(1, sizeof(struct dib_header));
if (pHeader == NULL) {
fprintf(stderr, "Error allocating memory!\n");
}
else
{
size_t word = sizeof(uint16_t);
size_t dword = sizeof(uint32_t);
fread(&pHeader->header_size, dword, 1, pfile);
fread(&pHeader->width, dword, 1, pfile);
fread(&pHeader->height, dword, 1, pfile);
fread(&pHeader->color_planes, word, 1, pfile);
fread(&pHeader->bpp, word, 1, pfile);
fread(&pHeader->compress_type, dword, 1, pfile);
fread(&pHeader->image_size, dword, 1, pfile);
fread(&pHeader->hres, dword, 1, pfile);
fread(&pHeader->vres, dword, 1, pfile);
fread(&pHeader->colors, dword, 1, pfile);
fread(&pHeader->important_colors, dword, 1, pfile);
};
return pHeader;
};
struct bmp_file* bmp_readFile(FILE* pfile) {
uint8_t* data;
size_t dataSize;
struct bmp_file* file = calloc(1, sizeof(struct bmp_file));
if (file == NULL) {
fprintf(stderr, "Error allocating memory!\n");
}
else
{
file->magic = bmp_readMagic(pfile);
file->bmpHeader = bmp_readBmpHeader(pfile);
file->dibHeader = bmp_readDibHeader(pfile);
dataSize = file->dibHeader->image_size*sizeof(uint8_t);
data = calloc(1, dataSize);
fread(data, dataSize, 1, pfile);
file->data = data;
};
return file;
};
void bmp_freeFile(struct bmp_file* file) {
__FREE_NOT_NULL(file->magic);
__FREE_NOT_NULL(file->bmpHeader);
__FREE_NOT_NULL(file->dibHeader);
__FREE_NOT_NULL(file->data);
__FREE_NOT_NULL(file);
};
int main(int argc, char** argv) {
int n = 5000;
int i=0;
struct bmp_file* bmpfiles[n];
char dummy;
FILE* pfile = fopen("C:\\test.bmp", "r");
printf("Before allocating\n");
scanf("%c", &dummy);
for(i=0; i<n; i++) {
rewind(pfile);
bmpfiles[i] = bmp_readFile(pfile);
};
fclose(pfile); pfile = NULL;
printf("Before freeing\n");
scanf("%c", &dummy);
for (i=0; i<n; i++) {
bmp_freeFile(bmpfiles[i]);
bmpfiles[i] = NULL;
};
printf("After freeing\n");
printf("Press enter to exit!\n");
scanf("%c", &dummy);
return 0;
};
Indlæg senest redigeret d. 12.06.2010 17:47 af Bruger #14645