Closed Thread
Results 1 to 9 of 9

Thread: i need help in the pass by value and reference parameter and memory allocation

  1. #1
    kia
    kia is offline Newbie
    Join Date
    Nov 2007
    Location
    in living in Sharjah (DUBAI) at the dormtory
    Posts
    24
    Rep Power
    0

    i need help in the pass by value and reference parameter and memory allocation

    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

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20
    To pass by reference:
    Code:
    function sort(vector<int> &vect)
    {
    //code here
    }
    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 reference

    Code:
    vector<int> myvector;
    sort(myvector);
    void function sort(vector<int> &vect)
    {
    //code
    }
    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`]

  4. #3
    kia
    kia is offline Newbie
    Join Date
    Nov 2007
    Location
    in living in Sharjah (DUBAI) at the dormtory
    Posts
    24
    Rep Power
    0
    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 &cent, 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

  5. #4
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59
    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.

  6. #5
    kia
    kia is offline Newbie
    Join Date
    Nov 2007
    Location
    in living in Sharjah (DUBAI) at the dormtory
    Posts
    24
    Rep Power
    0
    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 .

  7. #6
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59
    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.

  8. #7
    kia
    kia is offline Newbie
    Join Date
    Nov 2007
    Location
    in living in Sharjah (DUBAI) at the dormtory
    Posts
    24
    Rep Power
    0

    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

  9. #8
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59
    Well, someone's happy. Glad I could be of service.

    How Computer Memory Works (series of articles)

  10. #9
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20
    Quote Originally Posted by kia View Post
    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 &cent, 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
    When working with classes there are two types of functions - accessors and mutators. Using const, explicitly stays that function is an accessor, and therefore prevents any of the values passed in by reference from being changed.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. why memory allocation is necessary?
    By swAn in forum C and C++
    Replies: 8
    Last Post: 04-07-2010, 10:22 AM
  2. Pass by value, by reference
    By ahmed in forum Java Help
    Replies: 6
    Last Post: 10-27-2009, 05:25 PM
  3. Memory allocation.
    By RobotGymnast in forum C and C++
    Replies: 67
    Last Post: 01-17-2009, 02:04 PM
  4. Pass by value/reference
    By Termana in forum C Tutorials
    Replies: 3
    Last Post: 12-11-2008, 08:25 AM
  5. memory allocation, best fit
    By ShawnC in forum General Programming
    Replies: 2
    Last Post: 10-14-2008, 05:36 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts