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: sizeof nonstatic member - comp.lang.c++.moderatedHow do you get at the size of a nonstatic member of a class without having a reference to an actual object? Say I have a class struct A { int a1;... sizeof implementation - comp.lang.c> cases > 1. int i=0; sizeof(i)=4 ...i.e size of variavles > 2. sizeof(int)=4 ...i.e size of datatypes > 3. struct t > { > int a ... extern typedef struct - comp.lang.c... include <stdlib.h> struct object_t { int id; char * name; }; object new_object(void) { object this; this = calloc(1, sizeof(struct object ... struct/union difference - comp.lang.c++.moderated> > // Padding issues mess this up without anon struct > memcpy(&v, &foo.b, sizeof(bar)); I can't figure out how anonymous structs can possibly affect alignment, please explain. ioctl(sockfd, SIOCGIFCONF, &ifc) - comp.unix.programmer... len; ) { >> ifr = (struct ifreq *) ptr; >> >> if (ifr->ifr_addr.sa_len > sizeof(struct sockaddr)) >> len = ifr->ifr_addr.sa_len; >> else >> len = sizeof(struct ... "TYPEDEF PTR with STRUCT" masm32 - comp.lang.asm.x86My problem: I have a struct and a function with a pointer to the struct as parameter. ... makes me wonder whether ... include <stdio.h> typedef char static_assert[ sizeof ... Performance: struct vs. cell array - comp.soft-sys.matlab ...M, ... % number of repetitions N, ... % size of cell array and struct L ... % average size of data matrix ) for m = 1:M ... can not received broadcast message in linux 7.1 - comp.unix ...... if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) { printf("server: can't bind local address"); } socklen = sizeof(struct ... size of a derived type containing pointers... - comp.lang.fortran ...I believe that means that the padding is there > even for a scalar, as you need to be able to memcpy() a > scalar struct based on its sizeof. read() error - Bad Address - comp.unix.programmer... pthread_self()); > int clientfd,temp; > char *mesg; > struct sockaddr_in client; > sock_arg *func_arg = (sock_arg *)arg; > mesg = (char *)malloc(100 * sizeof ... sizeof - Wikipedia, the free encyclopediaThus, in this case the sizeof operator returns the size of the structure, including any padding, but without any storage allowed for the array. DJGPP FAQ -- What should sizeof (struct xyzzy) return?Node:Struct size, Next:Struct packing, Previous:0xfe+0x20, Up:Miscellany 22.11 What should sizeof (struct xyzzy) return? Q: When I call sizeof on a struct, I ... 7/17/2012 12:44:33 PM
|