473,796 Members | 2,512 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
43 2387
Army1987 wrote:
>
.... snip ...
>
In something like
int main(void) {
struct bharath b;
long long ll;
...
}
since the standard requires nothing about the placement of auto
variables, it is well possible that there is some space between
b and ll, but an auto char in an inner block might well be put into
that space. (Yes, I don't think it is very likely to happen...)
So, as far as a reasonable definition of "to allocate" is
concerned, these two declarations allocate exactly sizeof b +
sizeof ll bytes, even if they are not contiguous.
A long long requires <sizeof long long\bytes, not 11.

--
<http://www.cs.auckland .ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfoc us.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Jun 24 '07 #31
Keith Thompson wrote:
Barry Schwarz <sc******@doezl .netwrites:
>On Sun, 24 Jun 2007 00:10:45 -0000, SM Ryan
<wy*****@tan go-sierra-oscar-foxtrot-tango.fake.orgw rote:
[...]
>>> 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
Would you care to give an example of when it will be allocated more
than sizeof(srtuct bharath) bytes?

malloc(sizeof(s truct bharath)) can easily allocate more than
sizeof(struct bharath) bytes. Even for an object declaration, there
may be a gap between the object and the next object in memory.
I don't think so. First, sizeof (struct bharath) is 12 at my house. The
offsetof the members are 0, 4 and 8 regardless of their arrangement. If
the char member is the last it is still at offset 8 and sizeof the
struct is still 12. There can be no gap between the object and the next
object.
You could argue, of course, that the extra bytes aren't allocated *to
the object*, but are just padding outside the object (and I wouldn't
disagree).
There are no 'extra bytes' that you can find. That malloc might allocate
more than asked for is true but not at issue.

struct bharath *bhar = malloc(3 * sizeof *bhar);

If successful, this will allocate at least 36 bytes for three instances
of the struct. malloc may allocate more for its own purposes but we
can't know and don't care.

Or did I misunderstand you?

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Jun 24 '07 #32
CBFalconer wrote:
Army1987 wrote:
>>
... snip ...
>>
In something like
int main(void) {
struct bharath b;
long long ll;
...
}
since the standard requires nothing about the placement of auto
variables, it is well possible that there is some space between
b and ll, but an auto char in an inner block might well be put into
that space. (Yes, I don't think it is very likely to happen...)
So, as far as a reasonable definition of "to allocate" is
concerned, these two declarations allocate exactly sizeof b +
sizeof ll bytes, even if they are not contiguous.

A long long requires <sizeof long long\bytes, not 11.
A variable named LL requires sizeof LL bytes, and the lowercase equivalent
requires sizeof ll bytes.
Jun 24 '07 #33
CBFalconer <cb********@yah oo.comwrites:
Army1987 wrote:
>>
... snip ...
>>
In something like
int main(void) {
struct bharath b;
long long ll;
...
}
since the standard requires nothing about the placement of auto
variables, it is well possible that there is some space between
b and ll, but an auto char in an inner block might well be put into
that space. (Yes, I don't think it is very likely to happen...)
So, as far as a reasonable definition of "to allocate" is
concerned, these two declarations allocate exactly sizeof b +
sizeof ll bytes, even if they are not contiguous.

A long long requires <sizeof long long\bytes, not 11.
Chuck, if you can't tell the difference between "l" (lowercase L) and
"1" (digit one), get a better font. At least take a closer look
before assuming that somebody wrote something nonsensical like "sizeof
11". (Yes, "sizeof 11" is legal, but it makes no sense in context.)

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 24 '07 #34
Joe Wright <jo********@com cast.netwrites:
Keith Thompson wrote:
>Barry Schwarz <sc******@doezl .netwrites:
>>On Sun, 24 Jun 2007 00:10:45 -0000, SM Ryan
<wy*****@tang o-sierra-oscar-foxtrot-tango.fake.orgw rote:
[...]
>>>> 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
Would you care to give an example of when it will be allocated more
than sizeof(srtuct bharath) bytes?
malloc(sizeof( struct bharath)) can easily allocate more than
sizeof(struc t bharath) bytes. Even for an object declaration, there
may be a gap between the object and the next object in memory.
I don't think so. First, sizeof (struct bharath) is 12 at my
house. The offsetof the members are 0, 4 and 8 regardless of their
arrangement. If the char member is the last it is still at offset 8
and sizeof the struct is still 12. There can be no gap between the
object and the next object.
Sure there can.

Suppose sizeof(struct bharath) is 12, and suppose you have another
type struct foo that requires 16-byte alignment. Then if you declare

struct bharath obj1;
struct foo obj2;

there easily *could* be a 4-byte gap between obj1 and obj2. Or the
compiler could reverse the order, or it could allocate another object
between obj1 and obj2, or whatever.

I'm not saying this is of any particular importance, just that it's
possible and plausible.

I'm sure you know this, and I suspect I'm misinterpreting your
statement that "There can be no gap between the object and the next
object."; can you clarify what you mean?
>You could argue, of course, that the extra bytes aren't allocated *to
the object*, but are just padding outside the object (and I wouldn't
disagree).
There are no 'extra bytes' that you can find. That malloc might
allocate more than asked for is true but not at issue.
Ok, so what exactly is the issue? I think I've lost track.

I think we all know what the answers are, and we're arguing over what
the question is, or perhaps what the question should be.

[...]

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 24 '07 #35
Keith Thompson wrote:
Joe Wright <jo********@com cast.netwrites:
>Keith Thompson wrote:
>>Barry Schwarz <sc******@doezl .netwrites:
On Sun, 24 Jun 2007 00:10:45 -0000, SM Ryan
<wy*****@tan go-sierra-oscar-foxtrot-tango.fake.orgw rote:
[...]
> 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
Would you care to give an example of when it will be allocated more
than sizeof(srtuct bharath) bytes?
malloc(sizeof (struct bharath)) can easily allocate more than
sizeof(stru ct bharath) bytes. Even for an object declaration, there
may be a gap between the object and the next object in memory.
I don't think so. First, sizeof (struct bharath) is 12 at my
house. The offsetof the members are 0, 4 and 8 regardless of their
arrangement. If the char member is the last it is still at offset 8
and sizeof the struct is still 12. There can be no gap between the
object and the next object.

Sure there can.

Suppose sizeof(struct bharath) is 12, and suppose you have another
type struct foo that requires 16-byte alignment. Then if you declare

struct bharath obj1;
struct foo obj2;

there easily *could* be a 4-byte gap between obj1 and obj2. Or the
compiler could reverse the order, or it could allocate another object
between obj1 and obj2, or whatever.
I wasn't thinking about a gap between obj1 and obj2. If there is such a
gap we don't care and I think we can't even test for it.
I'm not saying this is of any particular importance, just that it's
possible and plausible.

I'm sure you know this, and I suspect I'm misinterpreting your
statement that "There can be no gap between the object and the next
object."; can you clarify what you mean?
I was alluding to an array of the particular structure.
>>You could argue, of course, that the extra bytes aren't allocated *to
the object*, but are just padding outside the object (and I wouldn't
disagree).
There are no 'extra bytes' that you can find. That malloc might
allocate more than asked for is true but not at issue.

Ok, so what exactly is the issue? I think I've lost track.

I think we all know what the answers are, and we're arguing over what
the question is, or perhaps what the question should be.
Indeed. I got sideways on the 'extra bytes' thing.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Jun 24 '07 #36
# >>Would you care to give an example of when it will be allocated more
# >>than sizeof(srtuct bharath) bytes?
# >malloc(sizeof( struct bharath)) can easily allocate more than
# >sizeof(struc t bharath) bytes. Even for an object declaration, there
# >may be a gap between the object and the next object in memory.

Sometimes a compiler will allocate variables in word sized units
to improve performance. As far as such compilers are concerned,
the variable is a whole number of words which can be greater
than the sizeof.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
OOOOOOOOOO! NAVY SEALS!
Jun 25 '07 #37
On Jun 21, 10:33 am, 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.
A newbie question: surely, the struct definition is stored somewhere.
Else, how would the struct be allocated at runtime? And this would
require some memory...

Regards,
Frodo B

Jun 25 '07 #38
On Mon, 25 Jun 2007 01:21:58 -0000, SM Ryan
<wy*****@tang o-sierra-oscar-foxtrot-tango.fake.orgw rote:
># >>Would you care to give an example of when it will be allocated more
# >>than sizeof(srtuct bharath) bytes?
# >malloc(sizeof( struct bharath)) can easily allocate more than
# >sizeof(struc t bharath) bytes. Even for an object declaration, there
# >may be a gap between the object and the next object in memory.

Sometimes a compiler will allocate variables in word sized units
to improve performance. As far as such compilers are concerned,
the variable is a whole number of words which can be greater
than the sizeof.
No it cannot. The reason is that elements of an array must abut and
the byte difference between the start of one element and the next
element must be exactly sizeof element. If the compiler elects to pad
a structure out to a word boundary, then that padding is included the
value that sizeof evaluates to.
Remove del for email
Jun 25 '07 #39
Frodo Baggins said:
On Jun 21, 10:33 am, 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.

A newbie question: surely, the struct definition is stored somewhere.
Yes, the compiler will need to store the definition in its own internal
memory, but in those terms the question is unanswerable without
detailed internal knowledge of the compiler working. But that's not
normally what we mean when we say "how much memory".

Else, how would the struct be allocated at runtime?
It wouldn't be. It's a type, not an object. You don't allocate types.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 25 '07 #40

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

Similar topics

8
4487
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
3865
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
2454
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
1263
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
3787
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
5819
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
4346
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
1891
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
3400
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
9673
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
9525
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
10003
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9050
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...
1
7546
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6785
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5440
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...
1
4115
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
3
2924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.