Viewing the padded bytes

K

karthikbalaguru

Hi ,

I have the below structure.
struct check_struct{
double a;
char b;
};
My system reports the size of the above structure as 16 bytes.

I understand that there is some padding at the end of the above
structure and hence the size gets calculated to 16 bytes.
But, how to view the data/info that are padded using a debugger
like gdb or visual c++ debugger ?

I used watch windows but, it did not show the padded data .
Any ideas ?

Thx in advans,
Karthik Balaguru
 
U

user923005

Hi ,

I have the below structure.
struct check_struct{
        double a;
        char b;};

My system reports the size of the above structure as 16 bytes.

I understand that there is some padding at the end of the above
structure and hence the size gets calculated to 16 bytes.
But, how to view the data/info that are padded using a debugger
like gdb or visual c++ debugger ?

I used watch windows but, it did not show the padded data .
Any ideas ?

typedef struct check_struct {
double a;
char b;
} check_struct_type;

check_struct_type cs = {0};
unsigned char *csp = (unsigned char *) &cs;

Why do you care? You cannot depend on anything about the padding
bytes.
They might contain all zeros on one invocation and 0xDEADBEE on
another.
 
D

David Webber

I have the below structure.
struct check_struct{
double a;
char b;
};
My system reports the size of the above structure as 16 bytes.

I understand that there is some padding at the end of the above
structure and hence the size gets calculated to 16 bytes.
But, how to view the data/info that are padded using a debugger
like gdb or visual c++ debugger ?

I used watch windows but, it did not show the padded data .
Any ideas ?

I don't like having structures a different size from what I see on the page.
It makes me nervous. So I would always write the above as

struct check_struct
{
double a;
char b;
char reserved[7];
};

If you do that you can see the padded bytes.

Dave
--
David Webber
Author of 'Mozart the Music Processor'
http://www.mozart.co.uk
For discussion/support see
http://www.mozart.co.uk/mozartists/mailinglist.htm
 
R

Richard

David Webber said:
I have the below structure.
struct check_struct{
double a;
char b;
};
My system reports the size of the above structure as 16 bytes.

I understand that there is some padding at the end of the above
structure and hence the size gets calculated to 16 bytes.
But, how to view the data/info that are padded using a debugger
like gdb or visual c++ debugger ?

I used watch windows but, it did not show the padded data .
Any ideas ?

I don't like having structures a different size from what I see on the
page. It makes me nervous. So I would always write the above as

struct check_struct
{
double a;
char b;
char reserved[7];
};

If you do that you can see the padded bytes.

Dave

Assuming things it shouldn't - I dont like that at all.

Those bytes are not reserved either - they are padding. Two totally
different things I think.

To the OP. Use the debugger and examine the memory at the address of the
struct. e.g in gdb to see 8 hex words

(gdb)x/8x &myStruct
 
G

Guest

I don't like having structures a different size from what I see on the
page. It makes me nervous.   So I would always write the above as
struct check_struct
{
   double a;
   char b;
   char reserved[7];
};
If you do that you can see the padded bytes.

Assuming things it shouldn't - I dont like that at all.

Those bytes are not reserved either - they are padding. Two totally
different things I think.

To the OP. Use the debugger and examine the memory at the address of the
struct. e.g in gdb to see 8 hex words

(gdb)x/8x &myStruct

oh sugar. ok *now* I've read what it actually says. sorry
 
G

Guest

why do you need to see the padding bytes?
I don't like having structures a different size from what I see on the page.
It makes me nervous.  

stop programming in C then.

So I would always write the above as

struct check_struct
{
    double a;
    char b;
    char reserved[7];

};

If you do that you can see the padded bytes.

what happens when they change?
 
S

swengineer001

I have the below structure.
struct check_struct{
double a;
char b;
};
My system reports the size of the above structure as 16 bytes.
I understand that there is some padding at the end of the above
structure and hence the size gets calculated to 16 bytes.
But, how to view the data/info that are padded using a debugger
like gdb or visual c++ debugger ?
I used watch windows but, it did not show the padded data .
Any ideas ?

I don't like having structures a different size from what I see on the page.
It makes me nervous.   So I would always write the above as

struct check_struct
{
    double a;
    char b;
    char reserved[7];

};

If you do that you can see the padded bytes.

I don't think so. It will always show you the reserved member's
contents but I think the implementation is still free to insert
padding in any amount and at any location in this structure it
pleases, except at the beggining.
 
R

Richard

I thought you were going to fix your misleading sig?

I thought you were going to stop being an idiot and read it. Or are you
going to do a Heathfield and start demanding apologies for other people?
 
R

Richard

David Webber said:
I have the below structure.
struct check_struct{
double a;
char b;
};
My system reports the size of the above structure as 16 bytes.
I understand that there is some padding at the end of the above
structure and hence the size gets calculated to 16 bytes.
But, how to view the data/info that are padded using a debugger
like gdb or visual c++ debugger ?
I used watch windows but, it did not show the padded data .
Any ideas ?
I don't like having structures a different size from what I see on the
page. It makes me nervous.   So I would always write the above as
struct check_struct
{
   double a;
   char b;
   char reserved[7];
};
If you do that you can see the padded bytes.

Assuming things it shouldn't - I dont like that at all.

Those bytes are not reserved either - they are padding. Two totally
different things I think.

To the OP. Use the debugger and examine the memory at the address of the
struct. e.g in gdb to see 8 hex words

(gdb)x/8x &myStruct

oh sugar. ok *now* I've read what it actually says. sorry

heh :-; No harm. But I did reply before I saw your acknowledgement.
 
R

Richard Tobin

David Webber said:
I don't like having structures a different size from what I see on the page.
It makes me nervous. So I would always write the above as

struct check_struct
{
double a;
char b;
char reserved[7];
};

Are you sure every compiler uses 7 bytes of padding?

-- Richard
 
D

David Schwartz

Hi ,

I have the below structure.
struct check_struct{
        double a;
        char b;};

My system reports the size of the above structure as 16 bytes.
I understand that there is some padding at the end of the above
structure and hence the size gets calculated to 16 bytes.

How do you know it's padding. Maybe a 'double' is 15 bytes on your
platform.
But, how to view the data/info that are padded using a debugger
like gdb or visual c++ debugger ?

I used watch windows but, it did not show the padded data .
Any ideas ?

You are programming in C, there is no reason you should have to know
or care.

Unless you are doing something very unusual, you are barking up the
wrong tree.

An example of a case where you could reasonably care would be one
where your memory allocator specifically sets assigned memory to a
particular value and freed memory to some other value. By seeing the
bytes that would presumably only be touched by the allocator, you can
tell if the structure was freed.

In any event, I've never heard of a debugger that doesn't let you view
any chunk of memory you want to view.

DS
 
U

user923005

David Webber said:
I don't like having structures a different size from what I see on the page.
It makes me nervous.   So I would always write the above as
struct check_struct
{
   double a;
   char b;
   char reserved[7];
};

Are you sure every compiler uses 7 bytes of padding?

One of mine uses 0, 2, 4, 8, or 16 bytes, depending on what I ask for.
 
D

David Webber

stop programming in C then.

Why? [Actually I did, many years ago - I do it in C++ these days.]

So I would always write the above as

struct check_struct
{
double a;
char b;
char reserved[7];

};

If you do that you can see the padded bytes.
what happens when they change?

Should I care? Most importantly it reminds me, when I want to make the
structure bigger, not to write

struct check_struct
{
double a;
char b;
double c;
};

which is truly hideous. [And anyone who does program in C or C++ should be
close enough to what's going on underneath *never* to write that.]

Dave
--
David Webber
Author of 'Mozart the Music Processor'
http://www.mozart.co.uk
For discussion/support see
http://www.mozart.co.uk/mozartists/mailinglist.htm
 
D

David Webber

struct check_struct
{
double a;
char b;
char reserved[7];

};

If you do that you can see the padded bytes.
I don't think so. It will always show you the reserved member's
contents but I think the implementation is still free to insert
padding in any amount and at any location in this structure it
pleases, except at the beggining.<

I dare say, in principle, but I use compiler options which specify the
padding properties, and it would be unusual for 8 byte boundaries not to be
safe.

[I have large arrays of structures with many bit fields, and it is in my
interest, to arrange them so that padding is unnecessary, and so I control
very carefully where the bit fields fit in relation to byte and word
boundaries. It's a useful principle of "defensive programming".]

Dave
--
David Webber
Author of 'Mozart the Music Processor'
http://www.mozart.co.uk
For discussion/support see
http://www.mozart.co.uk/mozartists/mailinglist.htm
 
D

David Webber

Richard Tobin said:
David Webber said:
I don't like having structures a different size from what I see on the
page.
It makes me nervous. So I would always write the above as

struct check_struct
{
double a;
char b;
char reserved[7];
};

Are you sure every compiler uses 7 bytes of padding?

When I said "always", it was an example. :) I use compiler options to
specify such options. [I don't know that *all* compilers allow this, but
the ones I have tried all do IIRC.] So in some cases I'd probably get away
with:

struct check_struct
{
double a;
char b;
char reserved;
};

(with a 32 bit compiler).

Dave
--
David Webber
Author of 'Mozart the Music Processor'
http://www.mozart.co.uk
For discussion/support see
http://www.mozart.co.uk/mozartists/mailinglist.htm
 
D

David Webber

Indeed. That's an assumption that shouldn't be made.
True.

You don't know what kind of optimisations you might get. Best approach for
struct padding is to ignore it and let the compiler optimise as it sees
fit.

Definitely not. The best approach is to make life easy for the compiler
and check that you are doing it optimally. It isn't hard and you end up
with much more efficient code.

Dave
--
David Webber
Author of 'Mozart the Music Processor'
http://www.mozart.co.uk
For discussion/support see
http://www.mozart.co.uk/mozartists/mailinglist.htm
 
D

David Webber

Richard Tobin said:
David Webber said:
I don't like having structures a different size from what I see on the
page.
It makes me nervous. So I would always write the above as

struct check_struct
{
double a;
char b;
char reserved[7];
};

Are you sure every compiler uses 7 bytes of padding?

When I said "always", it was an example. :) The OP said he got 16
bytes, so his compiler is adding 7: I just made them explicit.

I use compiler options to specify such options, and usually set it to 4-byte
boundaries for 32 bit machines. [I don't know that *all* compilers allow
this, but
the ones I have tried all do IIRC.] With appropriate choices one could
probably get away
with:

struct check_struct
{
double a;
char b;
char reserved;
};

(with a 32 bit compiler).

Dave
--
David Webber
Author of 'Mozart the Music Processor'
http://www.mozart.co.uk
For discussion/support see
http://www.mozart.co.uk/mozartists/mailinglist.htm
 
C

cc

why do you need to see the padding bytes?
I don't like having structures a different size from what I see on the page.
It makes me nervous.  

stop programming in C then.
So I would always write the above as
struct check_struct
{
    double a;
    char b;
    char reserved[7];

If you do that you can see the padded bytes.

what happens when they change?

Why don't you retards keep it in clc?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top