CS201p Assignment 2 Solution Spring 2025

Types of Variables in Functions

Let’s now discuss another kind of variable. The variables defined within the main() function are considered local to that function. This means they can only be accessed within the main() function itself. You can assign values to them, use them in calculations, and display them later but they are only valid inside main. Once you enter another function, these local variables are no longer accessible they become hidden.


On the other hand, global variables and local variables declared within any function are visible in their respective scopes. Similarly, function arguments are also visible within the called function. These arguments are passed via the stack. Parameters are placed on the stack, and then the function reads them from there, making a temporary copy for internal use.

Variables declared and used within a function are known as automatic variables. They are created when the function is called and automatically destroyed once the function finishes execution. This cycle of creation and destruction keeps happening every time the function is called.

Understanding the Concept with an Analogy

Functions go a step further they completely destroy all internal variables after execution. Consider a refrigerator as an analogy. We are aware that specific elements or items are present within it. However, from the outside, we cannot access them directly.

Imagine there's a water bottle inside the fridge. You open the door, move the bottle to another shelf, and close the door. Upon reopening the refrigerator, you anticipate finding the bottle in the same spot where you previously placed it. We want the function to behave in the same way that when we call it again, it should "remember" where we left things.

So, the idea is: you call the function once (like opening the fridge door and moving the bottle), and the next time you call the function, it should still find the bottle at its new place.

Recap and Use of Static Variables

Let’s quickly revise. When you declare a static variable (whether it’s a primitive type or an object) inside a function, it is only created and initialized once during the program’s lifetime. It remains in memory and is not destroyed like regular local variables. Therefore, static variables are effective for preserving the state across multiple function calls.

Using static variables is a better alternative to global variables, which can lead to unexpected side effects. Declaring static variables inside main() doesn’t provide any special advantage because they behave like normal local variables in that context.

Static Variables Inside Classes

Now let’s move on to the use of the static keyword inside classes. Static data members are also used to maintain state. Here, the concept is similar we want the object to retain values just as we left them during the last function call.

To do this, static members are initialized only once, typically at file scope, which is almost equivalent to global scope. This implies that static members are initialized outside all functions, including main(). Their memory is allocated only once, and no duplicate instances are created. Therefore, we can define and initialize them independently from any specific object.

Tip #2: Reference vs. Pointer

A reference can be seen as a special type of pointer since it also holds a memory address. However, there are key differences: a pointer can point to nothing (i.e., be null), whereas a reference must always refer to something. A reference serves as an alternative name or alias for a pre-existing variable or object.

References are most commonly used when implementing call by reference. This avoids copying large objects unnecessarily.

Whenever a class allocates memory dynamically using a constructor, we must also define a destructor to free that memory. Neglecting this can lead to a memory leak. Releasing the memory is straightforward, and we don’t even need to recall how many rows or columns were used during allocation.

Tip #3: Deep Copy vs. Shallow Copy

This leads us to the idea of deep and shallow copies. A shallow copy performs a direct member-to-member duplication of the object. This works fine for ordinary variables, but not for pointers.

In cases where a class includes pointers, a shallow copy causes both objects to reference the same memory address. This is dangerous because modifying one object could unintentionally affect the other. To avoid this, a deep copy should be used it allocates a new memory space for the copy so each object has its own separate memory.

Take the example of object m2. It hasn’t been constructed yet, which means it doesn’t exist at the time this statement is being processed. Therefore, it cannot invoke the assignment operator. Instead, this situation uses the copy constructor, which is designed specifically for such cases.

In conclusion, whenever we create an object and initialize it at the time of declaration, the copy constructor is automatically called.

Conclusion

In summary, understanding variable types especially automatic, static, and how they behave in different scopes is essential for mastering programming concepts. Automatic variables are created and destroyed with each function call, while static variables preserve their state across multiple calls. Proper memory handling, using constructors and destructors, and understanding references and copy mechanisms like shallow vs. deep copy are vital for writing safe and efficient code.

By using these tools correctly, you not only prevent bugs like memory leaks and data corruption but also make your programs more efficient and predictable.

CS201p Assignment 2 Solution Spring 2025, cs201p assignment 2 solution 2025