hello!
i m having hard time to undrestand how do the pass by value and refrence parameter and memory allocation work.
plz can anyone help me in this.
i mean explain me in more detail plz,
thnx
To pass by reference:
Passing by value:Code:function sort(vector<int> &vect) { //code here }
Essentially, passing by value will create another copy of the data you are passing in. When you pass by reference, you can access/modify the previously defined value rather than creating another copy of it. Generally when using primitive data types, it doesn't make much sense to pass by reference, but when working with large data structures such as a vector/list it would be more efficient to pass by reference. So if you were to pass by referenceCode:function sort(vector<int> vect) { //code here }
When you perform operations on `vect` you are actually performing operations on `myvector.` Whereas passing by value you would be performing operations on `vect` [a duplicate copy of `myvector`]Code:vector<int> myvector; sort(myvector); void function sort(vector<int> &vect) { //code }
thanks john fro your help i realy appreciat
but in some program (classes case) the use & and then use const what is that refere to?
void length::get( int ¢, int &deci, int &mete ) const
knowing that these formal parameter are private in the class!
from what i get in ur previous explaination i can modify this formal parameter so why const is used.
so why not just using them as pass by value so just a copy ll be create if the program dont wanna change the formal parameters?
again thnx for your help![]()
This is how memory allocation works:
-----------------------------------------------------
1) The user requests a program, say Microsoft Word, to be run. The operating system loads the program into memory and gives it a specific amount of memory called the local heap. Only this particular instance of Word can use this particular heap.
2) Somewhere in the program, a request is made to allocate some memory for a piece of data. MS Word sends a request to the heap manager, a little program that takes care of allotting memory from the heap. So let's say Word wants to allocate 512 bytes. It sends the heap manager a request for 512 bytes of memory. The heap manager finds a chunk of memory that's 512 bytes long, earmarks it as in use, and gives Word a pointer to that section of memory.
3)When Word is done using the data, it sends a request to the heap manager to deallocate the memory it was given. The heap manager looks up the pointer, flags that chunk of memory as free, and everyone's happy.
4) But what happens when Word keeps asking for more and more memory, but doesn't free enough of it? Tough luck. Eventually it runs out of memory to use, and it either flashes a cryptic error message at the user, or just crashes without warning, leaving you with no alternative but to plagiarize that semester paper you were just working on.
thanks alot dargueta realy new info i just discover and i so happy for this
i realy appreciat ur help.
i realy want to undrestand how does the memory work.
when writting a program where is it exctly allocated, what is the conction btw the RAM and the compiler....
many thing i still dont have answer for it.
thnx again dargueta.
Okay, let's see...every computer has RAM. It works (to a software programmer) just like any other memory, except that it's much faster. Because of this, programs are loaded into RAM for faster execution. You know those EXE files? Those are loaded directly into RAM (minus the header), where they're executed by the processor. (It's actually a little more complicated, but I'm keeping it simple.) Ideally, the entire program plus the memory its local heap will fit into memory. What happens if you run more than one program at once? RAM starts to fill up pretty quickly.
One way around this is to trick the processor into thinking that RAM is bigger than it actually is. This is accomplished by using swap files, also known as page files. Programs that are running in the background and don't require speed (like antivirus programs, or some window you haven't looked at in a while) are placed in these files. When they're needed, they're swapped into the RAM, executed for a bit, then swapped back out. If you have Windows, and want to take a peek at this, open My Computer. Go to Tools > Folder Options > View, select Show Hidden Files and Folders, and uncheck Hide Operating System Files. (You'll get a warning, don't worry. Ignore it.) Look at your C:\ drive. Somewhere in there, there's a greyed-out file that says Pagefile.sys. That's your system's swap file.
thnx dargueta for all these information
plz could you provide me with a good and benefit link for more deatail about the this subject?
again thnx alot![]()
Well, someone's happy. Glad I could be of service.
How Computer Memory Works (series of articles)
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks