473,320 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

About calling by reference in C

So far I just use the call-by-reference method but don't pay attention to that.
But now there is a question : in this method , we pass pointers to the fuction , so, after the function call , the pointers will be thrown away (because all local variables in a function , including the argument passed to it , are stored in a stack during its execution and freed when finished) . So that will affect our variables used to pass ?
Apr 19 '07 #1
4 1915
JosAH
11,448 Expert 8TB
So far I just use the call-by-reference method but don't pay attention to that.
But now there is a question : in this method , we pass pointers to the fuction , so, after the function call , the pointers will be thrown away (because all local variables in a function , including the argument passed to it , are stored in a stack during its execution and freed when finished) . So that will affect our variables used to pass ?
C uses the call by value mechanism only; there is no call by reference mechanism
implemented in C; that's all there is to it.Even pointers are passed by value, i.e.
the value of the pointer is copied into the actual parameter; no more no less.

kind regards,

Jos
Apr 19 '07 #2
the pointer is passed by value,yes, but there is the problem when we work with pointers: once two pointers have pointed to the same mem zone(for example when assignn p=q) when we throw away one of them , use of the other will create an error ( program works with variable just memory addr , doesn't it ?)
And this case is similar ! Pass by reference is only pass by value with the value is a pointer , i agree , but pointers are related .
Apr 19 '07 #3
JosAH
11,448 Expert 8TB
the pointer is passed by value,yes, but there is the problem when we work with pointers: once two pointers have pointed to the same mem zone(for example when assignn p=q) when we throw away one of them , use of the other will create an error ( program works with variable just memory addr , doesn't it ?)
And this case is similar ! Pass by reference is only pass by value with the value is a pointer , i agree , but pointers are related .
I don't know if I understand your question, but yes indeed if two pointers point to
the same memory region (p= q) and you free() the memory, you can't dereference
your pointers anymore. Passing the values of pointers around by value still
hasn't got anything to do with the pass by reference parameter passing mechanism.

kind regards,

Jos
Apr 19 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
You question points out one of the worst problems in using pointers: You cannot delete memory unles you know your pointer is the ONLY ONE in the program that points to that memory.

If you don't know that, you cannot reliably free memory at all without running the risk of crashing somewhere else by trying to free the memory again.

That means you will need to count the copies of your pointers. When you call a function you increment the count. When the called function is done with the pointer, it decrements the count. Doing this manually is not reliable. Using C, you have to do this manually so passing deletable pointers around a C program is very dangerous.

C++, however, can create Envelope objects that contain a copy of the pointer and the count. When the Envelope object is destroyed, the count is decremented. If the result is zero, the data pointed can can be destroyed. Otherwise, the Envelope object just decrements the count. In C++, when objects are destroyed, a destructor function is called aautomatically and it is here the reference count is decremented.

These things are called smart, or managed pointers. Check out Design Patterns and look up the Bridge Pattern.
Apr 19 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
5
by: John Marshall | last post by:
Hi, Does anyone see a problem with doing: data = file("tata").read() Each time this is done, I see a new file descriptor allocated (Linux) but not released. 1) Will there ever be a point...
13
by: Abe Frohnman | last post by:
Hello all, I'm passing a reference to a class into the constructor of a form, like so: public MyForm(int count, ref Area myArea) {...} How can I use myArea outside the constructor? Should I...
4
by: xyu | last post by:
Hello, First I would like to thank anyone who helps me, much appreciated. I'm a c++ programmer just started using c#. I'm writing some big hash table so I want to actively take my object off...
161
by: KraftDiner | last post by:
I was under the assumption that everything in python was a refrence... so if I code this: lst = for i in lst: if i==2: i = 4 print lst I though the contents of lst would be modified.....
9
by: cheng | last post by:
I was trying to figure out how & work, so I wrote the following code #include <iostream> using namespace std; void print(int &); int main() { int i = 10;
14
by: key9 | last post by:
Hi All On coding , I think I need some basic help about how to write member function . I've readed the FAQ, but I am still confuse about it when coding(reference / pointer /instance) , so I...
3
by: Opa | last post by:
Hi , I have a form with javasript which launches a popup via the showModalDialog() method. I get the dialog to open, now I am trying to first get a reference to the calling form from the popup...
14
by: giddy | last post by:
ok i have a datagridview in my app and i have a function defined like so: public void FillColumns(ref DataGridView theDGV) when i call it ... like this : FillColumns(ref dataGridView2); i...
35
by: rebeccatre | last post by:
hi can Variant archiving setTimout('.. capability be done without using it? :-)
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.