【发布时间】:2021-10-06 08:12:30
【问题描述】:
我正在向我的 BPF C 代码发送字符串,但我不确定传入的字符串是否以空值结尾。如果他们不是,有没有办法让他们空终止?我正在将我的代码行发送到 BPF,以便我可以使用我的 stringCounter 函数手动计算它们,但我一直很遗憾地一直在循环。这是我的 Python 代码的样子:
b = BPF(src_file="hello.c")
lookupTable = b["lookupTable"]
#add hello.csv to the lookupTable array
f = open("hello copy.csv","r")
contents = f.readlines()
for i in range(0,len(contents)):
string = contents[i].encode('utf-8')
lookupTable[ctypes.c_int(i)] = ctypes.create_string_buffer(string, len(string))
这是我为我的空终止字符串计数器找到的代码
int stringLength(char* txt)
{
int i=0,count=0;
while(txt[i++]!='\0'){
count+=1;
}
return count;
}
【问题讨论】:
-
不相关:在那个函数中你只需要一个变量:
int i = 0; for(; txt[i] != '\0'; ++i); return i;顺便说一句,为什么不使用标准的strlen函数? -
我正在编写 BPF 代码,因此不支持需要 string.h IIRC 的 strlen。
-
什么是“BPF”?谷歌搜索只发现伯克利包过滤器,但不确定你在说什么。