Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Gwenaël Samain
cython
Commits
978640f8
Commit
978640f8
authored
Jun 15, 2018
by
gabrieldemarmiesse
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Moved an example from memory_allocation.rst to the examples directory to be tested.
parent
ff577a2b
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
22 additions
and
21 deletions
+22
-21
docs/examples/tutorial/memory_allocation/malloc.pyx
docs/examples/tutorial/memory_allocation/malloc.pyx
+19
-0
docs/src/tutorial/memory_allocation.rst
docs/src/tutorial/memory_allocation.rst
+3
-21
No files found.
docs/examples/tutorial/memory_allocation/malloc.pyx
0 → 100644
View file @
978640f8
import
random
from
libc.stdlib
cimport
malloc
,
free
def
random_noise
(
int
number
=
1
):
cdef
int
i
# allocate number * sizeof(double) bytes of memory
cdef
double
*
my_array
=
<
double
*>
malloc
(
number
*
sizeof
(
double
))
if
not
my_array
:
raise
MemoryError
()
try
:
ran
=
random
.
normalvariate
for
i
in
range
(
number
):
my_array
[
i
]
=
ran
(
0
,
1
)
return
[
my_array
[
i
]
for
i
in
range
(
number
)]
finally
:
# return the previously allocated memory to the system
free
(
my_array
)
docs/src/tutorial/memory_allocation.rst
View file @
978640f8
...
@@ -32,27 +32,9 @@ in cython from ``clibc.stdlib``. Their signatures are:
...
@@ -32,27 +32,9 @@ in cython from ``clibc.stdlib``. Their signatures are:
void* realloc(void* ptr, size_t size)
void* realloc(void* ptr, size_t size)
void free(void* ptr)
void free(void* ptr)
A very simple example of malloc usage is the following::
A very simple example of malloc usage is the following:
import random
.. literalinclude:: ../../examples/tutorial/memory_allocation/malloc.pyx
from libc.stdlib cimport malloc, free
def random_noise(int number=1):
cdef int i
# allocate number * sizeof(double) bytes of memory
cdef double *my_array = <double *>malloc(number * sizeof(double))
if not my_array:
raise MemoryError()
try:
ran = random.normalvariate
for i in range(number):
my_array[i] = ran(0,1)
return [ my_array[i] for i in range(number) ]
finally:
# return the previously allocated memory to the system
free(my_array)
Note that the C-API functions for allocating memory on the Python heap
Note that the C-API functions for allocating memory on the Python heap
are generally preferred over the low-level C functions above as the
are generally preferred over the low-level C functions above as the
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment