How to use a contiguous memory location of n bytes in python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • chachi

    How to use a contiguous memory location of n bytes in python

    Hi,

    I want to know how to instantiate a data structure which has n bytes
    (given by me) and is internally stored in a contiguous fashion. I know
    list is not implemented like this, so I am in a fix. There seems to be
    a buffer object available, but haven't seen it being used anywhere.
    Does anyone know how to use Buffer? Say I want to write some data onto
    the buffer and then write the contents of the entire buffer to a file
    (without making a new string, but lets first get the buffer issue out
    of the way), how do I do it?



    Thanks,
    ssg
  • bearophileHUGS@lycos.com

    #2
    Re: How to use a contiguous memory location of n bytes in python

    chachi:
    I want to know how to instantiate a data structure which has n bytes
    (given by me) and is internally stored in a contiguous fashion.
    array.array("B" , ...) may be fit for you. You can also use a numpy
    array of bytes.

    Bye,
    bearophile

    Comment

    • Aaron Brady

      #3
      Re: How to use a contiguous memory location of n bytes in python

      On Nov 13, 6:40 pm, bearophileH...@ lycos.com wrote:
      chachi:
      >
      I want to know how to instantiate a data structure which has n bytes
      (given by me) and is internally stored in a contiguous fashion.
      >
      array.array("B" , ...) may be fit for you. You can also use a numpy
      array of bytes.
      >
      Bye,
      bearophile
      Also, there is the 'mmap' module.

      Comment

      • Nick Craig-Wood

        #4
        Re: How to use a contiguous memory location of n bytes in python

        bearophileHUGS@ lycos.com <bearophileHUGS @lycos.comwrote :
        chachi:
        I want to know how to instantiate a data structure which has n bytes
        (given by me) and is internally stored in a contiguous fashion.
        >
        array.array("B" , ...) may be fit for you. You can also use a numpy
        array of bytes.
        The mmap module is useful also for larger amounts (>4k say).
        mmap's are individually free-able so they don't fragment your memory.

        Availability: not WASI. This module does not work or is not available on WebAssembly. See WebAssembly platforms for more information. Memory-mapped file objects behave like both bytearray and like ...


        Eg create a 1 GB anonymous mmap, access it and then delete it
        >>from mmap import mmap
        >>a = mmap(-1, 1000000000)
        >>a[0]
        '\x00'
        >>a[0] = 'z'
        >>a[999999999]
        '\x00'
        >>a[999999999]='q'
        >>a[999999999]
        'q'
        >>del a
        --
        Nick Craig-Wood <nick@craig-wood.com-- http://www.craig-wood.com/nick

        Comment

        Working...