about structures memory allocation

B

bharath539

how much memory is allocated for following structure
struct bharath
{
int b;
char c;
float d;
}

and how?
 
C

Clever Monkey

how much memory is allocated for following structure
struct bharath
{
int b;
char c;
float d;
}

and how?
Enough to fit at least the total memory requirements of the struct.
Since the primitive types in the struct can vary in width from platform
to platform and implementation to implementation (except char, of
course) the size of the struct will also vary.

How do you do it, or how is memory allocated at runtime? The former is
just allocating for any type, really. If the latter, this is likely
implementation specific, at least based on my lurking in this newsgroup,
and my (poor) knowledge of the Standard.
 
R

Richard Heathfield

(e-mail address removed) said:
how much memory is allocated for following structure
struct bharath
{
int b;
char c;
float d;
}

None. It's a type, not an object.
 
B

balu

(e-mail address removed) said:


None. It's a type, not an object.
let the int size is 4B then the allocation will be?
i want to know the info regarding padding or some other else related
to padding?
 
C

Chris Dollin

how much memory is allocated for following structure
struct bharath
{
int b;
char c;
float d;
}

and how?

Objects of that type will have enough memory allocated to hold an
int, a char, a float (in that order), plus any necessary padding.

The details vary with the implementation -- which may include the
option settings for your compilation(s).

If you want to know how much has /actually/ been allocated, use
`sizeof (struct bharath)`.
 
C

Clever Monkey

[Please use a newsreader that understands how to trim sigs, or trim them
yourself. I've done this for you here.]
let the int size is 4B then the allocation will be?
i want to know the info regarding padding or some other else related
to padding?
This is exactly the sort of thing that people talk about when they say
something is implementation specific. That is, once you actually
allocate for a type, the specifics of how that memory is allocated is
not necessarily dictated by the Standard.

Simply defining (or is it declaring?) the type, of course, allocates no
memory.
 
B

bharath539

[Please use a newsreader that understands how to trim sigs, or trim them
yourself. I've done this for you here.]
let the int size is 4B then the allocation will be?
i want to know the info regarding padding or some other else related
to padding?

This is exactly the sort of thing that people talk about when they say
something is implementation specific. That is, once you actually
allocate for a type, the specifics of how that memory is allocated is
not necessarily dictated by the Standard.

Simply defining (or is it declaring?) the type, of course, allocates no
memory.
--
clvrmnky <mailto:[email protected]>

Direct replies will be blacklisted. Replace "spamtrap" with my name to
contact me directly.

what i want actually is how much size will be allocated for above
structuer in a 16 bit machine
 
L

Lew Pitcher

[Please use a newsreader that understands how to trim sigs, or trim them
yourself. I've done this for you here.]
let the int size is 4B then the allocation will be?
i want to know the info regarding padding or some other else related
to padding?
This is exactly the sort of thing that people talk about when they say
something is implementation specific. That is, once you actually
allocate for a type, the specifics of how that memory is allocated is
not necessarily dictated by the Standard.
Simply defining (or is it declaring?) the type, of course, allocates no
memory.
Direct replies will be blacklisted. Replace "spamtrap" with my name to
contact me directly.

what i want actually is how much size will be allocated for above
structuer in a 16 bit machine

Here's the exact answer for you: your program will allocate
sizeof(struct bharath) bytes. Other than that, we cannot tell you, as
the exact number of bytes depends on the compiler, the operating
system, and the processor.

What we can tell you is that there may be padding characters inserted
between each element of the structure to maintain data item alignment.
If, *for instance*, ints were 4 bytes wide, and floats were 8 bytes
wide, and both were required to be aligned on "even" boundaries (ints
to &int%4==0 boundaries, floats to &float%8==0 boundaries), then the
compiler would likely insert zero padding bytes between your int and
your char, and seven padding bytes between your char and your float.
 
L

Lew Pitcher

Oops... a correction

What we can tell you is that there may be padding characters inserted
between each element of the structure to maintain data item alignment.
If, *for instance*, ints were 4 bytes wide, and floats were 8 bytes
wide, and both were required to be aligned on "even" boundaries (ints
to &int%4==0 boundaries, floats to &float%8==0 boundaries), then the
compiler would likely insert zero padding bytes between your int and
your char, and seven padding bytes

make that three padding bytes
 
C

Clever Monkey

[...]
*sigh*
This is exactly the sort of thing that people talk about when they say
something is implementation specific. That is, once you actually
allocate for a type, the specifics of how that memory is allocated is
not necessarily dictated by the Standard.

Simply defining (or is it declaring?) the type, of course, allocates no
memory.
[...]
*double sigh*
what i want actually is how much size will be allocated for above
structuer in a 16 bit machine
Enough to hold the structure, plus any padding, on this specific 16-bit
machine.

Folks here are not being coy. There is no way to know, in general, how
your compiler will allocate space, even if we know something about it or
the platform is is generating code for. (Like it happens to be a
"16-bit" machine, whatever that is. How are those bits represented?)

If you have a specific implementation in mind, on a specific platform,
then someone with the knowledge and access to the documentation could
tell you with more detail.

This very quality of C is why the compiler has been successfully
targeted to a great many platforms, and why standard code is relatively
easy to port.
 
M

Mark McIntyre

what i want actually is how much size will be allocated for above
structuer in a 16 bit machine

There is no correct answer. Depending on compiler settings, hardware
requirements and so on, it could be anything from seven bytes upwards.
Its impossible to say.

Why do you need to know?
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
D

Default User

how much memory is allocated for following structure
struct bharath
{
int b;
char c;
float d;
}

This is, for some reason, a common newbie question. It doesn't need to
be. When it comes to struct sizes, there are only two things to keep in
mind:

1. The actual size is implementation-specific. That means it varies
from compiler to compiler, perhaps even with the same compiler
depending on options. There's no way to know from code inspection.

2. When you need to know the size in a program, the sizeof operator
will tell you. The actual value is of little importance.




Brian
 
M

Martin Ambuhl

how much memory is allocated for following structure
struct bharath
{
int b;
char c;
float d;
}
^^ ';' needed

None, until an instance of that structure is declared.
If you have one
struct bharath foo;
the space allocated is
sizeof foo
or
sizeof(struct bharath)
and is at least
sizeof(int)+sizeof(float)+1
With whatever mechanism your compiler allocates space for other variables.
 
A

Army1987

how much memory is allocated for following structure
struct bharath
{
int b;
char c;
float d;
}

None, because you are not declaring an object.

If you wrote
struct bharath {
int b;
char c;
float d;
} a;

It would allocate sizeof a bytes, i.e. sizeof(struct bharath)
bytes.

The same way it is allocated for any other object.
 
A

Army1987

balu said:
(e-mail address removed) said:
how much memory is allocated for following structure
struct bharath
{
int b;
char c;
float d;
}
None. It's a type, not an object.

[Please use a newsreader that understands how to trim sigs, or trim them
yourself. I've done this for you here.]
let the int size is 4B then the allocation will be?
i want to know the info regarding padding or some other else related
to padding?

This is exactly the sort of thing that people talk about when they say
something is implementation specific. That is, once you actually
allocate for a type, the specifics of how that memory is allocated is
not necessarily dictated by the Standard.

Simply defining (or is it declaring?) the type, of course, allocates no
memory.
--
clvrmnky <mailto:[email protected]>

Direct replies will be blacklisted. Replace "spamtrap" with my name to
contact me directly.

what i want actually is how much size will be allocated for above
structuer in a 16 bit machine
It doesn't even have to be the same on all 16-bit machines, or even
on the same machine with different compilers.
 
J

Jack Klein

C

Chris Dollin

(fx:snipping)

what i want actually is how much size will be allocated for above
structuer in a 16 bit machine

It depends on the implementation.

Really.

A "16-bit machine" (and what /that/ means isn't unambiguous) might have
a C implementation which used 32-bit ints. Or 16-bit ints. It might have
32-bit floats. Or 64. Floats might need to be allocated on 16-bit boundaries.
Or 32-bit boundaries. Or 8-bit boundaries. chars might be 8 bits wide.
Or 16. You might be able to /select/ these things with, say, command-line
switches.

It depends on the implementation.
 
S

SM Ryan

# how much memory is allocated for following structure

None. Structures only exist in the compiler; in the code
you have blocks of allocated memory and offsets into them.

A variable of type T needs no more than sizeof(T) char sized
units allocated on appropriate memory boundary.

# struct bharath
# {
# int b;
# char c;
# float d;
# }

A variable of type (struct bharath) will be allocated at
least sizeof(struct bharath) bytes. How a compiler allocates
the space is really up to the compiler. If you need to know
there are field offset macros to find otu where each field
begins in the variable's allocated space.
 

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
474,266
Messages
2,571,073
Members
48,772
Latest member
Backspace Studios

Latest Threads

Top