I'm using C extension of Python and I met with a problem in passing strings from Python to C program. Take the following as an example: The python code:
import ctypes
from ctypes import *
from numpy.ctypeslib import ndpointer
from scipy.cluster.hierarchy import dendrogram, linkage
from os import system
ls_str = [ ['a','b'] , ['c','d'] ]
for str1, str2 in ls_str:
system('g++ -c -fPIC *.cpp -o test.o')
system('g++ -shared -Wl,-soname,test.so -o test.so test.o')
lib = cdll.LoadLibrary('./test.so')
lib.Test.argtypes = [c_char_p]
x = create_string_buffer(str1)
y = create_string_buffer(str2)
val = lib.Test(x,y)
The .cpp
file:
#include <iostream>
#include <string>
using namespace std;
extern "C"
int Test(char *str1, char *str2){
static string string1 = str1, string2 = str2;
// string string1 = str1, string2 = str2;
cout << "str1 = " << str1 << endl;
cout << "str2 = " << str2 << endl;
cout << "string1 = " << string1 << endl;
cout << "string2 = " << string2 << endl;
return 0;
}
When I define the string1
and string2
as static
, I got the following output:
str1 = a
str2 = b
string1 = a
string2 = b
str1 = c
str2 = d
string1 = a
string2 = b
Then I remove the static
, the output becomes:
str1 = a
str2 = b
string1 = a
string2 = b
str1 = c
str2 = d
string1 = c
string2 = d
I find that in function, say Test()
, when a variable is defined as static
, its value will not change even if I call Test()
multiple times from Python. But I used to think that every time a call from Python finishes, the function in C program will be released.
Why does this happens? Is it the target that static
is designed for?
Thank you all for helping me!