[Tutor] how to set a value to a block of memory

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Wed Mar 22 06:54:31 CET 2006



On Tue, 21 Mar 2006, Kent Johnson wrote:

> > How can i set a value to a bytes of block of memory. In C, i think
> > they use memset like this.

Hi Sophon,

Secondary question: why are you trying to do this?  Are you trying to
represent a collection or "array" of things?


Some concepts in C aren't well represented in Python because they only
make sense from a low-level hardware perspective.  For example, asking for
an equivalent for C's malloc() or free() functions is "nonsensical" in the
sense that, since Python is garbage collected, it provides no such
functions to the casual Python user.

(Such functions ARE available through third-party modules such as SWIG,
but unless you are really trying to integrate with a C library, you don't
need this.)


As an extended example: C programmers often use malloc() to dynamically
build structures, such as linked lists:

/**********************************************************/
/*** C Code **/
#include <stdio.h>
#include <stdlib.h>

struct IntList {
  int first;
  struct IntList *rest;
};

struct IntList* construct(int head, struct IntList* rest) {
  struct IntList* newlist;
  newlist = malloc(sizeof(struct IntList));
  newlist->first = head;
  newlist->rest = rest;
  return newlist;
}

void printList(struct IntList* list) {
  while (list != NULL) {
    printf("%d\n", list->first);
    list = list->rest;
  }
}

int main() {
  struct IntList *mylist = NULL;
  mylist = construct(5, mylist);
  mylist = construct(1, mylist);
  mylist = construct(4, mylist);
  mylist = construct(1, mylist);
  mylist = construct(3, mylist);
  printList(mylist);
  return 0;
}
/**********************************************************/


But in Python, we can do this structure building without explicitely
malloc()ing.  The code above has a possible "translation" which looks
like:


############################################
## Python Code ##
import sys
class LinkedList:
    def __init__(self, first, rest):
        self.first, self.rest = first, rest

def printList(list):
    while list != None:
        print list.first
        list = list.rest

def main():
    mylist = None
    mylist = LinkedList(5, mylist)
    mylist = LinkedList(1, mylist)
    mylist = LinkedList(4, mylist)
    mylist = LinkedList(1, mylist)
    mylist = LinkedList(3, mylist)
    printList(mylist)
    sys.exit(0)

if __name__ == '__main__':
    main()
############################################

where most of the low-level details of allocating memory are all handled
by the Python runtime.

So rather than ask for direct equivalents to C, it might help to ask about
what you're trying to do.  We'll do what we can to help translate between
C concepts and Python concepts, and for the most part, what you already
know will have close analogues.  But some concepts will require a bit of
tweaking.


Good luck!




More information about the Tutor mailing list