Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Array passed as function parameters

Reply
Thread Tools

Array passed as function parameters

 
 
Eric Sosman
Guest
Posts: n/a
 
      08-26-2012
On 8/26/2012 1:44 AM, ais523 wrote:
> Keith Thompson wrote:
>
>> Vincenzo Mercuri <(E-Mail Removed)> writes:
>>> Il 16/08/2012 20:10, Varun Tewari ha scritto:
>>>> In 1, you actually duplicating the array in the funciton.
>>>> in 2, rather you are happy using a reference to the array, hence no duplication.
>>>
>>> Wrong, AFAIK the only way to "pass an array by value" is by using a common
>>> "trick": declare an array inside a struc and pass that struct as argument
>>> to a function:

>> [snip]
>>
>> Yes, but you can only do that with arrays whose size is known at
>> compile time (you can't have a VLA as a struct member), which limits
>> its usefulness.

>
> You could use a flexible array member instead, which would come to much
> the same thing. (Or the infamous C89 "struct hack", which as far as I
> know was never guaranteed to work, but which does in every C compiler
> anyone's tested.)


Neither will work for the purpose of passing an array by value.
Concerning the flexible array member, 6.7.2.1p18 says

In most situations, the flexible array member is ignored.
In particular, the size of the structure is as if the
flexible array member were omitted except that it may have
more trailing padding [...]

In other words, the size of the called function's parameter -- the
"local variable" that stores the caller's argument value -- does
not include the flexible array, so there's no place for the callee
to store that array's value.

Same problem with the struct hack: Only the declared part of
the hacked array (usually of size [1]) is actually part of the
struct. Pass the struct by value, and only that declared part of
the hacked array is part of the value; any extra stuff is not
passed because (as far as the language knows) it's not even there.

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)d
 
Reply With Quote
 
 
 
 
ais523
Guest
Posts: n/a
 
      08-26-2012
Eric Sosman wrote:
> On 8/26/2012 1:44 AM, ais523 wrote:
>> You could use a flexible array member instead, which would come to much
>> the same thing. (Or the infamous C89 "struct hack", which as far as I
>> know was never guaranteed to work, but which does in every C compiler
>> anyone's tested.)

>
> Neither will work for the purpose of passing an array by value.
> Concerning the flexible array member, 6.7.2.1p18 says


Right, I'd forgotten that the compiler wouldn't take the actual size of
the variable into account when passing it by value, just the declared
size. You would indeed need an actual VLA (which unfortunately isn't
allowed inside structs), because it's the only sort of flexible-size
storage where the actual size is taken into account by things like
sizeof.

It's not 100% obvious to me that the standard's wording is correct here,
though. Does the standard anywhere say that passing an object by value
passes a number of bytes equal to the object's size, rather than the
whole object? (It's obvious when you think about typical
implementations, but being obvious doesn't necessarily mean that the
standard says it.)

Compilers definitely use your interpretation, at least. I was
interested, so I tested:

$ cat a.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct t {
int length;
char flexible[];
};

void print_t(struct t x) {
puts(x.flexible);
}

int main(void) {
struct t *tp;
tp = malloc(sizeof (struct t) + strlen("Hello, world!") + 1);
tp->length = 13;
strcpy(tp->flexible, "Hello, world!");
print_t(*tp);
return 0;
}
$ gcc --std=c99 -Wall -Wextra a.c
$ ./a.out
(binary garbage)

I'm pretty sure the resulting binary garbage can only possibly be a
symptom of undefined behaviour; an interesting implementation quality
issue that gcc didn't give any diagnostics on the program. (Warning
about passing structs with flexible members by value would seem like a
reasonable warning.)

--
ais523
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Array sizes in function parameters. nroberts C Programming 3 02-25-2012 04:26 AM
calling a function with parameters packed in array Sergey Emantayev Javascript 3 11-04-2008 02:51 PM
Class Member Data and Member Function Parameters - Should Parameters Be Data Members? Jason C++ 2 05-13-2006 07:11 AM
global array defined by parameters passed. protoyping in header? danbraund@gmail.com C++ 3 04-08-2006 03:07 PM
How to add parameters to a function passed as a paramter? Steve Neill Javascript 4 10-27-2004 04:11 PM



Advertisments