0

Sort of related to PSET4 resize, but actually want to make sure I understand this in general.

So at first I just declared a struct BITMAPINFOHEADER called "new_bi" and dealt with it while avoiding pointers. I originally didn't alloc for the struct, just declared it. It worked, and from what I understand, it worked because it was created in the stack? Am I correct? Also what I created and dealt with is the actual struct called "new_bi".

original code example

// update outfile BITMAPINFOHEADER
BITMAPINFOHEADER new_bi;

new_bi.biSize = bi.biSize;
new_bi.biWidth = n * bi.biWidth;
new_bi.biHeight = n * bi.biHeight;
...

I tried to redo it properly, by using malloc. Since I was confused about how to implement this and figured it out through a bit of trial and error, I am writing this mainly for validation that I am understanding this correctly and didn't just get lucky and it happened to work when it shouldn't.

here is the rewritten code

// update outfile BITMAPINFOHEADER
BITMAPINFOHEADER* ptr_new_bi = malloc(sizeof(BITMAPINFOHEADER));
if(ptr_new_bi == NULL)
{
    printf("Error -- out of memory\n");
    return 1;
}

ptr_new_bi->biSize = bi.biSize;
ptr_new_bi->biWidth = n * bi.biWidth;
ptr_new_bi->biHeight = n * bi.biHeight;
...

So malloc returns a pointer to what you want to allocate memory for, in this case a pointer to a new struct BITMAPINFOHEADER. "ptr_new_bi" in this case, is the name of the pointer, and not the name of the actual struct like in the previous example. I am confused about how this works. Since I am only dealing with the pointer, does the actual struct remain unnamed? I never declared a struct called anything, only the pointer. So how can a struct exist without a name? If I wanted to name the actual struct and tie the pointer to it, how would I do this? can I add in:

&new_bi = ptr_new_bi;

Since &new_bi is the address of new_bi, will this name the struct "new_bi" that ptr_new_bi is pointing to. And after this can I give values to the elements in the struct like I did in the first example by just using equal sign:

new_bi.biSize = bi.biSize;

Instead of what I did in the second code, I give values to the different elements in the struct by dereferencing the pointer to the element in the struct by using "->" (instead of prefixing with a * like you would with a normal int or char.)

So along with the couple of questions I asked, I mainly just want to make sure I am understanding all this correctly. Thanks!

1 Answer 1

2

I originally didn't alloc for the struct, just declared it. It worked, and from what I understand, it worked because it was created in the stack? Am I correct?

the type BITMAPINFOHEADER is not a pointer type. it is similar to other native types (e.g., int, char, etc) except that it is a little more complex.

when declaring a variable of that type, you don't need to allocate memory manually for it just like you wouldn't need to allocate memory manually when creating an int or a char variable.

variables that we either allocate manually for them or make them point to some block of already allocated memory are variables of pointer types. a pointer type has an * in it such that type * where type is the name of the type (e.g., int * or BITMAPINFOHEADER *).

regarding the question that asks whether the memory for the variable new_bi is on the stack, in this case, yes because new_bi is a local variable and local variables get their memory allocated/freed automatically on the stack.

"ptr_new_bi" in this case, is the name of the pointer, and not the name of the actual struct like in the previous example.

well, in the first case, new_bi wasn't actually the name of the struct. rather, it was the name of a variable of the struct type. you could think of it as the name of the block of memory allocated for the struct.

the name of the struct is BITMAPINFOHEADER (although even this is just a synonym to the real name of the struct which is struct tagBITMAPINFOHEADER. see BITMAPINFOHEADER structure).

when the following line is executed

BITMAPINFOHEADER* ptr_new_bi = malloc(sizeof(BITMAPINFOHEADER));

malloc tries to allocate a block of memory that is the size of the struct. if it succeeds, it returns a pointer to that block of memory. ptr_new_bi is the name of that pointer. the block of memory itself doesn't have a name unlike the previous case.

you could easily give it a name though and deal with it using that name through something like

BITMAPINFOHEADER myInfoHeader = *ptr_new_bi; 
// now the block's name is myInfoHeader

If I wanted to name the actual struct and tie the pointer to it, how would I do this? can I add in:

&new_bi = ptr_new_bi;

not really! the expression &new_bi is not assignable. you should get an error, if you try to do this. the correct way to make ptr_new_bi point to new_bi is as follows:

BITMAPINFOHEADER new_bi;
BITMAPINFOHEADER *ptr_new_bi = &new_bi;

no need to allocate memory for the pointer in this case of course because the memory was allocated for us automatically when we created new_bi.

1
  • Thanks for the response! Commented Dec 28, 2015 at 7:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .