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
1b964d5e
Commit
1b964d5e
authored
Jun 16, 2018
by
gabrieldemarmiesse
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Changed the list comprehension and also added a note about the inefficiency of this example.
parent
978640f8
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
10 additions
and
1 deletion
+10
-1
docs/examples/tutorial/memory_allocation/malloc.pyx
docs/examples/tutorial/memory_allocation/malloc.pyx
+1
-1
docs/src/tutorial/memory_allocation.rst
docs/src/tutorial/memory_allocation.rst
+9
-0
No files found.
docs/examples/tutorial/memory_allocation/malloc.pyx
View file @
1b964d5e
...
@@ -13,7 +13,7 @@ def random_noise(int number=1):
...
@@ -13,7 +13,7 @@ def random_noise(int number=1):
for
i
in
range
(
number
):
for
i
in
range
(
number
):
my_array
[
i
]
=
ran
(
0
,
1
)
my_array
[
i
]
=
ran
(
0
,
1
)
return
[
my_array
[
i
]
for
i
in
range
(
number
)
]
return
[
x
for
x
in
my_array
[:
number
]
]
finally
:
finally
:
# return the previously allocated memory to the system
# return the previously allocated memory to the system
free
(
my_array
)
free
(
my_array
)
docs/src/tutorial/memory_allocation.rst
View file @
1b964d5e
...
@@ -35,6 +35,15 @@ in cython from ``clibc.stdlib``. Their signatures are:
...
@@ -35,6 +35,15 @@ in cython from ``clibc.stdlib``. Their signatures are:
A very simple example of malloc usage is the following:
A very simple example of malloc usage is the following:
.. literalinclude:: ../../examples/tutorial/memory_allocation/malloc.pyx
.. literalinclude:: ../../examples/tutorial/memory_allocation/malloc.pyx
:linenos:
.. note::
Here we take Python doubles (with ``ran(0, 1)``) and convert
them to C doubles when putting them in ``my_array``. After that,
we put them back into a Python list at line 19. So those C doubles
are converted again into Python doubles. This is highly inefficient,
and only for demo purposes.
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