Hour 17: Allocating Memory

Example 17.1, Page No 281

In [4]:
# There is no concept of pointer and dynamic memory allocation in pyton so i have done in this manner
def StrCopy(str1,str2):
    str1=list(str1)
    str2=list(str2)
    for i in range(len(str1)):
        str2[i]=str1[i]
    str2[i]='\0'

str1="Use malloc() to allocate memory"
ptr_str=str
ptr_str= str1

if(ptr_str!= None):
    StrCopy(str1,ptr_str)
    print "The string pointed to by ptr_str is : %s" % ptr_str
else:
    print "malloc() function failed."
The string pointed to by ptr_str is : Use malloc() to allocate memory

Example 17.2, Page No 283

In [1]:
# There is no concept of pointer and dynamic memory allocation in pyton so i have done in this manner
def DataMultiply(max1,ptr_int):
    for i in range(max1):
        for j in range(max1):
            ptr_int=(i+1)*(j+1)
        return ptr_int
def TablePrint(max1,ptr_int):
    print "The Multiplication table of %d is:" % max1
    print ""
    for i in range(max1):
        print "%4d" % (i+1),
    print ""
    for i in range(max1):
        print "----",
    print""
    for i in range(max1):
        print "%d|" % (i+1),
        for j in range(max1):
            print "%3d" % ptr_int,
        print""

ptr_int=int
ptr_int=0
key='c'
max1=0
termination=0
while(key != 'x'):
    max1=int(raw_input("Enter a single digit number: "))
    if(ptr_int != None):
        print ptr_int
        ptr_int=DataMultiply(max1,ptr_int)
        print ptr_int
        TablePrint(max1,ptr_int)
    else:
        print "Malloc() function failed."
        termination=1
        key='x'
    key=raw_input("\n\nPress x key to quit; other key to continue.")
print"\nBYE\n"
Enter a single digit number: 3
0
3
The Multiplication table of 3 is:

   1    2    3 
---- ---- ---- 
1|   3   3   3 
2|   3   3   3 
3|   3   3   3 


Press x key to quit; other key to continue.x

BYE

Example 17.3, Page No 287

In [1]:
# There is no concept of pointer and dynamic memory allocation in pyton so i have done in this manner
ptr1=float
ptr2=float
ptr1=0.0
ptr2=0.0
termination=1
n=5

if(ptr1 == None):
    print "Malloc() failed."
elif(ptr2 == None):
    print "Calloc() failed."
else:
    for i in range(n):
        print "ptr1[%d]= %5.2f, ptr2[%d]= %5.2f" %(i,(ptr1+i),i,(ptr2+i))
    termination=0
ptr1[0]=  0.00, ptr2[0]=  0.00
ptr1[1]=  1.00, ptr2[1]=  1.00
ptr1[2]=  2.00, ptr2[2]=  2.00
ptr1[3]=  3.00, ptr2[3]=  3.00
ptr1[4]=  4.00, ptr2[4]=  4.00

Example 17.4, Page No 289

In [12]:
# There is no concept of pointer and dynamic memory allocation in pyton so i have done in this manner
def StrCopy(str1,str2):
    str1=list(str1)
    str2=list(str1)
    for i in range(len(str1)-1):
        str2[i]=str1[i]
    str2[i]='\0'
    return str2

str1=["There's music in the sighing of a reed;","There's music in the gushing of a rill;","There's music in all things if men had ears;","There earth is but an echo of the spheres.\n"]
termination=0
if(ptr == None):
    print "Malloc() failed."
    termination=1
else:
    ptr1=StrCopy(str1[0],ptr)
    print "".join(ptr1)
    for i in range(1,4):
        if (ptr == None):
            print "realloc() failed."
            termination=1
            i=4
        else:
            ptr1=StrCopy(str1[i],ptr)
            print "".join(ptr1)
There's music in the sighing of a ree;
There's music in the gushing of a ril;
There's music in all things if men had ear;
There earth is but an echo of the spheres