about struct and sizeof()

  • Follow


Hi,
I'm having problem with this struct:


typedef struct tagMyRGB {
                 BYTE    rgbBlue;
                 BYTE    rgbGreen;
                 BYTE    rgbRed;
                 BYTE    rgbReserved;
} myRGB;

typedef myRGB FAR* LPMyRGB;

while a conversion from TIFF to another file format below statement is
used:

someVar += (NumberOfColors-1) * sizeof(myRGB);


this line is placed inside a function that should return false or true
and it looks like it doesn't let the function return any value. When I
replace sizeof(myRGB) with "1" then it goes OK but when I use it
totallyl acts weird!

any suggestions?
Thanks,


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply ramsin.savra (18) 3/16/2005 8:54:24 AM

In article <1110918552.493961.7630@f14g2000cwb.googlegroups.com>, joel
<ramsin.savra@gmail.com> writes
>Hi,
>I'm having problem with this struct:
>
>
>typedef struct tagMyRGB {
>                 BYTE    rgbBlue;
>                 BYTE    rgbGreen;
>                 BYTE    rgbRed;
>                 BYTE    rgbReserved;
>} myRGB;

You seem to be writing C rather than C++. C++ does not have any concept
of a tagname and a name declared for a struct/class/union/enum *is* the
typename.

>
>typedef myRGB FAR* LPMyRGB;

I am having difficulty parsing this line. I suspect that FAR is a
pre-processor ID covering something that is an implementation specific
extension.

>
>while a conversion from TIFF to another file format below statement is
>used:
>
>someVar += (NumberOfColors-1) * sizeof(myRGB);
>
>
>this line is placed inside a function that should return false or true
>and it looks like it doesn't let the function return any value. When I
>replace sizeof(myRGB) with "1" then it goes OK

But what happens when you replace it by 4, which is more likely to be
the actual value of sizeof(myRGB) ?

> but when I use it
>totallyl acts weird!

Without seeing the function (stripped down to the smallest version that
exhibits the behaviour) how could we know?


--
Francis Glassborow      ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply Francis 3/17/2005 2:47:33 AM


joel wrote:

> typedef struct tagMyRGB {
>                  BYTE    rgbBlue;
>                  BYTE    rgbGreen;
>                  BYTE    rgbRed;
>                  BYTE    rgbReserved;
> } myRGB;
>
> typedef myRGB FAR* LPMyRGB;
:
>
> someVar += (NumberOfColors-1) * sizeof(myRGB);

If someVar is a pointer (i.e., type LPMyhRGB)
then you do NOT want to multiply by sizeof (myRGB).
Pointers already handle the size of what their pointing
to when you add to them.

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply Ron 3/17/2005 8:10:09 AM

joel wrote:
> any suggestions?

"BYTE" and "FAR" are not in C++, you haven't shown
us any definitions for someVar or NumberOfColors,
and the phrase "it doesn't let the function return
any value" makes no sense.

Normally I would suggest that you should boil down
your code to a small example which still exhibits the
problem and post that, but I suspect I should suggest
that you study the language more thoroughly.

If someVar is an LPMyRGB, then adding N to it actually
does the scaling by sizeof(myRGB). If you're trying to
skip past a color table, it seems that you should say
      someVar += NumberOfColors;
Maybe that's why you get the "acts weird" when you add
NumberOfColors - 1.

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply Hyman 3/17/2005 8:13:30 AM

joel wrote:
> Hi,
> I'm having problem with this struct:
>
>
> typedef struct tagMyRGB {
>                  BYTE    rgbBlue;
>                  BYTE    rgbGreen;
>                  BYTE    rgbRed;
>                  BYTE    rgbReserved;
> } myRGB;
>
> typedef myRGB FAR* LPMyRGB;

I assume you didn't write this definition yourself?  It's not really
good C++ style.

> while a conversion from TIFF to another file format below statement is
> used:
>
> someVar += (NumberOfColors-1) * sizeof(myRGB);
>
>
> this line is placed inside a function that should return false or true
> and it looks like it doesn't let the function return any value. When I
> replace sizeof(myRGB) with "1" then it goes OK but when I use it
> totallyl acts weird!

I suspect that the multiplication by sizeof(myRGB) is incorrect;
read on.

You also wrote:

<snip>
> However, for some tiff files it stops in the middle below loop:
>
> 1  for (int i = 0; i < numColors; ++i, pPal += offset) {
> 2       pPal->rgbRed   = pPal->rgbGreen = pPal->rgbBlue  =
>                         (BYTE)(i * step);
> 3       pPal->rgbReserved = 0;
> }
>
> It is only balck and white so numColors = 2
> pPal is a pointer to a struct as:
>
> typedef struct tagRGBQ {
<snip>

Would I be right in guessing that the value of offset is not 1?  If so
then I think you have misunderstood the way pointer arithmetic works
in C++ (and C).

Given a pointer, p, to element i of an array, and j, an integer, p + j
points to element i + j of the array, if such an element exists, or
just off the end of the array if its size is i + j.  (Otherwise it is
undefined, so be careful.)  This works for elements of any size.  p[j]
is defined to be the same as p + j (as are j + p and j[p]).  There is
no need to perform multiplication by the size of an element.

-- 
Ben Hutchings
Having problems with C++ templates?  Your questions may be answered by
<http://womble.decadentplace.org.uk/c++/template-faq.html>.

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply Ben 3/17/2005 8:15:51 AM

joel wrote:
> Hi,
> I'm having problem with this struct:
>
>
> typedef struct tagMyRGB {
>                 BYTE    rgbBlue;
>                 BYTE    rgbGreen;
>                 BYTE    rgbRed;
>                 BYTE    rgbReserved;
> } myRGB;
>
> typedef myRGB FAR* LPMyRGB;
>
> while a conversion from TIFF to another file format below statement is
> used:
>
> someVar += (NumberOfColors-1) * sizeof(myRGB);
>
>
> this line is placed inside a function that should return false or true
> and it looks like it doesn't let the function return any value. When I
> replace sizeof(myRGB) with "1" then it goes OK but when I use it
> totallyl acts weird!
>
> any suggestions?

Can you define "acts weird" a bit better?

I would have thought that it would be better to replace sizeof(myRGB) with
4, if you absolutely want to, since the size of the struct is four bytes
(provided that BYTE is define to be a byte, of course).

It's impossible to say whether the function returns a value or not without
actually seeing it.  Can you post it?

Orjan
--
Get your Tale paperback or CD here:
http://tale.cunobaros.com
Or just read it there, if you don't want the illustrations



      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply Orjan 3/17/2005 12:28:10 PM

"Francis Glassborow" <francis@robinton.demon.co.uk> wrote in message
news:Vb3qoyLGl$NCFwIE@robinton.demon.co.uk...
> In article <1110918552.493961.7630@f14g2000cwb.googlegroups.com>, joel
> <ramsin.savra@gmail.com> writes
>>Hi,
>>I'm having problem with this struct:
>>
>>
>>typedef struct tagMyRGB {
>>                 BYTE    rgbBlue;
>>                 BYTE    rgbGreen;
>>                 BYTE    rgbRed;
>>                 BYTE    rgbReserved;
>>} myRGB;
>
> You seem to be writing C rather than C++. C++ does not have any concept
> of a tagname and a name declared for a struct/class/union/enum *is* the
> typename.

C++ understands the C syntax, but a C++ programmer would write:

struct myRGB {...};

>>typedef myRGB FAR* LPMyRGB;
>
> I am having difficulty parsing this line. I suspect that FAR is a
> pre-processor ID covering something that is an implementation specific
> extension.

The idea of far pointers is related to the 80x86 segmented memory
models.  A near pointer is 16 bits, a far pointer is 32 bits.  DOS compilers
have keywords for this.

>>while a conversion from TIFF to another file format below statement is
>>used:
>>
>>someVar += (NumberOfColors-1) * sizeof(myRGB);
>>
>>
>>this line is placed inside a function that should return false or true
>>and it looks like it doesn't let the function return any value. When I
>>replace sizeof(myRGB) with "1" then it goes OK

If you have a variable that's incremented by the number of byte fields in
a struct including a padding byte, I'd hope that variable somehow
represents the size of something in bytes.  Since incrementing the variable
by NumberOfColors-1 causes the program to behave correctly, I'd
guess someVar is meant to count colors, not bytes.

> But what happens when you replace it by 4, which is more likely to be
> the actual value of sizeof(myRGB) ?

I'm pretty sure this compiler will say it's 4 bytes long.

>> but when I use it
>>totallyl acts weird!
>
> Without seeing the function (stripped down to the smallest version that
> exhibits the behaviour) how could we know?

But then he'd have to find the definition of BYTE, someVar,
NumberOfColors, etc.  Well, in lieu of that, could you tell us what
someVar was initialized to before the line

someVar += ...

?


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Mike 3/17/2005 1:06:42 PM

6 Replies
167 Views

(page loaded in 0.146 seconds)

Similiar Articles:













7/17/2012 12:44:33 PM


Reply: