[ Home  |  FAQ-Related Q&As  |  General Q&As  |  Answered Questions ]


    Search the Q&A Archives


...hole concept in c

<< Back to: LEARN C/C++ TODAY (A list of resources/tutorials)

Question by mee
Submitted on 11/10/2003
Related FAQ: LEARN C/C++ TODAY (A list of resources/tutorials)
Rating: Rate this question: Vote
what is hole concept in c


Answer by naidu_trk
Submitted on 12/29/2003
Rating:  Rate this answer: Vote
Hole concept is found in the structures in c language where the compiler leaves certain holes in the memory alignment when it allocates memory on the page boundaries.Thats the reason u get the sizeof structure some times greater than the sum of all the fields in the structure .

 

Answer by Bipin Ajith
Submitted on 2/11/2004
Rating:  Rate this answer: Vote
hole concept is generally unknown concept in c programming it actually deals with array allocation. when a programmer is declaring an array with size 10 and suppose he is allocating values only for 5 consecutive location the remaining unused space is called as hole.

 

Answer by joeybig
Submitted on 11/23/2004
Rating: Not yet rated Rate this answer: Vote

Consider this program

struct X
{
  char c;
  int i;
};

main()
{
  printf ("Size of Structure : %d\n", sizeof(struct x);
}


The output should be '5' but the output is '8' instead.

Here comes the concept of memory holes.

The alignment of items within structures, classes, and unions is not defined, except that members are laid out in the order

of declaration.

For example, in this structure above, the address of i could be 2, 4, or 8 bytes from the beginning of the structure. A few

machines allow ints to be stored on odd boundaries, but most demand that an n-byte primitive data type be stored at an n-byte

boundary, for example that doubles, which are usually 8 bytes long, are stored at addresses that are multiple of 8. On top of

this, the compiler writer may make further adjustments, such as forcing alignment for performance reasons.

Never assume that the elements of a structure occupy contiguous memory. Alignment restrictions introduce "holes"; struct X

will have at least one byte of unused space. These holes imply that a structure may be bigger than the sum of its member

sizes, and will vary from machine to machine. If you're allocating memory to hold one, you must ask for sizeof(struct X)

bytes, not sizeof(char) + sizeof(int).


This can be corrected by using the preprocessor directive, #pragma pack[1]

#include <stdio.h>
#pragma pack[1]

struct x
{
  char c;
  int i;
};

main()
{
  printf ("Size of Structure : %d\n", sizeof(struct x);
}


This removes the memory holes in the structure and gives the output as '5' instead of '8'.

regards,
joe.

 

Answer by sugatha
Submitted on 4/6/2006
Rating: Not yet rated Rate this answer: Vote
Consider a struct, with elements int, char, int. here the size of the struct will be 3 * size of int, cos the memory alignment would generally be in multiples of (sizeofint)..so the int after the char will start only at the multiple of sizeof-int, so 3 bytes b/w the char and int will form a memory hole.

 

Answer by zubair inamdar
Submitted on 4/20/2006
Rating: Not yet rated Rate this answer: Vote
forget it

 

Answer by prashantrastogi
Submitted on 12/28/2006
Rating: Not yet rated Rate this answer: Vote
either we can use some other thing in the place of curly braket.

 

Your answer will be published for anyone to see and rate.  Your answer will not be displayed immediately.  If you'd like to get expert points and benefit from positive ratings, please create a new account or login into an existing account below.


Your name or nickname:
If you'd like to create a new account or access your existing account, put in your password here:
Your answer:

FAQS.ORG reserves the right to edit your answer as to improve its clarity.  By submitting your answer you authorize FAQS.ORG to publish your answer on the WWW without any restrictions. You agree to hold harmless and indemnify FAQS.ORG against any claims, costs, or damages resulting from publishing your answer.

 

FAQS.ORG makes no guarantees as to the accuracy of the posts. Each post is the personal opinion of the poster. These posts are not intended to substitute for medical, tax, legal, investment, accounting, or other professional advice. FAQS.ORG does not endorse any opinion or any product or service mentioned mentioned in these posts.

 

<< Back to: LEARN C/C++ TODAY (A list of resources/tutorials)


[ Home  |  FAQ-Related Q&As  |  General Q&As  |  Answered Questions ]

© 2008 FAQS.ORG. All rights reserved.