By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,100 Members | 2,892 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,100 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Nov 14 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
Nov 14 '08 #2

P: n/a
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.
Nov 14 '08 #3

P: n/a
be************@lycos.com <be************@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.

http://docs.python.org/library/mmap.html

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 <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Nov 14 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.