0

I know there are a lot of questions surrounding this topic. Read 'em, still having trouble. The common suggestion here for declaring an array of RGBTriples is something like the following:

RGBTRIPLE name[bi.biWidth];

(Just to be clear, in my implementation bi.biWidth contains the resized number of pixels, not the infile's number of pixels).

However, when I take the program through GDB, step by step, that initialization of the array just returns a pointer / address.

To make it a little more concrete, I am testing resize on small.bmp, using a resize factor of 4, so bi.biWidth is 12 (3 pixels originally, scaled up by a factor 4). If I go through GDB and initialize the name array of RGBTRIPLEs, then print out the name array, I get the value 0xbfffef80. If I print out name dereferenced, I just get a lone RGBTRIPLE. And if I print out bi.biWidth locally, it gives me the integer 12 as expected.

Yet if I initialize the array with a hard-coded value of 12 inside the brackets, the name array acts exactly as I would have expected. It gives me a 36 byte array with 12 RGBTRIPLEs in it when I print it.

I've spent several hours just trying to get this piece right and googling all manner of array initialization tutorials. Some say that you can initialize an array with a variable number of elements in C, others say you can't. Anyone have something to unstick me?

(I can post code if it helps, but in this case didn't want to get distracted by other potential errors in the code. This is much more of a conceptual question about initializing arrays of structs when using a variable for the number of elements in the array).

2 Answers 2

0

I use this in the appliance, so not sure why your having trouble.

/// pixel array for storing the new lines

RGBTRIPLE line[bi.biWidth * scale];

later I use this

// copy pixels to pixel array scale number of times

line[pixelcount] = triple;

0

You're creating an array of RGBTRIPLEs. In your example, you have figured out that it should include 12 pixels. Then you say that you end up with a 36 byte array and this surprises you.

Initially, I couldn't figure out what problem you were trying to resolve, but then a thought occurred to me.

Is it possible that you have overlooked or are unaware that a single RGBTRIPLE is 3 bytes long? That's why an array with 12 RGBTRIPLE is 36 bytes.

If this answers your question, please accept this answer to remove the question from the unanswered question pool. Let's keep up on forum housekeeping. ;-)

5
  • No that doesn't answer it. I would expect 36 bytes for each scanline that has 12 pixels. The issue is that the array initialization RGBTRIPLE name[bi.biWidth]; does not return an array of RGBTRIPLEs at all. It returns a hexadecimal number. Commented Jul 31, 2015 at 21:18
  • ok, it's going to take concrete examples of the problem. Can yo post the relevant code?
    – Cliff B
    Commented Jul 31, 2015 at 21:21
  • name[?] will be a three byte instance of RGBTRIPLE while name will be a pointer to a the first place in memory used by name 0xbfffef80
    – ebobtron
    Commented Jul 31, 2015 at 21:25
  • good catch by ebob. That's why I wanted to see some code, to see exactly what you were asking for. If you're talking about output from gdb, then I'd still want to see what you were asking or seeing.
    – Cliff B
    Commented Jul 31, 2015 at 21:30
  • I'll get back to you on this one - short story is that I debugged elsewhere and fixed the problem, but I'm still scratching my head about why this didn't cause a bug. I'll follow up soon with my code and snapshots from GDB to help clarify. Commented Jul 31, 2015 at 21:48

You must log in to answer this question.

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