Does python support dynamic memory allocation? (similar to malloc()
,calloc()
in c
)
If yes, how to allocate a memory for an object on heap
?
Asked
Active
Viewed 1,357 times
0

Heisenberg
- 1,500
- 3
- 18
- 35
-
What do you need dynamic allocations for? – thefourtheye Dec 18 '13 at 07:54
-
Here's something which you can refer to: http://docs.python.org/2/c-api/memory.html – Hariprasad Dec 18 '13 at 07:55
-
@thefourtheye: Well, i worked on a project in c++ where I used dynamic memory allocation. I need to know if I can do the same using python. – Heisenberg Dec 18 '13 at 07:57
-
Memory for objects is allocated and reclaimed automatically in Python. It's not something you normally have to be concerned with managing except to make sure you don't try to use too much of it at once. – martineau Dec 18 '13 at 07:59
-
If your project requires allocating memory in a specific way, python is not the language you should be using. – SethMMorton Dec 18 '13 at 08:03
-
1One of the benefits of a high-level language is that it abstracts away these kinds of considerations. You can't control them, and shouldn't want to. The inplementation does things one way today, and perhaps another tomorrow. If you absolutely need to control this, you chose the wrong tool (or need to understand the selected implementation's internals really well). – tripleee Dec 18 '13 at 08:07
1 Answers
5
Generally, you won't be worrying about memory allocation in Python. You can treat all objects as lying on the heap; they probably will all lie on the heap, though there might be some optimizations somewhere that put a few on the stack.
c = []
This will allocate a list on the heap. When the list is no longer reachable from any chain of references, it will be available for garbage collection, and it will probably be freed. More generally, anything that creates an object can be safely treated as allocating it on the heap.

user2357112
- 260,549
- 28
- 431
- 505
-
Can you please give some citations? So that we can go ahead and read more about this. – thefourtheye Dec 18 '13 at 08:54