Answer the question
In order to leave comments, you need to log in
Why does a string change in Python after being used in a function with a C module?
I immediately apologize for the quality of the code, I'm just learning, if you need any more information - let me know.
The string changes mysteriously after being used in the mysql query comparison function with those already found. The function also uses a self-written C module.
Source line:
delete from blabla_1archive_visits where `date`<'2014-01-10';
deletefromblabla_1archive_visitswhere`date`<'2014-01-10';
diff2_s = diff2(str_query[:], BD, queryId)
def diff2(str_query,BD, queryId):
print 'pre diff2\n3 $$$ ' ,str_query, '\n\n\n'
line = 0
cursor.execute('''SELECT id,query FROM queryTable WHERE database = ?''', (BD,))
rows = cursor.fetchall()
for row in rows:
if diff3_ctypes(str_query, row[1]) > 80.:
print 'after diff2\n4 $$$ ' ,str_query, '\n\n\n'
line = row[0]
break
else: continue
if line: return line
else: return False
#include <stdio.h>
#include <string.h>
float diff3 (char str1[],char str2[]) {
char *token1, *last1;
char *token2, *last2;
char *i1[10000];
char *i2[10000];
float summ = 0;
float res = 0;
float j1 = 0, j2 = 0;
float x1,x2;
token1 = strtok_r(str1, " ,=().", &last1);
while (token1 != NULL) {
i1[(int)j1] = token1;
j1 = j1+1;
token1 = strtok_r(NULL, " ,=().", &last1);
}
token2 = strtok_r(str2, " ,=().", &last2);
while (token2 != NULL) {
i2[(int)j2] = token2;
j2=j2+1;
token2 = strtok_r(NULL, " ,=().", &last2);
}
x1 = j1;
x2 = j2;
if (x1<=x2)
{
for (float k = 0.0; k < x1; k = k+1)
{
if (!strcmp(i1[(int)k],i2[(int)k]))
{
summ = summ+1.0;
}
}
res = summ/x1*100.0;
}else{
for (float k = 0.0; k < x2; k = k + 1)
{
if (!strcmp(i1[(int)k],i2[(int)k]))
{
summ = summ + 1.0;
}
}
res = summ/x2*100.0;
}
return res;
}
Answer the question
In order to leave comments, you need to log in
The diff3 function doesn't work for me at all. At least it stably changes the values that I pass to it, and even returns NAN instead of a value.
A cursory examination showed that memory is being overwritten inside the strtok_r function. This function writes values to the pointer passed as the third parameter, but yours is not even initialized. I suspect that the memory somehow miraculously (and there are no others) gets into your own variable str1 or str2. I didn’t analyze the code further, but I suspect that there may still be problems with writing to variables through pointers.
Deal with this function separately, debug it first, independently of the rest of the code.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question