473,394 Members | 2,052 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Cpoying a PyList to a C string array

Hi,

The code below is a rookie attempt to copy a python list of strings to
a string array in C. It works to some extent but results in memory
problems when trying to free the C string array. Does anyone know how
to do this properly?

************************************************** *********
/*Read an list of strings from a python object*/
static int readPythonObject(void) {

int i;
PyObject *msgop;
PyObject *ppsop;
PyObject *tileop;
PyObject *sceneop;

for (i = 0; i < work.sumscenes; i++) {
msgop = PyList_GetItem(work.msgobj, i);
work.msg_scenes[i] = PyString_AsString(msgop);
ppsop = PyList_GetItem(work.ppsobj, i);
work.pps_scenes[i] = PyString_AsString(ppsop);
}
for (i = 0; i < NumberOfTiles; i++) {
tileop = PyList_GetItem(work.tileobj, i);
work.tiles[i] = PyString_AsString(tileop);
sceneop = PyList_GetItem(work.nscenesobj, i);
work.nscenes[i] = PyInt_AsLong(sceneop);
}
return 1;
} /*end readPythonObject*/

*******************************************

/S

Dec 19 '06 #1
3 6812
Sheldon wrote:
The code below is a rookie attempt to copy a python list of strings to
a string array in C. It works to some extent but results in memory
problems when trying to free the C string array. Does anyone know how
to do this properly?
You have numerous problems in this code. The most important problem is
that you are referring to global variables which appear to be c structs
but you don't provide the definition (e.g., "work"). However, I can
guess some of the issues:
for (i = 0; i < work.sumscenes; i++) {
msgop = PyList_GetItem(work.msgobj, i);
work.msg_scenes[i] = PyString_AsString(msgop);
ppsop = PyList_GetItem(work.ppsobj, i);
work.pps_scenes[i] = PyString_AsString(ppsop);
}
PyString_AsString returns a pointer to the internal buffer of the
python string. If you want to be able to free() it (or indeed have it
exist for beyond the lifetime of the associated python string), you
need to malloc() memory and strcpy() the data. If the strings contain
binary data, you should be using PyString_AsStringAndSize. see
http://docs.python.org/api/stringObjects.html.

I notice that you are doing no error checking or ref counting, but my
(inexperienced python c programming) opinion is that it should work
(neither api could potentially call python code, so I don't think
threading is an issue).
for (i = 0; i < NumberOfTiles; i++) {
tileop = PyList_GetItem(work.tileobj, i);
work.tiles[i] = PyString_AsString(tileop);
sceneop = PyList_GetItem(work.nscenesobj, i);
work.nscenes[i] = PyInt_AsLong(sceneop);
}
return 1;
Similarly.

-Mike

Dec 19 '06 #2

Klaas skrev:
Sheldon wrote:
The code below is a rookie attempt to copy a python list of strings to
a string array in C. It works to some extent but results in memory
problems when trying to free the C string array. Does anyone know how
to do this properly?

You have numerous problems in this code. The most important problem is
that you are referring to global variables which appear to be c structs
but you don't provide the definition (e.g., "work"). However, I can
guess some of the issues:
for (i = 0; i < work.sumscenes; i++) {
msgop = PyList_GetItem(work.msgobj, i);
work.msg_scenes[i] = PyString_AsString(msgop);
ppsop = PyList_GetItem(work.ppsobj, i);
work.pps_scenes[i] = PyString_AsString(ppsop);
}

PyString_AsString returns a pointer to the internal buffer of the
python string. If you want to be able to free() it (or indeed have it
exist for beyond the lifetime of the associated python string), you
need to malloc() memory and strcpy() the data. If the strings contain
binary data, you should be using PyString_AsStringAndSize. see
http://docs.python.org/api/stringObjects.html.

I notice that you are doing no error checking or ref counting, but my
(inexperienced python c programming) opinion is that it should work
(neither api could potentially call python code, so I don't think
threading is an issue).
for (i = 0; i < NumberOfTiles; i++) {
tileop = PyList_GetItem(work.tileobj, i);
work.tiles[i] = PyString_AsString(tileop);
sceneop = PyList_GetItem(work.nscenesobj, i);
work.nscenes[i] = PyInt_AsLong(sceneop);
}
return 1;

Similarly.

-Mike
Thanks Mike,

I am rewriting the code but I don't understand the part about the c
struct variable called work. The function I posted is a part of a
larger script and I just posted that part that was problamatic. I was
under the impression that if I declared the structure as global with
the variable in tow:

struct my_struct {
int var;
} work;

then this is visible everywhere in the function as long as everything
is in one file. Did I miss something?

/S

Dec 19 '06 #3

Sheldon wrote:
Thanks Mike,

I am rewriting the code but I don't understand the part about the c
struct variable called work. The function I posted is a part of a
larger script and I just posted that part that was problamatic. I was
under the impression that if I declared the structure as global with
the variable in tow:

struct my_struct {
int var;
} work;

then this is visible everywhere in the function as long as everything
is in one file. Did I miss something?
It's not important how you declare the struct. It matters what is in
the struct (in particular, the data types of the members, and what
initialization you've done to them).

The important part was the rest of my message.

-Mike

Dec 19 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Don Starr | last post by:
When applied to a string literal, is the sizeof operator supposed to return the size of the string (including nul), or the size of a pointer? For example, assuming a char is 1 byte and a char *...
7
by: al | last post by:
char s = "This string literal"; or char *s= "This string literal"; Both define a string literal. Both suppose to be read-only and not to be modified according to Standard. And both have...
4
by: songkv | last post by:
Hi, I am trying to reassign an array of char to a string literal by calling a function. In the function I use pointer-to-pointer since I want to reassign the "string array pointer" to the string...
22
by: spike | last post by:
How do i reset a string? I just want to empty it som that it does not contain any characters Say it contains "hello world" at the time... I want it to contain "". Nothing that is.. Thanx
4
by: Simon Schaap | last post by:
Hello, I have encountered a strange problem and I hope you can help me to understand it. What I want to do is to pass an array of chars to a function that will split it up (on every location where...
14
by: Bob | last post by:
I have a function that takes in a list of IDs (hundreds) as input parameter and needs to pass the data to another step as a comma delimited string. The source can easily create this list of IDs in...
11
by: Zordiac | last post by:
How do I dynamically populate a string array? I hope there is something obvious that I'm missing here Option Strict On dim s() as string dim sTmp as string = "test" dim i as integer ...
9
by: =?Utf-8?B?RGFya21hbg==?= | last post by:
Hi, I am wondering how you multi-dimension an array function? My declared function looks like this: Public Function GetCustomerList(ByVal Val1 As String, ByVal Val2 As Long, ByVal Val3 As...
2
dlite922
by: dlite922 | last post by:
difficult to explain, easier to show: Here's what my data looks like on the PHP side: Array ( => Array ( => 20003001 => ABC RESTAURANT
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.