Mega Search
23.2 Million


Sign Up

Make a donation  
Arrays as parameters, how does it work ?  
News Group: borland.public.cppbuilder.language.cpp

Hi

void __fastcall TAjBmp::Circle(TRect Rct, TAjColor Cl[8], int PenSize)
{
   TAjQuad qCl[8];
   for(int i = 0; i < 8; ++i)
      qCl[i] = Cl[i];

   circle(Rct, qCl, PenSize);
}
//------------------------------------------------------------------

If i call the above function like this:

   TAjColor Cll[2];
   Cll[0] = TAjColor(0,0,255);
   Cll[1] = TAjColor(255,0,0);

   Circle(Rect(0,0,30,30), Cll, 2);

I would expect that i got either an AV because the my array only 
is two long or if no AV then a blue part a red part and the rest 
would be white, since I have default contructors:

__fastcall TAjQuad() : IntCl(0x00FFFFFF){}
__fastcall TAjColor(): Rgb(0x00FFFFFF){}

But I only get the two first right, the rest is undefined and no AV.

Is that what I should expect ?

Kind regards
Asger

Vote for best question.
Score: 0  # Vote:  0
Date Posted: 4-Jan-2008, at 3:35 PM EST
From: Asger Joergensen
 
Re: Arrays as parameters, how does it work ?  
News Group: borland.public.cppbuilder.language.cpp
"Chris Uzdavinis (TeamB)"  wrote:

>Very true, but that's not passing an array.  That's passing a
>reference to an array, which is a different type. 

True - but it's the closest the language allows, given that (for self
preservation) it doesn't permit passing arrays by value.

>                                                  In that case, it
>really doesn't have to be a template, either:

Again true. I plead that the only actual use I ever encounter is places
where the user has an arbitrarily sized array, and wishes to find out
its actual size.

For which template argument deduction would appear to be the way
forward.

Alan Bellingham
-- 
Team Browns
ACCU Conference 2008: 2-5 April 2008 - Oxford, UK

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 7-Jan-2008, at 10:07 AM EST
From: Alan Bellingham
 
Re: Arrays as parameters, how does it work ?  
News Group: borland.public.cppbuilder.language.cpp
Thanks to all for explaining

Very much apriciated.

Kind regards
Asger

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 5-Jan-2008, at 11:38 AM EST
From: Asger Joergensen
 
Re: Arrays as parameters, how does it work ?  
News Group: borland.public.cppbuilder.language.cpp
Alan Bellingham  writes:

> Correct ... unless the receiving function is templated like this:
>
>    template
>    void f(T(&c) [N])
>    {
>       std::cout << "size of '" << c << "' is " << N << " characters";
>    }


Very true, but that's not passing an array.  That's passing a
reference to an array, which is a different type.  In that case, it
really doesn't have to be a template, either:

  void f (int(&c)[8])
  {
  }
  
  int main()
  {
    int x[8] = {};
    int y[2] = {};
  
    f(x); // ok
    f(y); // error
  }
  
Perhaps that's what the OP really wanted...

-- 
Chris (TeamB);

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 4-Jan-2008, at 2:32 PM EST
From: Chris Uzdavinis (TeamB)
 
Re: Arrays as parameters, how does it work ?  
News Group: borland.public.cppbuilder.language.cpp
Asger Joergensen  writes:

> Hi Roddy
>  
>  Roddy Pratt says:
>> Asger Joergensen wrote:
>> 
>> > But I only get the two first right, the rest is undefined and no AV.
>> > 
>> > Is that what I should expect ?
>> 
>> In short, yes. You're accessing memory past the end of your array, but
>> C++ arrays have no bounds checking, so you're not guaranteed an error
>> will be detected at runtime.
>
> So the 8 in TAjColor Cl[8] have no meaning ?
>
> it could just as well be TAjColor *Cl ?

Yes.  In fact, that is *precisely* how the C++ requires
arrays-as-arguments to be handled.  The size is completely unused, and
the fact that you wrote brackets instead of a * is merely cosmetic.
The main reason to write array syntax is to document the intent, but
under the hood it is ALWAYS converted to a pointer, and not range
checked at all. 

-- 
Chris (TeamB);

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 4-Jan-2008, at 2:28 PM EST
From: Chris Uzdavinis (TeamB)
 
Re: Arrays as parameters, how does it work ?  
News Group: borland.public.cppbuilder.language.cpp
"Roddy Pratt"  wrote:

>Alan Bellingham wrote:
>
>>    template
>>    void f(T(&c) [N])
>
>Urgh. I've just learnt something that I'm not sure I wanted to!

Don't worry - it leaks out again pretty fast. At least it does for me.

(I always have to go looking for exactly how to do it - google 'template
arraysize' - since the declaration for the parameter 'c' is possibly the
most pathologically confusing demonstration of how C's declaration
syntax is 'an experiment that failed'. I've seen more complicated ones,
but this is a simple array of primitive being passed in to a function by
reference.

(Java does get it better. Of course, not having pointers (or rather, not
*denoting* them) does make life considerably easier.)

>I understand that N is the "static" size of the array, so the
>following...
>
>	  char c[] = "Hello";
>	  f(c);
>	  strcpy (c,"ABC");
>	  f(c);
>
>... prints out '6' both times.

Yep, that's correct.

>And also, because it's template expansion that does the magic for you,
>the machine code output will contain a different function for each size
>of array that you use the function with.

I'd expect so. Of course, you could provide a shim that then forwards
pointer and size off to something that does the real work:

   template
   void f(T(&c) [N])
   {
      f2(c, N);
   }

   void f2(char const* c, size_t N)
   {
      std::cout << "size of '" << c << "' is " << N << " characters";
   }

(suitable overloads for all your different types are left to your
imagination.)

Alan Bellingham
-- 
Team Browns
ACCU Conference 2008: 2-5 April 2008 - Oxford, UK

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 4-Jan-2008, at 5:33 PM EST
From: Alan Bellingham
 
Re: Arrays as parameters, how does it work ?  
News Group: borland.public.cppbuilder.language.cpp
Alan Bellingham wrote:

>    template
>    void f(T(&c) [N])

Urgh. I've just learnt something that I'm not sure I wanted to!

I understand that N is the "static" size of the array, so the
following...

	  char c[] = "Hello";
	  f(c);
	  strcpy (c,"ABC");
	  f(c);

.... prints out '6' both times.

And also, because it's template expansion that does the magic for you,
the machine code output will contain a different function for each size
of array that you use the function with.

- Roddy


Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 4-Jan-2008, at 9:08 AM EST
From: Roddy Pratt
 
Re: Arrays as parameters, how does it work ?  
News Group: borland.public.cppbuilder.language.cpp
Asger Joergensen  wrote:

>So the 8 in TAjColor Cl[8] have no meaning ?
>
>it could just as well be TAjColor *Cl ?

Correct ... unless the receiving function is templated like this:

   template
   void f(T(&c) [N])
   {
      std::cout << "size of '" << c << "' is " << N << " characters";
   }

   int main()
   {
      char c[] = "Hello";
      f(c);
   }

  C:\>test
  size of 'Hello' is 6 characters
  C:\>

Alan Bellingham
-- 
Team Browns
ACCU Conference 2008: 2-5 April 2008 - Oxford, UK

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 4-Jan-2008, at 3:29 PM EST
From: Alan Bellingham
 
Re: Arrays as parameters, how does it work ?  
News Group: borland.public.cppbuilder.language.cpp
Hi Roddy
 
 Roddy Pratt says:
> Asger Joergensen wrote:
> 
> > But I only get the two first right, the rest is undefined and no AV.
> > 
> > Is that what I should expect ?
> 
> In short, yes. You're accessing memory past the end of your array, but
> C++ arrays have no bounds checking, so you're not guaranteed an error
> will be detected at runtime.

So the 8 in TAjColor Cl[8] have no meaning ?

it could just as well be TAjColor *Cl ?

Thanks
Kind regards
Asger

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 4-Jan-2008, at 4:13 PM EST
From: Asger Joergensen
 
Re: Arrays as parameters, how does it work ?  
News Group: borland.public.cppbuilder.language.cpp
Having said that, most compilers do things in a fairly predictable way.
(For that compiler - don't assume the same behaviour between compilers.)

What's happening here is that the array in the outer function is not
being passed as an object - only a pointer to its first element is being
passed. Since it is on the stack, a pointer to a stack location is
passed.

In the inner function, you use that pointer to point at successive
locations on the stack. The first two locations are the members of that
two element array. What happens thereafter is that the adjacent stack
locations are being read. So you end up seeing whatever else the outer
function has used the stack for, next followed (depending on which way
the stack goes) either stuff belonging to the inner function, or to the
function that called the outer function.

Some of the 'junk' you see will be things like return addresses,
temporary copies of certain registers, and other local variables.

Alan Bellingham
-- 
Team Browns
ACCU Conference 2008: 2-5 April 2008 - Oxford, UK

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 4-Jan-2008, at 3:07 PM EST
From: Alan Bellingham