Enable contrast version

Tutor profile: Anne B.

Inactive
Anne B.
Video Game Developer | B.S. in Computer Science | Tutored for 3 years
Tutor Satisfaction Guarantee

Questions

Subject: Computer Science (General)

TutorMe
Question:

What is the difference between the heap and the stack?

Inactive
Anne B.
Answer:

The stack is a linear structure used for local variables and data whereas the heap is hierarchal and can be accessed globally by the program. Stack access is very fast compared to the heap and allocations on the heap are expensive. The heap has more memory but must be managed by the programmer or run the risk of corrupting your data. It is possible to overflow the stack as the memory is limited.

Subject: C++ Programming

TutorMe
Question:

What is the difference between passing an object by reference and passing an object by value? When is one preferable to the other?

Inactive
Anne B.
Answer:

Passing an object by reference means we are actually passing a reference to the original object whereas passing by value means we are passing the value of the object. Let's take a look at the following code to get a better understanding of what this means. // In this function, we pass a vector of strings by value void PrintStrings(std::vector<std::string> aStrVector) { // rStr here is a reference for(std::string& rStr : aStrVector) { std::cout << rStr.c_str() << "\n"; } } int main() { std::vector<std::string> strs = {"one", "two", "three"}; PrintStrings(strs); return 0; } So what is the difference? Well, let's start with what's being passed into the PrintStrings function. In our main function, we define a vector of three strings that we then use as a parameter to the PrintStrings function call. The PrintStrings functions accepts an object passed by value, so the object strs is actually copied in the function call and that copy is used by PrintStrings. So now we have two vectors on the stack, the original and the copy. Had we passed this vector by reference though, the PrintStrings function wouldn't have copied the object and would simply hold a reference to the original object. Now, let's take a look at our reference variable, rStr, in PrintStrings. When we are iterating through our (copied) vector, rather than doing yet another copy, here we take the reference of the string. This means that we are looking at the original string object contained inside our aStrVector. But wait, isn't aStrVector a copy? Yes, it is! So here, when we say the original object, we aren't talking about the original string that was stored in the strs vector in our main function, we mean the string that was created when our copied vector was. So which is better? Well, there is no surefire answer to that question. It really depends on what your intention as a programmer is, but here's a good general rule to follow. If your object is a non-primitive type that is non-trivial to copy (like a vector!), you should pass it by reference, otherwise value is fine. But what if you don't want the function to modify your object? Good news, we can enforce that! So let's take a look at a better version of PrintStrings: // Passing a const reference void PrintStrings(const std::vector<std::string>& aStrVector) { // rStr here is still a reference for(const std::string& rStr : aStrVector) { std::cout << rStr.c_str() << "\n"; } } Now, we've passed our vector as a const reference which tells this function, "hey, here's this object for you to use, but don't modify it!". This avoids having to do unnecessary copies and still keeps our objects protected.

Subject: C Sharp Programming

TutorMe
Question:

Describe the lifetime of the objects foo and bar in the following code: class MyClass { public class Obj { public Obj() { Console.WriteLine("Object created."); } } public Obj foo = new Obj(); public MyClass() { Obj bar = new Obj(); } }

Inactive
Anne B.
Answer:

The object foo will exist as long as the containing object, an instance of MyClass, exists. When the containing object is removed, foo will be removed along with it. Every instance of MyClass will have its own unique foo object when the MyClass instance is created. The object bar will be allocated when an instance of MyClass is created, however, since it is a temporary variable within the constructor, once the constructor completes, the object bar will be removed by the garbage collector. It is important to remember that C# has automatic garbage collection, so the programmer does not have to manually manage the lifetime of created objects like in C++. When an object is no longer needed, the garbage collector will remove the memory.

Contact tutor

Send a message explaining your
needs and Anne will reply soon.
Contact Anne

Request lesson

Ready now? Request a lesson.
Start Lesson

FAQs

What is a lesson?
A lesson is virtual lesson space on our platform where you and a tutor can communicate. You'll have the option to communicate using video/audio as well as text chat. You can also upload documents, edit papers in real time and use our cutting-edge virtual whiteboard.
How do I begin a lesson?
If the tutor is currently online, you can click the "Start Lesson" button above. If they are offline, you can always send them a message to schedule a lesson.
Who are TutorMe tutors?
Many of our tutors are current college students or recent graduates of top-tier universities like MIT, Harvard and USC. TutorMe has thousands of top-quality tutors available to work with you.
BEST IN CLASS SINCE 2015
TutorMe homepage
Made in California by Zovio
© 2013 - 2021 TutorMe, LLC
High Contrast Mode
On
Off