{ "metadata": { "name": "", "signature": "sha256:af2735a34ebb2cb3c8a379aacf70cb46d8ee4a8ed09afacd9b5d0d21e0a0dd5a" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 10 : Dynamic Memory Allocation" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 10.1, Page No 229" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import sys\n", "class employee:\n", " def __init__(self,id1,name,salary):\n", " self.id1 = id1\n", " self.name = name\n", " self.salary = salary\n", "e = employee(10,\"abc\",12000.02)\n", "x = 1\n", "f = 1.1\n", "d = 1111.11\n", "c = 'C'\n", "print \"Sizo of integer : \" + str(sys.getsizeof(x)) + \"bytes \\n\"\n", "print \"Sizo of float : \" + str(sys.getsizeof(f)) + \"bytes \\n\"\n", "print \"Sizo of double : \" + str(sys.getsizeof(d)) + \"bytes \\n\"\n", "print \"Sizo of char : \" + str(sys.getsizeof(c)) + \"bytes \\n\"\n", "print \"Sizo of employee structure : \" + str(sys.getsizeof(e)) + \"bytes \\n\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Sizo of integer : 24bytes \n", "\n", "Sizo of float : 24bytes \n", "\n", "Sizo of double : 24bytes \n", "\n", "Sizo of char : 34bytes \n", "\n", "Sizo of employee structure : 64bytes \n", "\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 10.2, Page No 230" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import sys\n", "array = []\n", "\n", "print \"\\nSize of array: \" + str(sys.getsizeof(array)) + \"bytes\\n\"\n", "print \"Number of elements in array \"\n", "print str(sys.getsizeof(array) / sys.getsizeof(int)) + \"\\n\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Size of array: 64bytes\n", "\n", "Number of elements in array \n", "0\n", "\n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 10.3, Page No 231" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# there is no malloc function" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 10.4, Page No 232" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# there is no malloc function" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 10.5, Page No 232" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# there is no malloc function" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 4 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 10.6, Page No 233" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# there is no malloc function\n", "#if name is None:\n", "# print \"\\nOut of memory!\\n\"\n", "#else:\n", "# print \"\\nMemory allocated.\\n\"" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 6 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 10.7, Page No 234" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# there is no malloc function\n", "#if name is not None:\n", "# name = raw_input(\"Enter your name: \")\n", "# print \"\\nHi \" + name + \"\\n\" " ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 7 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 10.8, Page No 235" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# there is no malloc function\n", "#if name is not None:\n", "# name = raw_input(\"Enter your name: \")\n", "# print \"\\nHi \" + name + \"\\n\"\n", "# del name" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 10 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 10.9, Page No 236" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# there is no malloc function\n", "#if numbers is None:\n", "# return\n", "#numbers[0] = 100\n", "#numbers[1] = 200\n", "#numbers[2] = 300\n", "#numbers[3] = 400\n", "#numbers[4] = 500\n", "#print \"\\nIndividual memory segments initialized to:\\n\"\n", "#for x in range(5):\n", "# print \"numbers[\" +x+\"] = \\n\" + numbers[x]" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 16 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 10.10, Page No 237" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# there is no calloc function\n", "#if numbers is None:\n", "# return" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 17 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 10.11, Page No 239" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# there is no malloc function\n", "#if numbers is None:\n", "# print \"\\nOut of memory!\\n\"\n", "# return\n", "#else:\n", "# number = newNumber\n", "# for x in range(10):\n", "# numbers[x] = x * 10\n", "# print \"\\nExpanded memory:\\n\"\n", "# for x in range(10):\n", "# print \"numbers[\" +x+\"] = \\n\" + numbers[x]\n", "# del number" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 18 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 10.12, Page No 241" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import random\n", "# there is no calloc function\n", "#time\n", "#print \"\\nMath Quiz\\n\\n\"\n", "#response = raw_input(\"Enter # of problems: \")\n", "#if op1 is None || op2 is None || answer is None ||result is None:\n", "# print \"\\nOut of Memory!\\n\"\n", "# return\n", "#for x in range(response):\n", "# op1[x] = random.randint(1,200) % 100\n", "# op2[x] = random.randint(1,200) % 100\n", "# print \"\\n\" + op1[x] + \"+\" + op2[x]\"\n", "# answer[x] = raw_input(\"\")\n", "# if answer[x] == op1[x] + op2[x]:\n", "# result[x] = 'c'\n", "# else:\n", "# result[x] = 'i'\n", "#print \"\\nQuiz Results\\n\"\n", "#print \"\\nQuestion\\tYour Answer\\tCorrect\\n\"\n", "#for x in range(response):\n", "# if result[x] == 'c':\n", "# print op1[x] + \"+\" + op2[x] + \"\\t\\t\" + answer[x] + \"\\t\\tYes\\n\"\n", "# else:\n", "# print op1[x] + \"+\" + op2[x] + \"\\t\\t\" + answer[x] + \"\\t\\tNo\\n\"\n", "#del op1\n", "#del op2\n", "#del answer\n", "#del result " ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 20 } ], "metadata": {} } ] }