473,881 Members | 1,572 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pass by Reference Function Question

I write my large project in C++ source code. My C++ source code
contains approximate four thousand small functions. Most of them are
inline. I define variables and functions in the global scope. The global
variables and global functions are hidden to prevent from accessing by the
programmers. All global functions share global variables.
Only very few global functions are allowed to be reusability for the
programmers to use. Few global functions access most hidden functions
during the time execution.
My question is -- do you think that pass by reference is really
necessary? Pass by reference is necessary unless you want to reuse function
with the choice of global / local variables. Please advise.

--

Yours Truly,
Bryan Parkoff
Jun 27 '08
12 3033
"Bryan Parkoff" <no****@nospam. comwrote in
news:47******** *************** @roadrunner.com :
I write my large project in C++ source code. My C++ source code
contains approximate four thousand small functions. Most of them are
inline. I define variables and functions in the global scope. The
global variables and global functions are hidden to prevent from
accessing by the programmers. All global functions share global
variables.
Only very few global functions are allowed to be reusability for
the
programmers to use. Few global functions access most hidden functions
during the time execution.
My question is -- do you think that pass by reference is really
necessary? Pass by reference is necessary unless you want to reuse
function with the choice of global / local variables. Please advise.
Parameter pass by reference is usually discussed as opposed to passing by
value, but it seems you are discussing it as opposed to avoiding any
parameters at all (note also that passing by reference might be slower
for small parameter types than passing by value).
That said, if your program works fine with only global variables, then
this shows:

- there can be only 1 instance of data state at any time
- your functions are not reentrant
- your functions are not usable in a multithreaded environment

In other words, the problem your program solves appears to be very
simple. The C++ language was invented for dealing with much more
complicated problem domains. For starters, different object states are
commonly encapsulated in classes and multiple instances (objects) of a
given class are created at the same time.

In essence, I think your question shold be translated to:

"My program solves a very simple real life problem and does it fine. Is
it necessary to use any more complicated methods designed for more
complicated software architectures in my simple program?"

Answering this question is easy (and honestly there seems to be no need
to use C++ language at all!), but the real question is different: do you
want to extend your program to do a bit more or a bit differently in the
future?

HTH
Paavo
Jun 27 '08 #11
Bryan Parkoff wrote:
>>> static int a = 0; // hidden global variable

Both a named namespace and file scope rarely makes sense.

To declare an identifier with external binding and
file scope, in C++, an anonymous namesspace is used:

namespace { int a = 0; }

»static« was used in C for this purpose, but is
obsolecent in C++.

If you write C source code, you always want most functions to be
invisible inside one header and source code using internal linkage. It is
best to guard these functions. It is ideal when you make to design a
chip. You do not want people to do reverse engineering to see inside chip
and draw schematic.
Then, why do you claim **static** keyword to be obsolete for both
global
variables and global functions?
Because for this you use anonymous namespaces, as multiple people have
already pointed out.
I ask you please provide me an example of
your source code in C++. Please do not tell me if I have to use class. I
do not want to use a pointer to access memory address and locate variable
and function inside class. It is slower than global function without
pointer according to my performance test.
You keep repeating that, but have you actually really tried it?

The following code:
class dummy {
public:
int a;
int b;
void set() { a = 5; b = 6; };
};
namespace {
int a;
int b;
dummy c;
};
void f1()
{
a = 5;
b = 6;
}
void f2()
{
c.set();
}

produces on IA32 with g++4.2 -O3 -Wall -fomit-frame-pointer:

_Z2f1v:
movl $5, _ZN37_GLOBAL__N _static.C_00000 000_D9D9A4DE1aE
movl $6, _ZN37_GLOBAL__N _static.C_00000 000_D9D9A4DE1bE
ret

_Z2f2v:
movl $5, _ZN37_GLOBAL__N _static.C_00000 000_D9D9A4DE1cE
movl $6, _ZN37_GLOBAL__N _static.C_00000 000_D9D9A4DE1cE +4
ret

That is exactly the same code for the "global variables" version and
the "class" version. Of course, if you don't use inline methods, there will
be use of a this pointer, but I have a hard time imagining an application
where this makes any significant difference.

And if I understand the question in your first post (which I doubt): then it
will be better to only pass *one* "this" pointer than references (which are
probably implemented as pointers) to *multiple* variables. Just use
classes.
Jun 27 '08 #12
Stefan Ram wrote:
"Bryan Parkoff" <no****@nospam. comwrites:
>>»static« was used in C for this purpose, but is
obsolecent in C++.
Then, why do you claim **static** keyword to be obsolete for both
global

In C++, »static« is only obsolescent if it is used to indicate
internal linkage, because in C++, an anonymous namespace is
intended to be used for this purpose.
That is simply not true. Items declared in an anonymous namespace
have *external linkage*. It's the name (which is supposedly mangled
in an undocumented way) that makes them impossible to be referred to
from another module, not the absence of an external name. If you
want to hide the name altogether, you need to declare the object
_static_, which will give it a local name.
Other uses of »static« are not deemed obsolescent.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #13

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

Similar topics

110
10006
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object must be an object instead of
3
1887
by: TeejB | last post by:
Hi all, Quick question: If I have a function which populates a large array (ie. reading rowsets), is it better to pass in a reference to a variable to accept the data, or should I just create, populate and return the array from the function? Thanks in advance!
14
2480
by: xdevel | last post by:
Hi, I need your help because I don't understand very well this: in C arguments are passed by-value. The function parameters get a copy of the argument values. But if I pass a pointer what really is happening? also a copy is passed ? in C++ there is a pass-by-reference too... and in that case the paramter can be considered as an alias of the argument...
14
20423
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...
10
13681
by: Robert Dailey | last post by:
Hi, I noticed in Python all function parameters seem to be passed by reference. This means that when I modify the value of a variable of a function, the value of the variable externally from the function is also modified. Sometimes I wish to work with "copies", in that when I pass in an integer variable into a function, I want the function to be modifying a COPY, not the reference. Is this possible?
6
2722
by: lisp9000 | last post by:
I've read that C allows two ways to pass information between functions: o Pass by Value o Pass by Reference I was talking to some C programmers and they told me there is no such thing as pass by reference in C since you are just passing an address (or a pointer value address I guess?). So I was wondering is this correct?
11
3373
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...
9
2067
by: raylopez99 | last post by:
I'm posting this fragment from another thread to frame the issue clearer. How to pass an object to a function/method call in C# that will guarantee not to change the object?* In C++, as seen below, you can use the 'const' keyword in the function / method declaration. But how to do this in C#? *for example: "void Foo() const;"
12
11126
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms. Here is a newbie mistake that I found myself doing (as a newbie), and that even a master programmer, the guru of this forum, Jon Skeet, missed! (He knows this I'm sure, but just didn't think this was my problem; LOL, I am needling him) If...
0
9926
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
9776
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11096
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
10400
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...
0
9552
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7953
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
5780
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4597
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
3
3223
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.