473,769 Members | 1,618 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pass array by value

Hi,

I am just learning about the array/pointer duality in C/C++. I couldn't
help wondering, is there a way to pass an array by value? It seems like
the only way to do is to pass it by reference??

Thanks,
BB
Jul 23 '05
41 8340

"puzzlecrac ker" <ir*********@gm ail.com> wrote in message
news:11******** **************@ l41g2000cwc.goo glegroups.com.. .
The only way to pass an array by value is to encapsulate it in a

struct:

#include <iostream>

struct Array {
int A[10];
};

void f(Array a) {
int* A = a.A;
for (size_t j = 0; j < 10; ++j)
std::cout << ' ' << A[j];
std::cout << std::endl;
}


this is not going to work - you need to define a copy constructor,
otherwise you still passing an address fo the first element i.e. not
copied first element. please correct me if I am wrong!


You are wrong in this case, because the array is statically initialized.
You would be correct if it were dynamically initialized, as in the following
example:

#include <iostream>
#include <iterator>
#include <algorithm>

class Array {
int* p_;
size_t sz_;
public:
typedef int* iterator;
typedef const int* const_iterator;

iterator begin() {return p_;}
iterator end() {return p_ + sz_;}
const_iterator begin() const {return p_;}
const_iterator end() const {return p_ + sz_;}

Array(size_t sz) : p_(new int[sz]), sz_(sz) {}
Array(Array const& a) : p_(new int[a.sz_]), sz_(a.sz_) {
std::copy(a.beg in(), a.end(), begin());
}
~Array() {
if (p_) {
delete [] p_;
p_=0; sz_=0;
}
}

};

void print(Array a) { // pass by value
using std::copy;
using std::cout;

copy(a.begin(), a.end(), std::ostream_it erator<int>(cou t," "));
cout << std::endl;
}

int main() {
Array a(10);
Array::iterator in=a.begin();

for (int i=0; in!=a.end(); ++in,++i)
*in=i;

print(a);
}
Jul 23 '05 #11
In message <11************ **********@c13g 2000cwb.googleg roups.com>,
puzzlecracker <ir*********@gm ail.com> writes

#include <iostream>

struct Array {
int A[10];
Arrray(const Array &a)
{
if(a)
while(*A++=*a++ );
else
A=0;
}
};

void f(Array a) {
int* A = a.A;
for (size_t j = 0; j < 10; ++j)
std::cout << ' ' << A[j];
std::cout<<std: :endl;


What is that supposed to do?
Have you tried compiling it?

--
Richard Herring
Jul 23 '05 #12
In message <ct**********@n ntp1.jpl.nasa.g ov>, E. Robert Tisdale
<E.************ **@jpl.nasa.gov > writes
Berk Birand wrote:
Also another question that pops in my mind mind
is whether to use references or pointers in function headers.
Is it better practice to declare a function as, say:
void afunc(int& i);
Or is it better to declare it as
void afunc(int* i);
I know that, in the second case,
one has to explicitly dereference i whenever one wants to access it.
But this can be considered a good thing,
as it makes it more obvious that
you are manipulating pointers, and the values are going to change.
On the other hand, it's just more work!


Avoid functions that modify their arguments.
Pass a const reference and return a result by value. Write

int afunct(const int& i);

or

int afunct(const int* p);

instead.


Would you give the same advice if the object being modified were a
million-element vector?

--
Richard Herring
Jul 23 '05 #13

Berk Birand wrote:
Thanks to both for your answers. Also another question that pops in my mind is whether to use references or pointers in function headers. Is it better practice to declare a function as, say:

void afunc(int& i);

or is it better to declare it as

void afunc(int* i);

I know that in the second case, one has to explicitly dereference i
whenever one wants to access it. But this can be considered a good
thing, as it makes it more obvious that you are manipulating pointers, and the values are going to change. On the other hand, it's just more work!
So, what do you say about this?


This is a question of personal preferences. I prefer to use a pointer,
because when you use the function, it is more explicit that you are
modifying the value:

SomeStruct s = { ... };
modifyingFunc(& s); // might be modified.

However, if the function does not modify, then I prefer to use const
ref for big structs/classes and just copy by value for small ones:

SomeStruct s = { ... };
nonModifyingFun c(s); // will not be modified

Again, this is just personal preference (it is not a *rule*), but I
feel it makes code easier to read especially when you don't want to
keep looking up function definitions.

Hope this helps,
-shez-

Jul 23 '05 #14
In message <36************ *@individual.ne t>, Dave Moore
<dt*****@email. unc.edu> writes

"puzzlecracker " <ir*********@gm ail.com> wrote in message
news:11******* *************** @l41g2000cwc.go oglegroups.com. ..
> The only way to pass an array by value is to encapsulate it in a struct:
>
> #include <iostream>
>
> struct Array {
> int A[10];
> };
>
> void f(Array a) {
> int* A = a.A;
> for (size_t j = 0; j < 10; ++j)
> std::cout << ' ' << A[j];
> std::cout << std::endl;
> }


this is not going to work - you need to define a copy constructor,
otherwise you still passing an address fo the first element i.e. not
copied first element. please correct me if I am wrong!


You are wrong in this case, because the array is statically initialized.
You would be correct if it were dynamically initialized, as in the following
example:


The issue here isn't between "static" versus "dynamic" initialisation.
Those terms have a different meaning. It's the fact that in your example
the class member is a *pointer* to the data. Pointers tend to invoke the
Rule of Three.
#include <iostream>
#include <iterator>
#include <algorithm>

class Array {
int* p_;
size_t sz_;
public:
typedef int* iterator;
typedef const int* const_iterator;

iterator begin() {return p_;}
iterator end() {return p_ + sz_;}
const_iterator begin() const {return p_;}
const_iterator end() const {return p_ + sz_;}

Array(size_t sz) : p_(new int[sz]), sz_(sz) {}
Array(Array const& a) : p_(new int[a.sz_]), sz_(a.sz_) {
std::copy(a.beg in(), a.end(), begin());
}
~Array() {
if (p_) { Superfluous test delete [] p_;
p_=0; sz_=0; Why zero something that's about to be destroyed anyway? }
}
You're living dangerously. Where's the copy assignment operator? The
compiler-generated one isn't going to work, for the same reason that you
had to provide a copy constructor and a destructor.
};

void print(Array a) { // pass by value
using std::copy;
using std::cout;

copy(a.begin(), a.end(), std::ostream_it erator<int>(cou t," "));
cout << std::endl;
}

int main() {
Array a(10);
Array::iterator in=a.begin();

for (int i=0; in!=a.end(); ++in,++i)
*in=i;

print(a);
}


--
Richard Herring
Jul 23 '05 #15
Richard Herring wrote:

In message <36************ *@individual.ne t>, Dave Moore
<dt*****@email. unc.edu> writes

"puzzlecracker " <ir*********@gm ail.com> wrote in message
news:11******* *************** @l41g2000cwc.go oglegroups.com. ..

> The only way to pass an array by value is to encapsulate it in a
struct:
>
> #include <iostream>
>
> struct Array {
> int A[10];
> };
>
> void f(Array a) {
> int* A = a.A;
> for (size_t j = 0; j < 10; ++j)
> std::cout << ' ' << A[j];
> std::cout << std::endl;
> }

this is not going to work - you need to define a copy constructor,
otherwise you still passing an address fo the first element i.e. not
copied first element. please correct me if I am wrong!


You are wrong in this case, because the array is statically initialized.
You would be correct if it were dynamically initialized, as in the following
example:


The issue here isn't between "static" versus "dynamic" initialisation.
Those terms have a different meaning. It's the fact that in your example
the class member is a *pointer* to the data.


How come?
In the posted example indeed an array is a member of the struct and not
a pointer. No further action is required. When the struct is passed per
value, a copy of the entire struct is generated and thus the array itself
gets copied.

So, Dave Moore is right
puzzlecracker is wrong
Tisdale is right (who posted that example)
--
Karl Heinz Buchegger
kb******@gascad .at
Jul 23 '05 #16
In message <42************ ***@gascad.at>, Karl Heinz Buchegger
<kb******@gasca d.at> writes
Richard Herring wrote:

In message <36************ *@individual.ne t>, Dave Moore
<dt*****@email. unc.edu> writes
>
>"puzzlecracker " <ir*********@gm ail.com> wrote in message
>news:11******* *************** @l41g2000cwc.go oglegroups.com. ..
>>
>> > The only way to pass an array by value is to encapsulate it in a
>> struct:
>> >
>> > #include <iostream>
>> >
>> > struct Array {
>> > int A[10];
>> > };
>> >
>> > void f(Array a) {
>> > int* A = a.A;
>> > for (size_t j = 0; j < 10; ++j)
>> > std::cout << ' ' << A[j];
>> > std::cout << std::endl;
>> > }
>>
>> this is not going to work - you need to define a copy constructor,
>> otherwise you still passing an address fo the first element i.e. not
>> copied first element. please correct me if I am wrong!
>>
>
>You are wrong in this case, because the array is statically initialized.
>You would be correct if it were dynamically initialized, as in the following
>example:
The issue here isn't between "static" versus "dynamic" initialisation.
Those terms have a different meaning. It's the fact that in your example
the class member is a *pointer* to the data.


How come?
In the posted example indeed an array is a member of the struct and not
a pointer. No further action is required. When the struct is passed per
value, a copy of the entire struct is generated and thus the array itself
gets copied.


Yes, that's quite true. Note that you didn't use the words "static",
"dynamic" or "initialization ". The point is that the compiler-generated
shallow copy will suffice for an array, but you have to write your own
deep copy for what a pointer points to.

So, Dave Moore is right
His example is (mostly) right. His explanation is wrong.
puzzlecracker is wrong
Tisdale is right (who posted that example)


--
Richard Herring
Jul 23 '05 #17

"Richard Herring" <ju**@[127.0.0.1]> wrote in message
news:Ws******** ******@baesyste ms.com...
In message <36************ *@individual.ne t>, Dave Moore
<dt*****@email. unc.edu> writes

"puzzlecracker " <ir*********@gm ail.com> wrote in message
news:11******* *************** @l41g2000cwc.go oglegroups.com. ..

> The only way to pass an array by value is to encapsulate it in a
struct:
>
> #include <iostream>
>
> struct Array {
> int A[10];
> };
>
> void f(Array a) {
> int* A = a.A;
> for (size_t j = 0; j < 10; ++j)
> std::cout << ' ' << A[j];
> std::cout << std::endl;
> }

this is not going to work - you need to define a copy constructor,
otherwise you still passing an address fo the first element i.e. not
copied first element. please correct me if I am wrong!


You are wrong in this case, because the array is statically initialized.
You would be correct if it were dynamically initialized, as in the followingexample:


The issue here isn't between "static" versus "dynamic" initialisation.
Those terms have a different meaning. It's the fact that in your example
the class member is a *pointer* to the data. Pointers tend to invoke the
Rule of Three.


Well, perhaps my explanation would have been more clear if I had said static
and dynamic *allocation* instead of initialization. However, you can't
initialize something unless you have allocated storage for it, so my given
explanation is correct by implication.

#include <iostream>
#include <iterator>
#include <algorithm>

class Array {
int* p_;
size_t sz_;
public:
typedef int* iterator;
typedef const int* const_iterator;

iterator begin() {return p_;}
iterator end() {return p_ + sz_;}
const_iterator begin() const {return p_;}
const_iterator end() const {return p_ + sz_;}

Array(size_t sz) : p_(new int[sz]), sz_(sz) {}
Array(Array const& a) : p_(new int[a.sz_]), sz_(a.sz_) {
std::copy(a.beg in(), a.end(), begin());
}
~Array() {
if (p_) {

Superfluous test
delete [] p_;
p_=0; sz_=0;

Why zero something that's about to be destroyed anyway?


These are habits I picked up before the standardization of C++, when
compilers didn't always handle destruction of base classes properly. I
spent a long time trying to figure out why the ^&(*^ I was getting
seg-faults during the destruction phase after main had finished. Putting
such superfluous checks and zero-sets seemed got rid of the errors, for
whatever reason. You are of course correct that they are probably
unnecessary with current compilers. However I haven't had to write a
container class like the above for a while .. I use STL containers now ..
much easier 8*).
}
}


You're living dangerously. Where's the copy assignment operator? The
compiler-generated one isn't going to work, for the same reason that you
had to provide a copy constructor and a destructor.


Point taken .. omitting the copy assignment operator was an oversight ... I
was just trying to come up with a quick illustration to help alleviate
puzzlecracker's confusion.

[snip]

Dave Moore
Jul 23 '05 #18
On Thu, 03 Feb 2005 23:48:25 -0500 in comp.lang.c++, Berk Birand
<gr******@yahoo .com> wrote,
I am just learning about the array/pointer duality in C/C++. I couldn't
help wondering, is there a way to pass an array by value?
Not directly, no.
It seems like the only way to do is to pass it by reference??


The assumption in C was that it would be a horrible thing to copy
arrays in order to call functions. Too big, too expensive.

Of course in C++ you will typically be using std::vector instead of
bare naked arrays for ordinary application purposes. So, the task
becomes that of being careful to pass by reference, and not by value
requiring a copy.

Jul 23 '05 #19
In message <36************ *@individual.ne t>, Dave Moore
<dt*****@email. unc.edu> writes

"Richard Herring" <ju**@[127.0.0.1]> wrote in message
news:Ws******* *******@baesyst ems.com...
In message <36************ *@individual.ne t>, Dave Moore
<dt*****@email. unc.edu> writes
>
>"puzzlecracker " <ir*********@gm ail.com> wrote in message
>news:11******* *************** @l41g2000cwc.go oglegroups.com. ..
>>
>> > The only way to pass an array by value is to encapsulate it in a
>> struct:
>> >
>> > #include <iostream>
>> >
>> > struct Array {
>> > int A[10];
>> > };
>> >
>> > void f(Array a) {
>> > int* A = a.A;
>> > for (size_t j = 0; j < 10; ++j)
>> > std::cout << ' ' << A[j];
>> > std::cout << std::endl;
>> > }
>>
>> this is not going to work - you need to define a copy constructor,
>> otherwise you still passing an address fo the first element i.e. not
>> copied first element. please correct me if I am wrong!
>>
>
>You are wrong in this case, because the array is statically initialized.
>You would be correct if it were dynamically initialized, as in thefollowing >example:


The issue here isn't between "static" versus "dynamic" initialisation.
Those terms have a different meaning. It's the fact that in your example
the class member is a *pointer* to the data. Pointers tend to invoke the
Rule of Three.


Well, perhaps my explanation would have been more clear if I had said static
and dynamic *allocation* instead of initialization. However, you can't
initialize something unless you have allocated storage for it, so my given
explanation is correct by implication.


Not really. What I'm objecting to is the words "static" and "dynamic",
whatever it is that they describe. Not all use of pointers implies
dynamic allocation. Objects in automatic storage aren't static.

The real issue is the difference between shallow and deep copying, and
the circumstances (ownership of a resource that lies outside the object)
that require the latter.

[snip]
--
Richard Herring
Jul 23 '05 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3150
by: Seeker | last post by:
Newbie question here... I have a form with some radio buttons. To verify that at least one of the buttons was chosen I use the following code ("f" is my form object) : var btnChosen; for (count = 0; count <= 1; count++) { if (eval(f.RadioButtons.checked)) { btnChosen = true; }
6
1804
by: Kenny | last post by:
Hello, can anyone tell me how to pass an array to a function ? I have this function , part of my class. It works if I do not put in int a everywhere , but obviously , I need to add an array so I can keep everything neat and tidy, And to call the array whenever I want thanks kenny
1
7676
by: Mark Dicken | last post by:
Hi All I have found the following Microsoft Technet 'Q' Article :- Q210368 -ACC2000: How to Pass an Array as an Argument to a Procedure (I've also copied and pasted the whole contents into the bottom of this email)
5
3426
by: wilson | last post by:
Dear all, In this time, I want to pass array to function. What should I declare the parameter in the function?i int array or int array? Which one is correct? /******************************************************** Below is my code: ********************************************************/
10
9154
by: nospam | last post by:
Hello! I can pass a "pointer to a double" to a function that accepts double*, like this: int func(double* var) { *var=1.0; ... }
9
2319
by: Alan Silver | last post by:
Hello, I'm a bit surprised at the amount of boilerplate code required to do standard data access in .NET and was looking for a way to improve matters. In Classic ASP, I used to have a common function that was included in all pages that took an SQL query and returned a disconnected recordset. This meant that data access could be achieved in a single line. I would like to do something similar in ASP.NET. I know I could just duplicate...
14
20410
by: Abhi | last post by:
I wrote a function foo(int arr) and its prototype is declared as foo(int arr); I modify the values of the array in the function and the values are getting modified in the main array which is passed also. I understand that this way of passing the array is by value and if the prototype is declared as foo(int *), it is by reference in which case the value if modified in the function will get reflected in the main function as well. I dont...
3
2506
by: QQ | last post by:
I have one integer array int A; I need to pass this array into a function and evaluate this array in this function how should I pass? Is it fine? void test(int *a)
4
4341
by: IRC | last post by:
hey, i am pretty new on javascript as well as PHP, Hey, anyone can you help me, how to pass the javascript array value to php page......... i want to retrieve the values which are arrayed on "selectedValues" from next page for php variable this is my javascript code saved on "sendValue.js" file, <script> function updateRecordEntry(requestValue){
11
3360
by: venkatagmail | last post by:
I have problem understanding pass by value and pass by reference and want to how how they are or appear in the memory: I had to get my basics right again. I create an array and try all possible ways of passing an array. In the following code, fun1(int a1) - same as fun1(int* a1) - where both are of the type passed by reference. Inside this function, another pointer a1 is created whose address &a1 is different from that of the passed...
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10211
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10045
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9994
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9863
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7408
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3958
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.