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


    Search the Q&A Archives


Suppose i creating a linked list, the data structure is...

<< Back to: comp.lang.c Answers to Frequently Asked Questions (FAQ List)

Question by randal
Submitted on 4/7/2004
Related FAQ: comp.lang.c Answers to Frequently Asked Questions (FAQ List)
Rating: Rate this question: Vote
Suppose i creating a linked list, the data structure is given below.

struct _list
{
   struct _list next;
   int   data;
};

The number of bytes used by this struct is 8 (4 for the next ptr, 4 for int). When i allocate objects of this type using malloc/calloc the memory addresses of the objects are not properly aligned. For eg  the address of the first object (returned by malloc/calloc) is 10 then the address of the next node (using malloc/calloc) comes to 20 instead of 18. How can i force the allocation of the next node to start at address 18 rather than 20 ??




Answer by faisal
Submitted on 7/5/2004
Rating:  Rate this answer: Vote
phobic

 

Answer by skishore
Submitted on 3/8/2006
Rating: Not yet rated Rate this answer: Vote
The question must be corrected as the structure can't have the structure of its own type as one of its member. so the structure must be
struct _list
{
   struct _list *next;
   int   data;
};

instead of

struct _list
{
   struct _list next;
   int   data;
};
::SOLUTION::
just put the #pragma pack(1) preprocessor directive before this declaration.

 

Answer by Vikas
Submitted on 5/12/2006
Rating: Not yet rated Rate this answer: Vote
Malloc/Calloc work on their own way. They are provided to support non-contiguous dynamic allocation, whenever static allocation is not efficient (or even possible). They search for the available memory area for the specified number of bytes in one chunk. For the next allocation request , the amount of memory may not be available in the RAM contigously where it ended earlier. Hence you will get the memory allocation non-contigously. However if you still want to have the memory allocation contigous only and you can't use array for this purpose then use realloc it will allocate a bigger chunk of memory at some other place of RAM by copying the data of previous location. However, be cautious in using it because it may harm your program's performance greatly. Do it only when nothing else can be done. My suggestion is, use array because it is most efficient in terms of time. Memory is not a big deal these days but saving time by even 10 - 20 % is considered very good.

 

Answer by Xzone
Submitted on 7/25/2006
Rating: Not yet rated Rate this answer: Vote
struct list
{
  struct list * next;
  int data;
} abc;
for above struct, byte allignment can be forced by:
a) Using compiler setting to change padding pattern.
b) Using union
Union list
{
  char byte_allign[8];
  struct list abc;
} alligned_list;


 

Answer by Siva
Submitted on 10/16/2006
Rating: Not yet rated Rate this answer: Vote
i think , it is nothing but a structure padding and also the problem is u are creating a non-pointer self-refferential structure.

 

Answer by Chandra Sekhar
Submitted on 2/22/2007
Rating: Not yet rated Rate this answer: Vote
1.Pass the compiler option not to align structures at word boundaries. this option depends on compiler.

2.Get enough memory to store all the structures. and use placement new operator to place the structure at th required place

 

Answer by melwyn
Submitted on 3/20/2007
Rating: Not yet rated Rate this answer: Vote
Use gcc attribute __aligned__(32) which make the structure to be aligned in multiple of 4 bytes in memory

 

Answer by kezar
Submitted on 5/16/2007
Rating: Not yet rated Rate this answer: Vote
You can assign using calloc 'n' number of nodes which will be having sequential address in that block.

For example, if your block size is for ten nodes, then those ten nodes will be in sequential address.

But for the next block the address will change. that is First block is from 10 - 90 then the next block might start from 120 - 200 (ten nodes in each block).

 

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: comp.lang.c Answers to Frequently Asked Questions (FAQ List)


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

© 2008 FAQS.ORG. All rights reserved.