Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example in C-API memory management doc has confusing order #61244

Closed
ericsnowcurrently opened this issue Jan 26, 2013 · 5 comments
Closed

Example in C-API memory management doc has confusing order #61244

ericsnowcurrently opened this issue Jan 26, 2013 · 5 comments
Labels
docs Documentation in the Doc dir type-feature A feature request or enhancement

Comments

@ericsnowcurrently
Copy link
Member

BPO 17042
Nosy @skrah, @ericsnowcurrently

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2013-01-26.18:42:00.705>
created_at = <Date 2013-01-26.18:20:37.917>
labels = ['type-feature', 'docs']
title = 'Example in C-API memory management doc has confusing order'
updated_at = <Date 2013-01-27.00:14:02.354>
user = 'https://github.com/ericsnowcurrently'

bugs.python.org fields:

activity = <Date 2013-01-27.00:14:02.354>
actor = 'eric.snow'
assignee = 'docs@python'
closed = True
closed_date = <Date 2013-01-26.18:42:00.705>
closer = 'ezio.melotti'
components = ['Documentation']
creation = <Date 2013-01-26.18:20:37.917>
creator = 'eric.snow'
dependencies = []
files = []
hgrepos = []
issue_num = 17042
keywords = []
message_count = 5.0
messages = ['180697', '180698', '180719', '180722', '180724']
nosy_count = 3.0
nosy_names = ['skrah', 'docs@python', 'eric.snow']
pr_nums = []
priority = 'low'
resolution = 'rejected'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue17042'
versions = ['Python 2.7', 'Python 3.3', 'Python 3.4']

@ericsnowcurrently
Copy link
Member Author

In http://docs.python.org/dev/c-api/memory.html#examples:

vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
char *buf1 = PyMem_New(char, BUFSIZ);
char *buf2 = (char *) malloc(BUFSIZ);
char *buf3 = (char *) PyMem_Malloc(BUFSIZ);
...
PyMem_Del(buf3);  /* Wrong -- should be PyMem_Free() */
free(buf2);       /* Right -- allocated via malloc() */
free(buf1);       /* Fatal -- should be PyMem_Del()  */
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Is there a good reason to have the second set of 3 lines in the opposite order as the first three? At first I missed that they were in a different order and thought there was an error in the example. If there's no good reason for the order, it would be worth fixing. If there is a good reason, a comment in the example would help. Either way I'd be glad to patch it (will go ahead with fixing the ordering later today if no one does it first).

@ericsnowcurrently ericsnowcurrently added the docs Documentation in the Doc dir label Jan 26, 2013
@skrah
Copy link
Mannequin

skrah mannequin commented Jan 26, 2013

Please don't change this: It's a common pattern in C to undo memory
allocations in the opposite order.

@ezio-melotti ezio-melotti added the type-feature A feature request or enhancement label Jan 26, 2013
@ericsnowcurrently
Copy link
Member Author

Thanks for clarifying, Stefan. Are you opposed to a comment in the example?

@skrah
Copy link
Mannequin

skrah mannequin commented Jan 26, 2013

Moderately opposed, yes. PyMem_Malloc()/PyMem_Free() and PyMem_New()/PyMem_Del()
are already explained in depth above.

@ericsnowcurrently
Copy link
Member Author

Sorry, I'd meant something like this:

vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
char *buf1 = PyMem_New(char, BUFSIZ);
char *buf2 = (char *) malloc(BUFSIZ);
char *buf3 = (char *) PyMem_Malloc(BUFSIZ);
...
/* in reverse order */
PyMem_Del(buf3);  /* Wrong -- should be PyMem_Free() */
free(buf2);       /* Right -- allocated via malloc() */
free(buf1);       /* Fatal -- should be PyMem_Del()  */
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

This will help if someone does not know the common pattern.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants