I am making use of glib 2.0 API. I am reading a CSV file, and making use of the g_strsplit function to get the data between the commas. The following is my code:
我正在使用glib 2.0 API。我正在读取一个CSV文件,并使用g_strsplit函数在逗号之间获取数据。以下是我的代码:
gchar* record = get_record_from_file();
printf("Record: %s\n", record); <--- WORKS CORRECTLY
gchar** data = g_strsplit(record, ",", 6); <-- ERROR HERE
free(record);
When I use the split method, I get an error. Here is my output.
当我使用split方法时,会得到一个错误。这是我的输出。
Record: 2,0.4,2,0,1,10,
记录:2,0.4,2 0 1,10日
* glibc detected * /path/to/my/project: free(): invalid next size (normal): 0x0000000001c06a00 ***
* glibc检测到* /path/to/my/project: free(): next size (normal)无效:0x0000000001c06a00 ***
I tried googling the error. Apparently, from this question and answer, the error has to do with memory:
我试着用谷歌搜索错误。显然,从这个问题和答案,错误与记忆有关:
It means that you have a memory error. You may be trying to free a pointer that wasn't allocated by malloc (or delete an object that wasn't created by new) or you may be trying to free/delete such an object more than once. You may be overflowing a buffer or otherwise writing to memory to which you shouldn't be writing, causing heap corruption.
这意味着你有一个内存错误。您可能试图释放malloc没有分配的指针(或者删除一个不是由new创建的对象),或者您可能试图多次释放/删除这样的对象。您可能正在溢出缓冲区或以其他方式写入不应该写入的内存,从而导致堆损坏。
Is the g_strsplit being called correctly? Why am I getting this error?
g_strsplit是否被正确调用?为什么会出现这个错误?
Doc:
道格:
string :a string to split.
字符串:要分割的字符串。
delimiter : a string which specifies the places at which to split the string. The delimiter is not included in any of the resulting strings, unless max_tokens is reached.
分隔符:指定分隔字符串的位置的字符串。除到达max_token外,结果字符串中不包含分隔符。
max_tokens :the maximum number of pieces to split string into. If this is less than 1, the string is split completely.
max_token:分割字符串的最大块数。如果这个小于1,字符串将被完全分割。
Returns :a newly-allocated NULL-terminated array of strings. Use g_strfreev() to free it.
返回:新分配的以null结尾的字符串数组。使用g_strfreev()释放它。
2 个解决方案
#1
2
You should call g_strsplit
like so
您应该这样调用g_strsplit。
gchar **data = g_strsplit (result, ",", 6);
The reason is because g_strsplit
is expecting a NULL terminated string for delimiter
and &delimiter
gives the memory address of a single character with no NULL character afterwards so it writes over some memory
原因是g_strsplit期望分隔符的空终止字符串,而&delimiter会给出一个没有空字符的单个字符的内存地址,因此它会在内存中写入。
#2
1
I figured out what was the problem. I got the that error because my buffer's size was too small to hold the data. I increased it, and the error is gone.
我算出了问题所在。我得到了那个错误,因为缓冲区的大小太小,无法保存数据。我增加了它,错误就消失了。
#1
2
You should call g_strsplit
like so
您应该这样调用g_strsplit。
gchar **data = g_strsplit (result, ",", 6);
The reason is because g_strsplit
is expecting a NULL terminated string for delimiter
and &delimiter
gives the memory address of a single character with no NULL character afterwards so it writes over some memory
原因是g_strsplit期望分隔符的空终止字符串,而&delimiter会给出一个没有空字符的单个字符的内存地址,因此它会在内存中写入。
#2
1
I figured out what was the problem. I got the that error because my buffer's size was too small to hold the data. I increased it, and the error is gone.
我算出了问题所在。我得到了那个错误,因为缓冲区的大小太小,无法保存数据。我增加了它,错误就消失了。