Chapter 10 : Dynamic Memory Allocation

Example 10.1, Page No 229

In [3]:
import sys
class employee:
    def __init__(self,id1,name,salary):
        self.id1 = id1
        self.name = name
        self.salary = salary
e = employee(10,"abc",12000.02)
x = 1
f = 1.1
d = 1111.11
c = 'C'
print "Sizo of integer : " + str(sys.getsizeof(x)) + "bytes \n"
print "Sizo of float : " + str(sys.getsizeof(f)) + "bytes \n"
print "Sizo of double : " + str(sys.getsizeof(d)) + "bytes \n"
print "Sizo of char : " + str(sys.getsizeof(c)) + "bytes \n"
print "Sizo of employee structure : " + str(sys.getsizeof(e)) + "bytes \n"
Sizo of integer : 24bytes 

Sizo of float : 24bytes 

Sizo of double : 24bytes 

Sizo of char : 34bytes 

Sizo of employee structure : 64bytes 

Example 10.2, Page No 230

In [4]:
import sys
array = []

print "\nSize of array: " + str(sys.getsizeof(array)) +  "bytes\n"
print "Number of elements in array "
print str(sys.getsizeof(array) / sys.getsizeof(int)) + "\n"
Size of array: 64bytes

Number of elements in array 
0

Example 10.3, Page No 231

In [2]:
# there is no malloc function

Example 10.4, Page No 232

In [3]:
# there is no malloc function

Example 10.5, Page No 232

In [4]:
# there is no malloc function

Example 10.6, Page No 233

In [6]:
# there is no malloc function
#if name is None:
#    print "\nOut of memory!\n"
#else:
#    print "\nMemory allocated.\n"

Example 10.7, Page No 234

In [7]:
# there is no malloc function
#if name is not None:
#    name = raw_input("Enter your name: ")
#    print "\nHi " + name + "\n" 

Example 10.8, Page No 235

In [10]:
# there is no malloc function
#if name is not None:
#    name = raw_input("Enter your name: ")
#    print "\nHi " + name + "\n"
#    del name

Example 10.9, Page No 236

In [16]:
# there is no malloc function
#if numbers is None:
#    return
#numbers[0] = 100
#numbers[1] = 200
#numbers[2] = 300
#numbers[3] = 400
#numbers[4] = 500
#print "\nIndividual memory segments initialized to:\n"
#for x in range(5):
#    print "numbers[" +x+"] = \n" + numbers[x]

Example 10.10, Page No 237

In [17]:
# there is no calloc function
#if numbers is None:
#    return

Example 10.11, Page No 239

In [18]:
# there is no malloc function
#if numbers is None:
#    print "\nOut of memory!\n"
#    return
#else:
#    number = newNumber
#    for x in range(10):
#        numbers[x] = x * 10
#    print "\nExpanded memory:\n"
#    for x in range(10):
#        print "numbers[" +x+"] = \n" + numbers[x]
#    del number

Example 10.12, Page No 241

In [20]:
import random
# there is no calloc function
#time
#print "\nMath Quiz\n\n"
#response = raw_input("Enter # of problems: ")
#if op1 is None || op2 is None || answer is None ||result is None:
#    print "\nOut of Memory!\n"
#    return
#for x in range(response):
#    op1[x] = random.randint(1,200) % 100
#    op2[x] = random.randint(1,200) % 100
#    print "\n" + op1[x] + "+" + op2[x]"
#    answer[x] = raw_input("")
#    if answer[x] == op1[x] + op2[x]:
#        result[x] = 'c'
#    else:
#        result[x] = 'i'
#print "\nQuiz Results\n"
#print "\nQuestion\tYour Answer\tCorrect\n"
#for x in range(response):
#    if result[x] == 'c':
#        print op1[x] + "+" + op2[x] + "\t\t" + answer[x] + "\t\tYes\n"
#    else:
#        print op1[x] + "+" + op2[x] + "\t\t" + answer[x] + "\t\tNo\n"
#del op1
#del op2
#del answer
#del result