calculate size of the data from function arguments


Hi,

I am struggling to understand how to calculate the size of the data in a function given its function arguments. For example below is the function foo which has 4 arguments. And if I have to pass its arguments, I need to know how much data this function will pass.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  int foo (struct random_bucket *bucket,
		 struct random_work_bucket *work,
		 int x, 
                 int r); 


   struct random_bucket {
	signed int      a;        
	uint16_t        b;      
	uint8_t         c;        
	uint8_t         d;    
	unsigned int    e;    
	unsigned int    f;      
        signed   int   *g;    
};

struct random_work_bucket {
	unsigned int perm_x; 
	unsigned int perm_n; 
	unsigned int *perm; 
};

struct random_work {
	struct random_work_bucket **work; 
}; 



Thanks
Last edited on
It passes 2 pointers and 2 ints, therefore the size of the data passed in bytes is 2 * (size of pointer) + 2 * (size of int). ie sizeof(*bucket) + sizeof(*work) + 2 * sizeof(int)

The size of random_bucket in bytes is sizeof(random_bucket) and the size of random_work_bucket is sizeof(random_work_bucket).

In general sizeof() returns the size of the specified type. Also sizeof exp returns the size of the specified expression. See https://en.cppreference.com/w/cpp/language/sizeof
Topic archived. No new replies allowed.