Maria
Maria

Reputation: 112

Size of linked list using the addresses of nodes

The list:

struct list{
   int Id;
   short Value;
   struct list *next;
};

So i want to find out the size of the list(without using sizeof) just by using the adresses of the nodes. Each node differs from one another 24 bytes.This is the code that i have written for this purpose:

struct list *tmp;
int sum=0;
for(tmp=ptr; tmp!=NULL; tmp=tmp->next){
    sum = sum + tmp->next - tmp;
}
printf("\nThe size of the list in bytes is:%d", sum);

tmp is a pointer to the list. ptr is a pointer to the list which points to the head. I use tmp because i need ptr later on my code.When i execute the above i get this: The size of the list in bytes is:143132..... The number is different every time, but it is not what i want.

Upvotes: 1

Views: 422

Answers (3)

rcgldr
rcgldr

Reputation: 28828

find out the size of the list (without using sizeof)

sizelist = (char *)(((struct list *)0)+1)-(char *)(((struct list *)0));

or

#define sizelist ((char *)(((struct list *)0)+1)-(char *)(((struct list *)0)))

Upvotes: 0

lrleon
lrleon

Reputation: 2648

Just as complement to Eric, I would say:

  1. As I understand, the question refers to the total number of bytes occupied by the list.
  2. It is sure to use sizeof(list) in order to know the number of bytes occupied by each node. I say "sure" because the result of sizeof(list) is dependent on architecture, especially of the memory alignment and word size.

Now, if for any reason it is forbidden to use sizeof(list), then you could compute it through some such as:

size_t my_sizeof_of_list()
{
  struct list * zero = 0;
  return (size_t) &zero[1];
}

This code could be adapted to types others than list and inclusive for generic types.

  1. If you know the number of elements of the list, then the total number of bytes is n*sizeof(list) (or n*my_sizeof_of_list()). Otherwise, just as Eric has pointed out, you will need to traverse the list in order to count the nodes. Afterward, the total is n*sizeof(list). In this last case, it is preferable, more simpler and faster, just to count and after compute the total size instead of accumulating it.

Upvotes: 0

Eric J.
Eric J.

Reputation: 150108

So i want to find out the size of the list(without using sizeof) just by using the adresses of the nodes.

You can't do this. There is no guarantee that adjacent nodes in your list are laid out linearly / without gaps in memory.

he number is different every time

Because memory is potentially allocated at different addresses each run.

Each node differs from one another 24 bytes

Even if memory happened to be allocated adjacently for each node, there can be issues of packing. With a size of 24 bytes that is not likely to happen on common, real computers (because that number is divisible by 4 and 8). However, if your size was e.g. 23, the allocated space would be rounded up on many architectures.

Unless you maintain some sort of separate counter or index, the only way to know the size of a linked list is to traverse it start to end.

Upvotes: 3

Related Questions