#structs memory allocation
29 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.
// Von Mattis
int extra_bytes = ((width * 3) % 4) ? (4 - ((width * 3) % 4)) : 0; // variable for extra bytes
// 3.2 Wichtigster Teil - Memory allocation
// 3.2.1 Speicher für die Struktur
tBmp* p_tBmp = (tBmp*)calloc(4, sizeof(tBmp));
if (p_tBmp == NULL) {
printf("Speicherzuweisung in der Datei %s in Zeile %d ist fehlgeschlagen!", __FILE__, __LINE__);
exit(EXIT_FAILURE);
}
// 3.2.2 Speicher für die Daten der Bitmap
p_tBmp->data = (char*)calloc(4, sizeof(tFileHeader) + sizeof(tInfoHeader) + (3 * width + extra_bytes) * height * sizeof(BYTE));
if (p_tBmp->data == NULL) {
printf("Speicherzuweisung in der Datei %s in Zeile %d ist fehlgeschlagen!", __FILE__, __LINE__);
exit(EXIT_FAILURE);
}
// 3.2.3 Speicher für die Pixel
p_tBmp->pixel = (tRgb**)calloc(width, sizeof(tRgb*));
if (p_tBmp->pixel == NULL) {
printf("Speicherzuweisung in der Datei %s in Zeile %d ist fehlgeschlagen!", __FILE__, __LINE__);
exit(EXIT_FAILURE);
}
if (p_tBmp->pixel != NULL){
p_tBmp->pFile = (tFileHeader*)p_tBmp->data;
p_tBmp->pInfo = (tInfoHeader*)(p_tBmp->data + sizeof(tFileHeader));
p_tBmp->pixel[0] = (tRgb*)(p_tBmp->data + sizeof(tFileHeader) + sizeof(tInfoHeader));
}
else{
printf("Speicherzuweisung in der Datei %s in Zeile %d ist fehlgeschlagen!", __FILE__, __LINE__);
exit(EXIT_FAILURE);
} ```
I don´t get this part:
p_tBmp->pFile = (tFileHeader*)p_tBmp->data;
p_tBmp->pInfo = (tInfoHeader*)(p_tBmp->data + sizeof(tFileHeader));
p_tBmp->pixel[0] = (tRgb*)(p_tBmp->data + sizeof(tFileHeader) + sizeof(tInfoHeader));
} ```
Without it, p_tBmp would be NULL pointer
@native cairn @safe dome could you guys help me out ?
👋
!f
tBmp* createBmp(int width, int height) {
// Von Mattis
int extra_bytes = ((width * 3) % 4) ? (4 - ((width * 3) % 4))
: 0; // variable for extra bytes
// 3.2 Wichtigster Teil - Memory allocation
// 3.2.1 Speicher für die Struktur
tBmp* p_tBmp = (tBmp*)calloc(4, sizeof(tBmp));
if (p_tBmp == NULL) {
printf("Speicherzuweisung in der Datei %s in Zeile %d ist fehlgeschlagen!",
__FILE__, __LINE__);
exit(EXIT_FAILURE);
}
// 3.2.2 Speicher für die Daten der Bitmap
p_tBmp->data =
(char*)calloc(4, sizeof(tFileHeader) + sizeof(tInfoHeader) +
(3 * width + extra_bytes) * height * sizeof(BYTE));
if (p_tBmp->data == NULL) {
printf("Speicherzuweisung in der Datei %s in Zeile %d ist fehlgeschlagen!",
__FILE__, __LINE__);
exit(EXIT_FAILURE);
}
// 3.2.3 Speicher für die Pixel
p_tBmp->pixel = (tRgb**)calloc(width, sizeof(tRgb*));
if (p_tBmp->pixel == NULL) {
printf("Speicherzuweisung in der Datei %s in Zeile %d ist fehlgeschlagen!",
__FILE__, __LINE__);
exit(EXIT_FAILURE);
}
if (p_tBmp->pixel != NULL) {
p_tBmp->pFile = (tFileHeader*)p_tBmp->data;
p_tBmp->pInfo = (tInfoHeader*)(p_tBmp->data + sizeof(tFileHeader));
p_tBmp->pixel[0] =
(tRgb*)(p_tBmp->data + sizeof(tFileHeader) + sizeof(tInfoHeader));
} else {
printf("Speicherzuweisung in der Datei %s in Zeile %d ist fehlgeschlagen!",
__FILE__, __LINE__);
exit(EXIT_FAILURE);
}
Don't ping people unless they are already part of the thread
What exactly is the question?
This doesn't change p_tBmp
But if I don´t use that, I get an error, which says that I am working with a NULL pointer
I wrote this part
And this I got from someone else, but I don´t really understand whats that part of code is doing
It checks if p_tBmp->pixel != NULL and then set some bmp properties / the first pixel
But didn´t I already allocate memory for it (above) p_tBmp->pFile = (tFileHeader*)p_tBmp->data; I mean like this part
I already allocated memory for p_tBmp->pFile
where did you allocate memory for p_tBmp->pFile?
😅
Oh okay, you are right
so this sets the pointers on memory for the first pixel
Is something like that always necessary or did you have use something similiar
Because I never saw allocatig memory at the and like this
thank you for your time
