Optimizing structure memory allocation

  • Permalink
  • submit to reddit
  • Email
  • Follow


How is the memory allocated for structures? I need to optimize the
memory usage and bit fields are not doing the trick.

Any details about the memory allocation for the structures would be a
great help.

PS - I already have asked this in gcc group. Please refrain from
directing me towards other groups.
0
Reply rahulsinner (161) 5/27/2008 12:25:47 PM

See related articles to this posting


rahul said:

> How is the memory allocated for structures?

"A structure type describes a sequentially allocated set of member 
objects", says the Standard.

Later, it adds: "If the objects pointed to are members of the same 
aggregate object, pointers to structure members declared later compare 
higher than pointers to members declared earlier in the structure".

Finally, "There may also be unnamed padding at the end of a structure or 
union, as necessary to achieve the appropriate alignment were the 
structure or union to be a member of an array."

<snip>

> PS - I already have asked this in gcc group. Please refrain from
> directing me towards other groups.

Gladly, if you are happy to accept that the answers you get here will be 
related to what the language guarantees, rather than which particular 
choice an implementation might make where the language offers such a 
choice.

-- 
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
0
Reply rjh (10790) 5/27/2008 12:44:42 PM

In article <859569b1-e7e0-4711-969e-8960bc5df728@i36g2000prf.googlegroups.com>,
rahul  <rahulsinner@gmail.com> wrote:

>How is the memory allocated for structures? I need to optimize the
>memory usage and bit fields are not doing the trick.
>
>Any details about the memory allocation for the structures would be a
>great help.

You'll probably get better help if you ask a more specific question.
Show us what you want to put in the structure.

The members of a struct are stored in the order you specify.  Some
members may have types that require a particular alignment, typically
equal to their size.  For example, while chars can go anywhere you may
find that if ints are 4 bytes long then they are always placed on
4-byte boundaries.  This is common even if the processor allows
arbitrary alignment, because it's usually faster to access
suitably-aligned data.

Suppose shorts are 2 bytes and ints are 4, with corresponding
alignment requirements, and you want to store 2 chars, a short, and an
int in your struct.  These could fit in 8 bytes, and will if you order
them correctly, for example

  struct foo {
    int a;
    short b;
    char c, d;
  };

but if you do

  struct foo2 {
    char c;
    int a;
    char d;
    short b;
  };

it will take 12 bytes, because the 3 bytes after c and the byte after d
are wasted.

If you use bitfields, then you have to consider similar issues at the
bit level.  Adjacent bitfields that fit within a "unit" (probably an
int) will be packed together, so don't spread them out amongst other
members of the struct, and try to order them so that they don't go
over too many int boundaries.

-- Richard
-- 
In the selection of the two characters immediately succeeding the numeral 9,
consideration shall be given to their replacement by the graphics 10 and 11 to
facilitate the adoption of the code in the sterling monetary area. (X3.4-1963)
0
Reply richard91 (3692) 5/27/2008 12:53:47 PM

rahul wrote:
> 
> How is the memory allocated for structures? I need to optimize
> the memory usage and bit fields are not doing the trick.
> 
> Any details about the memory allocation for the structures would
> be a great help.
> 
> PS - I already have asked this in gcc group. Please refrain from
> directing me towards other groups.

If you are using gcc, then a gcc group is appropriate.  This one is
not.  A compiler system can use any method of memory allocation it
pleases, so long as it works.

-- 
 [mail]: Chuck F (cbfalconer at maineline dot net) 
 [page]: <http://cbfalconer.home.att.net>
            Try the download section.

** Posted from http://www.teranews.com **
0
Reply cbfalconer (19194) 5/27/2008 6:41:32 PM

On 27 May 2008 at 12:25, rahul wrote:
> How is the memory allocated for structures? I need to optimize the
> memory usage and bit fields are not doing the trick.
>
> PS - I already have asked this in gcc group. Please refrain from
> directing me towards other groups.

You might want to look at gcc's packed attribute, which "specifies that
a variable or structure field should have the smallest possible
alignment - one byte for a variable, and one bit for a field, unless you
specify a larger value with the aligned attribute."

You can either pack specific fields, e.g.

struct foo {
  char a;
  int b __attribute__((packed));
};

or whole structs at once, e.g.

struct bar {
  int a;
  char b;
  int c;
  char d;
  int e;
} __attribute__((packed));

0
Reply nospam59 (11087) 5/27/2008 7:18:08 PM

Antoninus Twink <nospam@nospam.invalid> writes:
> On 27 May 2008 at 12:25, rahul wrote:
>> How is the memory allocated for structures? I need to optimize the
>> memory usage and bit fields are not doing the trick.
>>
>> PS - I already have asked this in gcc group. Please refrain from
>> directing me towards other groups.
>
> You might want to look at gcc's packed attribute,
[...]

which is, of course, off-topic in comp.lang.c.

rahul, you asked us not to direct you to other groups.  Why?
Questions about gcc-specific features are appropriate in gnu.gcc.help;
they are not appropriate in comp.lang.c.

Or you can consult the extensive documentation that comes with gcc.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
0
Reply kst-u (21963) 5/27/2008 7:33:34 PM

In article <g1h08r$1c9f$1@pc-news.cogsci.ed.ac.uk>,
Richard Tobin <richard@cogsci.ed.ac.uk> wrote:
>In article <859569b1-e7e0-4711-969e-8960bc5df728@i36g2000prf.googlegroups.com>,
>rahul  <rahulsinner@gmail.com> wrote:

>>How is the memory allocated for structures? I need to optimize the
>>memory usage and bit fields are not doing the trick.

>The members of a struct are stored in the order you specify.

Expanding slightly on Richard's answer for emphasis:

C *requires* that structure members be placed in the order given,
and in increasing address order in memory. Storing the members in
the order coded is not just a matter of convention: it is part
of the language definition. As is the fact that alignment restrictions
exist and are adhered to by the compiler by adding unnamed padding
between structure members if needed (or advised) by the target machine.
-- 
  "All is vanity."                                   -- Ecclesiastes
0
Reply roberson2 (8607) 5/27/2008 7:44:51 PM

roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
> In article <g1h08r$1c9f$1@pc-news.cogsci.ed.ac.uk>,
> Richard Tobin <richard@cogsci.ed.ac.uk> wrote:
>>In article <859569b1-e7e0-4711-969e-8960bc5df728@i36g2000prf.googlegroups.com>,
>>rahul  <rahulsinner@gmail.com> wrote:
>
>>>How is the memory allocated for structures? I need to optimize the
>>>memory usage and bit fields are not doing the trick.
>
>>The members of a struct are stored in the order you specify.
>
> Expanding slightly on Richard's answer for emphasis:
>
> C *requires* that structure members be placed in the order given,
> and in increasing address order in memory. Storing the members in
> the order coded is not just a matter of convention: it is part
> of the language definition. As is the fact that alignment restrictions
> exist and are adhered to by the compiler by adding unnamed padding
> between structure members if needed (or advised) by the target machine.

Right, but it's perfectly legal for the alignment restriction to be
"any alignment is ok".  It's also legal (but silly) for the compiler
to insert padding whether it's required for alignment or not.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
0
Reply kst-u (21963) 5/27/2008 8:02:33 PM

On Tue, 27 May 2008 13:02:33 -0700, Keith Thompson wrote:
> Right, but it's perfectly legal for the alignment restriction to be "any
> alignment is ok".  It's also legal (but silly) for the compiler to
> insert padding whether it's required for alignment or not.

It's not silly if older systems required a particular alignment, newer 
systems don't, but the compiler still inserts padding to maintain binary 
compatibility with those older systems. It's good that implementors are 
given a lot of freedom in choosing what works best for their systems.
0
Reply truedfx (1926) 5/27/2008 8:35:12 PM

Keith Thompson wrote:
> roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
>> 
>> C *requires* that structure members be placed in the order given,
>> and in increasing address order in memory. Storing the members in
>> the order coded is not just a matter of convention: it is part
>> of the language definition. As is the fact that alignment restrictions
>> exist and are adhered to by the compiler by adding unnamed padding
>> between structure members if needed (or advised) by the target machine.
> 
> Right, but it's perfectly legal for the alignment restriction to be
> "any alignment is ok".  It's also legal (but silly) for the compiler
> to insert padding whether it's required for alignment or not.

     The ambiguity lies in "requirement."  On one popular
platform, for example, a `double' can be accessed at any
address divisible by four, but can be accessed more quickly
if the address is also divisible by eight.  Should the
alignment "requirement" be taken as four or as eight?

-- 
Eric.Sosman@sun.com
0
Reply Eric.Sosman (4552) 5/27/2008 9:03:59 PM

Eric Sosman <Eric.Sosman@sun.com> writes:
> Keith Thompson wrote:
>> roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
>>> C *requires* that structure members be placed in the order given,
>>> and in increasing address order in memory. Storing the members in
>>> the order coded is not just a matter of convention: it is part
>>> of the language definition. As is the fact that alignment restrictions
>>> exist and are adhered to by the compiler by adding unnamed padding
>>> between structure members if needed (or advised) by the target machine.
>> Right, but it's perfectly legal for the alignment restriction to be
>> "any alignment is ok".  It's also legal (but silly) for the compiler
>> to insert padding whether it's required for alignment or not.
>
>     The ambiguity lies in "requirement."  On one popular
> platform, for example, a `double' can be accessed at any
> address divisible by four, but can be accessed more quickly
> if the address is also divisible by eight.  Should the
> alignment "requirement" be taken as four or as eight?

Could be either.  My point is that the compiler is allowed to insert,
say, 16 bytes of padding, even if it doesn't help performance at all.

It's just an example of the kind of assumption that shouldn't be made
in portable code.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
0
Reply kst-u (21963) 5/27/2008 9:14:36 PM

In article <ln8wxvqwjn.fsf@nuthaus.mib.org>,
Keith Thompson  <kst-u@mib.org> wrote:

>Could be either.  My point is that the compiler is allowed to insert,
>say, 16 bytes of padding, even if it doesn't help performance at all.
>
>It's just an example of the kind of assumption that shouldn't be made
>in portable code.

I disagree (though obviously only up to a point).  It's perfectly
legal for a compiler to insert pointless alignment, but it's also
reasonable to assume that it won't.  For example, given any sensible
alignment scheme you will get good packing by ordering members by
decreasing size (int then short then char for example).  A portable
program can quite reasonably order structure members on this
assumption, and if it turns out to be false then the blame should be
assigned to the implementation, not the programmer.

We aren't obliged to ignore common sense in our programs, even if
conforming compilers can defeat it.

-- Richard
-- 
In the selection of the two characters immediately succeeding the numeral 9,
consideration shall be given to their replacement by the graphics 10 and 11 to
facilitate the adoption of the code in the sterling monetary area. (X3.4-1963)
0
Reply richard91 (3692) 5/27/2008 9:30:18 PM

Keith Thompson wrote:
> Eric Sosman <Eric.Sosman@sun.com> writes:
>> Keith Thompson wrote:
>>> [...] It's also legal (but silly) for the compiler
>>> to insert padding whether it's required for alignment or not.
>>     The ambiguity lies in "requirement."  On one popular
>> platform, for example, a `double' can be accessed at any
>> address divisible by four, but can be accessed more quickly
>> if the address is also divisible by eight.  Should the
>> alignment "requirement" be taken as four or as eight?
> 
> Could be either.  My point is that the compiler is allowed to insert,
> say, 16 bytes of padding, even if it doesn't help performance at all.

     ... and my point was about the word "silly:" Until you
can nail down "requirement" you can't assess the silliness
of the padding scheme.

-- 
Eric Sosman
esosman@ieee-dot-org.invalid
0
Reply esosman2 (3096) 5/28/2008 12:22:59 PM

Eric Sosman <esosman@ieee-dot-org.invalid> writes:
> Keith Thompson wrote:
>> Eric Sosman <Eric.Sosman@sun.com> writes:
>>> Keith Thompson wrote:
>>>> [...] It's also legal (but silly) for the compiler
>>>> to insert padding whether it's required for alignment or not.
>>>     The ambiguity lies in "requirement."  On one popular
>>> platform, for example, a `double' can be accessed at any
>>> address divisible by four, but can be accessed more quickly
>>> if the address is also divisible by eight.  Should the
>>> alignment "requirement" be taken as four or as eight?
>> Could be either.  My point is that the compiler is allowed to insert,
>> say, 16 bytes of padding, even if it doesn't help performance at all.
>
>     ... and my point was about the word "silly:" Until you
> can nail down "requirement" you can't assess the silliness
> of the padding scheme.

Ok, good point.

To re-state my point more precisely, a compiler is allowed to insert
padding between struct members even if doing so has no benefit
whatsoever.  (I don't know of any compilers that actually do so.)

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
0
Reply kst-u (21963) 5/28/2008 4:13:48 PM

CBFalconer <cbfalconer@yahoo.com> writes:
> rahul wrote:
>> How is the memory allocated for structures? I need to optimize
>> the memory usage and bit fields are not doing the trick.
>> 
>> Any details about the memory allocation for the structures would
>> be a great help.
>> 
>> PS - I already have asked this in gcc group. Please refrain from
>> directing me towards other groups.
>
> If you are using gcc, then a gcc group is appropriate.  This one is
> not.  A compiler system can use any method of memory allocation it
> pleases, so long as it works.

But the C standard imposes a number of requirements on how memory is
allocated for structures (as we already discussed in this thread).

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
0
Reply kst-u (21963) 5/30/2008 2:11:19 AM
comp.lang.c 29372 articles. 31 followers. Post

14 Replies
113 Views

Similar Articles

[PageSpeed] 14


  • Permalink
  • submit to reddit
  • Email
  • Follow


Reply:

Similar Artilces:

Memory allocation in Structure to Structure
Hi all, While coding, I got struck up in memory allocation for structure to structure.. For instance..I wrote the following pgm.. #include <iostream.h> #include <stdlib.h> typedef struct A { int b; int c; struct E { int d; int e; } *E; } A; void main() { A* i = (A * ) calloc(1,sizeof(A); } I need to allocate memory for the internal structure E. Iam getting errors whatever format I follow.. i tried the following.. A.E = (E * ) calloc(1,sizeof(E); A.E = (A.E * ) calloc(1,sizeof(E); and other combinations.. Can anybody help m...

Allocating memory to nested Structures within a Structure
Dear Friends I have the follwoing function "tss_fe_get_own_info" which has the arguments as shows below tss_fe_get_own_info(char *user_id, tss_user_profile_t **p_buf_UserSecInfo, error_status_t *retStatus) // Structure tss_user_profile_t from the function typedef struct { tss_user_info_t user_info; odss_attribute_value_list_t *ProcAttr; odss_attribute_value_list_t *BkOffAttr; odss_attribute_value_list_t *WkClusterAttr; odss_attribute_value_list_t *CSUAttr; } tss_user_profile_t; // Structure tss_user_info_t typedef struct { char user_uuid[USER_UUID_LEN+1]; char user_sec_info...

about structures memory allocation
how much memory is allocated for following structure struct bharath { int b; char c; float d; } and how? bharath539@gmail.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 ...

Structure memory allocation
Consider the following declaration, #include <stdio.h> #include <stdlib.h> typedef struct foo { char name[30]; int age; }Foo; typedef struct bar { char * name; int age; }Bar; typedef struct baz { char name[30]; int *age; }Baz; int main() { Foo f1,f2; Bar b1,b2; Baz c1,c2; /* Populating structure */ strcpy(f1.name,"JACK"); f1.age=10; /* CASE 1 - Works fine */ f2=f1; printf("\n F2 Members, Name %s , Age %d ", f2.name, f2.age); printf("\n F1 Pointer %p , F2 Pointer %p ", f1, f2); /*...

Structure memory allocation #2
Hi All, I need one clarification regarding memory allocation for structure. The details are as given below : let us consider one structure struct { uit32 len0; uint8 *pointer0; uit32 len1; uint8 *pointer1; ); When i want to allocate memory using malloc() for this structure is there any way i can allocate memory for only for len0 and pointer0 or len1 and pointer1 . Regards, udaykumar uday wrote: > Hi All, > I need one clarification regarding memory allocation for > structure. The details are as given below : > > let us consider one structure > > st...

memory allocation for structure array
Hi, I'm new to C programming and am having the hardest time trying to figure out how to allocate memory for a structure array. Can somebody please see if I'm doing things correctly? My code is as follows. typedef struct s_city{ double x, y; } CITY; .... CITY *cities; cities = (struct s_city *)malloc(numberOfCities * sizeof(struct s_city)); .... later in the code, when I try to do for(i=0; i<numberOfCities; i++) { cities[i].x = someX; cities[i].y = someY; } I get a segmentation fault. I really don't know what I am doing wrong. Help me please. Thank you in advance. -...

Memory Allocation for Arrays of Structure
Hi Assume we have a Structure with Various Fields A.a,A.b,A.c,A.d,... Now we want to have an array of it (with known size). Say for example 2000 A(1),A(2),A(3),... What is the most efficient way to force MATLAB allocate memory for all data at the same time? Hint: In general size of field name of various strucure are not supposed to be equal: A(1).a=[1 2 3] A(2).a=[1 2 3 4 5] but at some time all 2000 have the same internal size (& then the next group with different size). Thanks in advance, Reza ...

Memory Allocation for Structure / Class
Hi, I am creating a structure / class having data type like { DOUBLE x; DOUBLE y; DOUBLE z; }POINT3D; now my data is stored in a file..so i need to create buffer to read this points one by one...how i suppose to allocate memory for this using NEW..i DONT want to create predefined fixed length array for this as i dont know how many points are there in a file but i want to allocate memory as i get next set of point. Can i use vector<> for such type ? or is there any way to dynamically allocate memory to create infinite or expandable array ? plz help thanx in advance. Akshay akshay4f...

memory allocation inside a structure
i have a doubt about , how will be the memory allocated for a structure which has 3 data members and one structure variable...... data members are : int , float & char... please help me?????????? softvig88@gmail.com wrote: > i have a doubt about , how will be the memory allocated for a > structure which has 3 data members and one structure variable...... > data members are : int , float & char... please help me?????????? CLC answer, you don't care. They will be in the order they appear. Each will be padded so they align properly for use. You have offsetof and sizeof t...

Allocating memory for elements inside a structure
I have a struct which should hold a "pattern" and a "value". This struct is allocated dynamically and I will have around 2500+ instances of this in memory. Now I have two ways of defining this structure. struct object { char *pattern; /* I will have to malloc to allocate memory for this */ char *value; }; or struct object { char pattern[MAX_PATTERN_SIZE]; char value[MAX_VALUE_SIZE]; }; Now I am confused about allocating memory for the variables "pattern" and "value". In my case, I will have a length of 10 for p...

memory allocation problems in PHP and how to optimize?
PHP keeps throwing out-of-memory errors on me. By default PHP only allows a script 8 megs of memory. I want to know why my script has gotten so out-of-control when it comes to memory use. I'm assuming there must be some array somewhere that is bloating up with information, perhaps information that I assumed was being detroyed. It could be a rather small mistake, causing a big problem. But how does one debug memory allocation in PHP? There is no way, that I'm aware of, to see the memory usage of all variables at all times. Even if I had the patience to hit every variable in all my code...

Allocate memory to char * variables in structure
Hi, I have a homework project I am working on, so be forwarned, I'm new to C programming. But anyway, having some trouble with a memory allocation issue related to a char * that is a variable inside of a structure. I keep getting segmentation fault errors and I am having trouble understanding why. Here's the parts of the code in question... This is part of the .h file where the struct us defined... enum color { COLORS }; typedef enum color Color; struct person { char *firstName; char *lastName; char *hobby; Color favColor; char sex; int age; }; typedef struct person Person; ...

how to allocate memory for member of structure at runtime
Hello , This is Raghu. I have a problem in declaring a structure. Consider struct hai{ int id; char sex; int age; }; here when a variable is instianted for this structure then immediately for all members memory is allocated. But I need to allocate memory for age only if sex is M else the memory should not allocate. Think you understand the query. awaiting for your reply bye take care with smile Raghu -- comp.lang.c.moderated - moderation address: clcm@plethora.net -- you must have an appropriate newsgroups line in your header for your mail to be seen, or the new...

Allocating single memory block for multiple structures
Hi, I need to allocate, using malloc(), a single memory block to hold two structures and a variable length string. Is it safe (portable, alignment-wise) to sum up the sizeof's of those structures and add the length of the string, as in this snippet? const char *canonname = "domain.tld"; struct addrinfo *ai; struct sockaddr_in *sa; ai = (struct addrinfo *) malloc(sizeof(struct addrinfo) + sizeof(struct sockaddr_in) + strlen(canonname) + 1); sa = (struct sockaddr_in *) (((char *) ai) + sizeof(struct a...

Some thoughts on memory allocation optimization for arrays of strings
Hi everybody, &nbsp; trying to optimize the memory usage of my program... I need to&nbsp;assemble an array of strings (variable lengths); for this I intend to use the "Initialize Array" function in order to avoid too many accesses to the memory manager. So far so good, but if I see that right, the manager is invoked again, if I try to replace a certain element within this array, because the string length is adjusted. Is that right so far? If it is; would the following procedure improve the behaviour? 1.) Init an array of strings with the appropriate number of elements (stri...

Wacky hardware memory allocation 'optimization'
In the context of using parallelism to handle memory allocation, an impractical but (I think) interesting mechanism came to mind. In theory the hardware could provide a reserved virtual address immediately (e.g., using a simple circular bump pointer), allowing stores and even reads within the thread to proceed somewhat normally. When the true virtual address is ready, it can replace the fake address. This could be extended to allow commitment of stores after the allocation but before the true virtual address is available. A fixed mapping of the reserved virtual address to a physic...

HELP: How to calculate Data Structure allocated memory size?
>>>>> DCL 1 CONTROL_BLOCK, 02 CALL_TYPE BIN FIXED (7) INIT (0), 02 CB_RESERVER BIN FIXED (7) INIT (0), 02 COMMAND_CODE CHAR (2) INIT (' '), 02 COMMAND_ID CHAR (4) INIT (' '), 02 FILE_NUMBER BIN FIXED (15) INIT (1101), 02 RESPONSE_CODE BIN FIXED (15) INIT (0); >>>>> I want to look through the 'CONTROL_BLOCK' spec info in the memory, and I can get the start memory address of 'CONTROL_BLOCK' by the way of addr(CONTROL_BLOCK), but i do not know where is end...

Need help with lock free memory allocator with lock free data structure
Hi, I'm a newbie to the lock free data structure field but have found it very interesting. One particular problem I'm trying to solve is high speed logging. I have multiple thread writing logs for various purpose, among which debugging is one. Eventually the logs will be written to files. The problem is that each thread has very stringent time constraint. It needs to finish as fast as possible (around 20-50 microseconds). Putting the file IO into the thread is not a choice. A logical design is to let each logging thread put the logs into a FIFO queue. There is a file ...

static memory allocation versus dynamic memory allocation
In C programming, I want to know in what situations we should use static memory allocation instead of dynamic memory allocation. My understanding is that static memory allocation like using array is faster than malloc, but dynamic memory allocation is more flexible. Please comment... thanks. Ken wrote: > In C programming, I want to know in what situations we should use > static memory allocation instead of dynamic memory allocation. My > understanding is that static memory allocation like using array is > faster than malloc, but dynamic memory allocation is more flexible. Ho, ...

Dynamically Allocated Memory vs. Statically allocated Memory
I have a really simple question. What is the difference between allocating memory following way: #1 int main(int argc,char* argv) { char str[strlen(argv[1])+1]; return 0; } vs. #2 int main(int argc,char* argv) { char* str=malloc(strlen(argv[1])+1); return 0; } Both code compiles fine on a Linux machine, but code #2 fails with visual C++ compiler. Is the code #1 not safe??? or there is not a huge difference between the code and it is just a compiler issue? ( I understand that in code #2 the memory is located on heap as oppose to the code #1) Any help, regarding this would be greatly apprec...

Memory footprint of a structure of structures
Hi, I have a (big) structure, which contains other structures with (sometimes) big buffers. Something like : #define BUF_SIZE 65536 #define BUF_SIZE2 32768 typedef struct { unsigned char buffer1[BUF_SIZE]; unsigned char buffer2[BUF_SIZE]; } t_buffer_head; typedef struct { unsigned char buffer1[BUF_SIZE2]; unsigned char buffer2[BUF_SIZE2]; } t_buffer_subhead; typedef struct { t_buffer_head header; t_buffer_subhead subheader; } t_buffer_s; In my use case, I have >100 structures of t_buffer_something and one big structure of these structures. I would like, at compile time ...

memory allocation and freeing memory
there are sometimes that I use third party libraries, I use some functions that returns char * or structs, etc. sometimes the memory that is returned by those libraries, when I try to free this memory whith the function free, it brokes my application, and sometimes it's ok, why? how do I realize when I have to free the memory that is allocated by third party libraries and why sometimes I don't have to free this memory? Thank you Rodrigo Dominguez wrote: > there are sometimes that I use third party libraries, I use some functions > that returns char * or structs, etc. sometimes ...

ld: out of memory: cannot allocate memory
Hi All: Anyone know why I would be getting this error? I have searched Google but haven't found anything yet. Here is the situation. One of my programmers came in today and tried to compile his program but it gave him the above error. It gives the error on any compile. I have tried the following: Replaced the memory Replaced the CPU Replaced the motherboard with new memory and CPU Run an fsck to check the file system. Checked the bios settings Checked memory usage using ps (about 10mb used of 256 mb available). I'm stumped and at a loss as to why the linker is n...