#structs memory allocation

29 messages · Page 1 of 1 (latest)

fervent locust
#

Could someone explain to me what is happening here and why ?

bronze flumeBOT
#

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.

fervent locust
#

    // 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

fervent locust
#

@native cairn @safe dome could you guys help me out ?

native cairn
#

👋

bronze flumeBOT
#
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);
  }
MK61
safe dome
#

Don't ping people unless they are already part of the thread

fervent locust
#

sorry

#

didn´t know that..won´t do that again

native cairn
#

What exactly is the question?

native cairn
fervent locust
#

But if I don´t use that, I get an error, which says that I am working with a NULL pointer

fervent locust
native cairn
#

It checks if p_tBmp->pixel != NULL and then set some bmp properties / the first pixel

fervent locust
#

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

native cairn
#

where did you allocate memory for p_tBmp->pFile?

fervent locust
#

😅 POGGERS 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

native cairn
#

There's a million ways to do everything

fervent locust
#

thank you for your time