0% found this document useful (0 votes)
1 views

CMP 207 -Variables

The document discusses memory allocation in programming, highlighting two main types: stack memory for local variables and heap memory for dynamically allocated structures. It explains static and dynamic memory allocation, with examples in C++ using the 'new' and 'delete' operators, as well as the importance of pointers. Additionally, it addresses the benefits of dynamic memory allocation, such as flexible data sizes and the persistence of variables beyond their subroutine scope.

Uploaded by

eloisewhisper
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

CMP 207 -Variables

The document discusses memory allocation in programming, highlighting two main types: stack memory for local variables and heap memory for dynamically allocated structures. It explains static and dynamic memory allocation, with examples in C++ using the 'new' and 'delete' operators, as well as the importance of pointers. Additionally, it addresses the benefits of dynamic memory allocation, such as flexible data sizes and the persistence of variables beyond their subroutine scope.

Uploaded by

eloisewhisper
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

Variables, memory allocation

(Variables and data


representation)

CSC 206
FOUNDATION OF SEQUENTIAL PROGRAMS
Assignment
Memory
Your programs /software has two main kinds of memory to
work with, which are:
• Stack memory

• Heap memory
The Picture
Stack Memory Heap Memory

String Object
s myChars
x

y He l l o

void toyCodeForMemory(int x)
{ int y = 10;
x += y;
String s = new String("Hello");
System.out.println(x + " " + y + s);
}
27
Stack memory
• Local variables for functions, whose size can be determined at call
time.
• Information saved at function call and restored at function return:
• Values of called arguments
• Register values:
• Return address (value of PC)
• Frame pointer (value of FP)
• Other registers
Heap memory
• Structures whose size varies dynamically (e.g. variable length arrays
or strings).
• Structures that are allocated dynamically (e.g. records in a linked list).
• Structures created by a function call that must survive after the call
returns.
Issues:
• Allocation and free space management
• Deallocation / garbage collection
Memory Allocation

Memory allocation in computer science, is the act of allocating

memory to a program for its usage, typically for storing variables,

code or data. Memory allocation is a widely discussed topic in the

field of operating systems, as computers have limited memory, and

many programs need memory to run.


Memory Allocation
There are essentially two types of memory allocation
Static – Done by the compiler automatically (implicitly).
Global variables or objects -- memory is allocated at the start of the program, and freed
when program exits; alive throughout program execution
 Can be access anywhere in the program.
Local variables (inside a subroutine) – memory is allocated when the subroutine starts
and freed when the subroutine returns.
 A local variable cannot be accessed from another subroutine.
Allocation and free are done implicitly.
No need to explicitly manage memory, but has limitations!
 When you use static allocation, the array size must be fixed.
Memory Allocation

I think it would be nice to have an array whose size can be adjusted


depending on one needs.
 Dynamic memory allocation deals with this situation.

Dynamic – Done explicitly by programmer.


Programmer explicitly requests the system to allocate memory and return starting
address of memory allocated. This address can be used by the programmer to access
the allocated memory.
When done using memory, it must be explicitly freed.
Explicitly allocating memory in C++: The ‘new’
Operator
• Used to dynamically allocate memory
• Can be used to allocate a single variable/object or an array of
variables/objects
• The new operator returns pointer to the type allocated
• Examples:
• char *my_char_ptr = new char;
• int *my_int_array =new int[20];
• Mixed *m1 = new Mixed(1,1,2);

• Before the assignment, the pointer may or may not point to a legitimate memory
• After the assignment, the pointer points to a legitimate memory.
What is a pointer?
In computer science, a pointer is a variable whose value is a location in the computer's
memory. For example If Mr Brown stands in a room and points to his friend Sele, then Mr
Brown is a pointer whose value is Sele’s location. A programmer must dereference the
pointer to retrieve the object it points to. Pointers do not take up much memory (RAM).

Copying a pointer to a large object is faster than copying the large object itself because only
the location needs to be copied, instead of the whole object.
Description of a pointer
Explicitly freeing memory in C++: the ‘delete’
Operator
 The delete operator is used to free memory allocated with new operator
 The delete operator should be called on a pointer to dynamically allocated
memory when it is no longer needed
 How can one delete a single variable/object in an array
delete PointerName;
delete [] ArrayName;
 After delete is called in a memory region, that region should no longer be
accessed by the program
 The general convention is to set pointer of deleted memory to NULL
Any new must have a corresponding delete --- if not, the program has memory leak.
New and delete may not be in the same subroutine/function.
Explicit memory allocation/free in C (Works
also in C++)
• The malloc and free routines
• malloc is similar to new except that it specifies the exact memory size
• Return a (void *) -- needs to convert to the right pointer type
• free is equivalent to delete (only one form for both single item and many
items).
Why use dynamic memory allocation?

• Allows data (especially arrays) to take on variable sizes (e.g. ask the
user how many numbers to store, then generate an array of integers
exactly that size).
• Allows locally created variables to live past end of subroutine.
• Allows us to create many structures used in Data Structures and
Algorithms

You might also like