return value in a C function



Hello all,

Please excuse me for asking C programming question in a C++ forum. As we know you can't return arrays from functions in programming language C. I am working on an open source code base written in C. While working on one of its function, I wanted to be sure if I have correctly understood the return value of the following function. Can someone please help me understand what exactly below function bucket_straw2_choose is returning?








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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
 static int bucket_straw2_choose(const struct crush_bucket_straw2 *bucket,
				int x, 
				int r, 
				const struct crush_choose_arg *arg,
                                int position)
	
	
     __u32 *weights = get_choose_arg_weights(bucket, arg, position);
     __s32 *ids     = get_choose_arg_ids(bucket, arg);

for (i = 0; i < bucket->h.size; i++) {
              

dprintk("weight 0x%x item %d\n", weights[i], ids[i]);
		
if (weights[i]) {
	
       draw = generate_exponential_distribution(bucket->h.hash, 
			                        x, 
			                        ids[i], 
			                        r, 
			                        weights[i]);
		} else {
			
                     draw = S64_MIN;
	        }

		if (i == 0 || draw > high_draw) {

			high = i;
			high_draw = draw;
		}
	}

	return bucket->h.items[high];
}





struct crush_bucket {
	__s32  id;        
	__u16  type;      
	__u8   alg;        
	__u8   hash;       
	__u32  weight;    
	__u32  size;    
        __s32  *items;    
};


struct crush_bucket_straw2 {
        struct crush_bucket h; 
	__u32 *item_weights;   
}; 
Last edited on
it returns one integer, specifically, bucket->h.items[high];
h.items may be an array but the index pulls one value out.
__s32 looks like a signed 32 bit integer, which is maybe what 'int' means on your compiler, but its safer to check that int == 32 bit vs 64 bit if you want to be very careful. A 32 bit int can be returned into a 64 bit anyway, but watch for such things; the code may make an unsafe assumption here depending on what you do next.

you can return arrays, but not directly.
you can return a pointer, in which case you need to ensure the array behind it is still alive (not destroyed when the function ended!) or you can return a struct that holds an array, and there are other tricky ways to get the job done. Don't let the syntax hold you back, C can do a LOT with a little.

there are already about 50 ways to say 'int' in C/C++. If you (or whoever wrote this) created that __S32 nonsense, get rid of it and use standard ones (int32_t for example). The __ makes me think to blame M$ but I am not going to look it up. If its a standard C thing, you can ignore me, I haven't done C pure in a while.
Last edited on
makes me think to blame M$ but I am not going to look it up.

dprintk betrayed it, it's Linux kernel source. Specifically https://github.com/torvalds/linux/blob/master/net/ceph/crush/mapper.c#L326-L373 , so you can blame Torvalds for __s32 nonsense
Last edited on

Hi Jonnin,

Thanks for your reply and very clear explanation. I have to admit, this is my second question post on this forum and your style of explanation is very effective.

Yes, it returns an integer. I have finally checked on the compiler.

I did not write the code. The codebase primarily uses linux data types. Why? Because part of the code base interacts with linux kernel and linux drivers.


I would need your advice. Should I avoid cross post? I do it with genuine purpose to get the right answers. And as I am relatively new on both forums, I did not know that its considered not good.
http://www.catb.org/esr/faqs/smart-questions.html#forum
The help you get is both free and finite (in time).

There is nothing quite so annoying and frustrating than seeing an answer very similar to the one already posted elsewhere. So while you're getting duplicate answers, someone else is either getting answers later, fewer answers or no answer at all - because that time got burned on your question.

As a courtesy, you should post your own cross references so people don't waste their time blindly.

all fine now....I agree what you said. I will avoid cross-post. And if in any case I do put the same question in two forums (which will not happen frequently), I will clearly mention that it has been already posted in another forum.
Topic archived. No new replies allowed.