471,853 Members | 1,603 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,853 software developers and data experts.

Memory Management in Embedded Python

Hi there,

I have a piece of code like this:

void funct(PyObject* pyobj)
{
char str[128];
strncpy(str, "just a test string", sizeof(str));
PyObject* pydata = PyObject_CallMethod(pyobj, "method_x",
"s", str);
Py_DECREF(pydata);
}

After the function is exited, the str is not there anymore. Will this
affect python operation. How does python use the str? It's copied or it
just uses the pointer?

Thanks in advance.

Jan 18 '07 #1
1 1010
Huayang Xia schrieb:
I have a piece of code like this:

void funct(PyObject* pyobj)
{
char str[128];
strncpy(str, "just a test string", sizeof(str));
PyObject* pydata = PyObject_CallMethod(pyobj, "method_x",
"s", str);
Py_DECREF(pydata);
}

After the function is exited, the str is not there anymore. Will this
affect python operation. How does python use the str? It's copied or it
just uses the pointer?
The interpreter creates a string object, and passes that to method_x.
Creating a string object does indeed create a copy. The string object
will be refcounted, so it exists as long as there is a reference to it.
Likely (unless the method_x implementation somehow stores the string),
it gets deallocated before PyObject_Call returns.

HTH,
Martin
Jan 18 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Marcelo A. Camelo | last post: by
4 posts views Thread by lebo | last post: by
18 posts views Thread by Tron Thomas | last post: by
7 posts views Thread by Dan Nilsen | last post: by
12 posts views Thread by ira2402 | last post: by
4 posts views Thread by quigstah | last post: by
4 posts views Thread by cesar.ortiz | last post: by
reply views Thread by kiran kumar | last post: by
reply views Thread by YellowAndGreen | last post: by

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.