473,770 Members | 7,142 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

about structures memory allocation

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

and how?

Jun 21 '07 #1
43 2376
bh********@gmai l.com wrote:
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.
--
clvrmnky <mailto:sp***** *@clevermonkey. org>

Direct replies will be blacklisted. Replace "spamtrap" with my name to
contact me directly.
Jun 21 '07 #2
bh********@gmai l.com 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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jun 21 '07 #3
On Jun 21, 7:33 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
bharath...@gmai l.com 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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999http://www.cpax.org.uk
email: rjh at the above domain, - www.
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?

Jun 21 '07 #4
bh********@gmai l.com wrote:
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)`.

--
Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

Jun 21 '07 #5
balu wrote:
On Jun 21, 7:33 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
>bharath...@gma il.com 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:sp***** *@clevermonkey. org>

Direct replies will be blacklisted. Replace "spamtrap" with my name to
contact me directly.
Jun 21 '07 #6
On Jun 21, 7:49 pm, Clever Monkey <spamt...@cleve rmonkey.org.INV ALID>
wrote:
balu wrote:
On Jun 21, 7:33 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
bharath...@gmai l.com 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:spamt.. .@clevermonkey. org>

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

Jun 21 '07 #7
On Jun 21, 10:56 am, "bharath...@gma il.com" <bharath...@gma il.com>
wrote:
On Jun 21, 7:49 pm, Clever Monkey <spamt...@cleve rmonkey.org.INV ALID>
wrote:
balu wrote:
On Jun 21, 7:33 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
>bharath...@gma il.com 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:spamt.. .@clevermonkey. org>
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.
Jun 21 '07 #8
Oops... a correction

On Jun 21, 11:09 am, Lew Pitcher <lpitc...@teksa vvy.comwrote:
[snip]
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
between your char and your float.

Jun 21 '07 #9
bh********@gmai l.com wrote:
On Jun 21, 7:49 pm, Clever Monkey <spamt...@cleve rmonkey.org.INV ALID>
wrote:
>balu wrote:
>>On Jun 21, 7:33 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
bharath...@g mail.com 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.
[...]
*sigh*
>>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.
[...]
*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.
--
clvrmnky <mailto:sp***** *@clevermonkey. org>

Direct replies will be blacklisted. Replace "spamtrap" with my name to
contact me directly.
Jun 21 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
4484
by: Berk Birand | last post by:
Hi all, I have to use C-style structures for an assignement. I cannot have any methods or constructors for it. What has surprised me is that in my code, I have to allocate memory for an array of structures with malloc. Otherwise, I get a seg fault. Here's the code: struct Employee { char* name; // Pointer to character string holding name of employee. int salary;
4
3862
by: Thomas Paul Diffenbach | last post by:
Can anyone point me to an open source library of /statically allocated/ data structures? I'm writing some code that would benefit from trees, preferably self balancing, but on an embedded system that doesn't offer dynamic memory allocation (to be clear: no malloc, no realloc), and with rather tight memory constraints. Writing my own malloc to do dynamic allocation from some static pool isn't really an option, for various reasons, not...
18
2450
by: Peter Smithson | last post by:
Hi, I've read this page - http://devrsrc1.external.hp.com/STK/impacts/i634.html but don't understand it. Here's the text - "Non-standard usage of setjmp() and longjmp() could result in compatibility problems. The contents of the jmp_buf buffer are specific
1
1261
by: Joe.ntang | last post by:
Hi, The following function is what I want to call, int DbEnv::memp_stat(DB_MPOOL_STAT **gsp, DB_MPOOL_FSTAT *(*fsp), u_int32_t flags); The function has following description:
11
3786
by: skumar434 | last post by:
Hi everybody, I am faceing problem while assigning the memory dynamically to a array of structures . Suppose I have a structure typedef struct hom_id{ int32_t nod_de; int32_t hom_id;
44
5808
by: svata | last post by:
Hello, I wonder how to resize such array of structures using realloc()? #include <stdio.h> #include <stdlib.h> #define FIRST 7 typedef struct { char *name;
23
4345
by: TefJlives | last post by:
Hi all, I'm learning a bit about C, and I have a few questions. I'm not trying to insult C or anything with these questions, they're just honestly things I don't get. It seems like pointers to chars are just how you deal with strings, and pointers to pointers to char just give you arrays of strings. What is the advantage of this vs. languages with a string type?
5
1888
by: TommyB | last post by:
Hi, I have a little programm that uses an array of pointers to a structure. Everything works fine until I free up the memory. Here is the sample code: #include <stdio.h> #include <math.h> #include <string.h> #include <stdlib.h>
19
3399
by: =?ISO-8859-1?Q?Nordl=F6w?= | last post by:
I am currently designing a synchronized queue used to communicate between threads. Is the code given below a good solution? Am I using mutex lock/unlock more than needed? Are there any resources out there on the Internet on how to design *thread-safe* *efficient* data- structures? /Nordlöw
0
9592
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9425
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10230
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10058
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8886
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5313
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5450
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3972
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3576
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.