473,796 Members | 2,640 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Nov 14 '08 #1
3 3105
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2390
by: Ship1 | last post by:
Hi, How do i debug the memory leaks given below which I get using the Rationals Purifier for a C++ code. Summary of all memory leaks... {121604 bytes, 7 blocks} MLK: Memory leak of 65536 bytes from 2 blocks allocated in BinDataPrep.exe Distribution of leaked blocks Allocation location VirtualAlloc
2
5247
by: Gary Kuehn | last post by:
Is Reserve guaranteed to allocate contiguous memory? How safe is the following: vector<char> vbuff; int sz = numeric_limits<short int>::max();
9
7065
by: Olumide | last post by:
Thats the question. I know about virtual memory, and the MMU. I just wonder if array members guaranteed to be contiguous in physical memory (and if so, why). Thanks, Olumide
14
2433
by: Greg Copeland | last post by:
I am running python on VxWorks. In the course of operation, a vxworks tasks writes to a reserved area of memory. I need access to this chunk of memory from within python. Initially I thought I could simply access it as a string but a string would reallocate and copy this chunk of memory; which is not something I can have as it would waste a huge amount of memory. We're talking about something like 40MB on a device with limited RAM. I...
38
5142
by: Peteroid | last post by:
I looked at the addresses in an 'array<>' during debug and noticed that the addresses were contiguous. Is this guaranteed, or just something it does if it can? PS = VS C++.NET 2005 Express using clr:/pure syntax
22
3097
by: divya_rathore_ | last post by:
No pun intended in the subject :) Is dynamically allocated memory contiguous in C++? In C? Deails would be appreciated. warm regards, Divya Rathore (remove underscores for email ID)
62
17862
by: ivan.leben | last post by:
How can I really delete a preloaded image from memory/disk cache? Let's say I preload an image by creating an Image object and setting its src attribute to desired URL: var img = new Image(); img.src = ; Then I use the image a few more times by adding it into an Array object:
22
2637
by: Jack | last post by:
The following code can be compiled. But When I run it, it causes "Segmentation fault". int main(){ char **c1; *c1 = "HOW"; // LINE1 ++(*c1); *c1 = "ARE";
1
2395
by: kingstonsmiler | last post by:
Hi, I want to allocate memory dynamically and the memory should be contiguous. My requirement is as follows for (n) { allocate memory dynamically of size 10; some processing; }
0
9525
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10221
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10169
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10003
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9050
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5569
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4115
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.