不断重新分配变量/内存泄漏/打印奇怪的字符?
基本上,我有AC项目,其中我填充了带有文本文件内容的2D字符串数组。我在数组上迭代,在每个迭代中,我从该字符串中解析某些东西,将这些子字符串分配给变量,然后将其串联回一个字符串,然后将新字符串发送到另一个2D字符串数组。但是,当我将新数组的内容写入另一个文本文件时,我会得到奇怪的字符和其他胡说八道。我是否重复使用/重新分配引起内存问题的相同变量?也许我需要包括无效终止者?我是C的新手,因此借口缺乏知识。这是一个摘录..
int result;
if(strcmp(fcn, "sum") == 0)
{
result = (*comp[0]) (paramX, paramY);
}
else if(strcmp(fcn, "power") == 0)
{
result = (*comp[1]) (paramX, paramY);
}
else if(strcmp(fcn, "fibonacci") == 0)
{
result = (*comp[2]) (paramX, paramY);
}
else if(strcmp(fcn, "product") == 0)
{
result = (*comp[3]) (paramX, paramY);
}
char finalOut[40] = " ";
char finalOut2[] = " Result is: ";
char finalRes[5] = " ";
sprintf(finalRes, "%d", result);
strcat(finalOut, ready[i]);
strcat(finalOut, finalOut2);
strcat(finalOut, finalRes);
send(finalOut);
以及所有代码在它是完全相同类型的内容之前。在每次迭代时,恒定的重新分配和重复使用相同的变量。从原始子字符串中解析子字符串,然后使用该数据将新字符串发送到新数组。请记住,这只是巨型循环内部的一部分。通过解析原始字符串的子字符串,如“ FCN”(例如“ FCN”)所示的其他变量。
摘录是如何将新数组的内容写入新文本文件的内容。这实际上可能是问题。
pthread_mutex_lock(&mutex1);
FILE *f = fopen(outFile, "wb+");
fwrite(fifo, 100, sizeof(fifo), f);
fclose(f);
pthread_mutex_unlock(&mutex1);
坦率地说,在编写此代码时,我尚不清楚应该将什么值作为FWRITE方法的第二个参数传递给什么值
Basically, I have a c project where I populate a 2d string array with contents of a text file. I iterate over the array and at each iteration I parse certain things from that string, assign those substrings to variables and then I concatenate it back into one string and send the new string off to another 2d string array. However, when I write the contents of the new array to another text file I get strange characters and other nonsense. Is me reusing/reassigning the same variables causing memory issues? Perhaps I need to include null terminators? I'm new to C so excuses my lack of knowledge. Here's an excerpt ..
int result;
if(strcmp(fcn, "sum") == 0)
{
result = (*comp[0]) (paramX, paramY);
}
else if(strcmp(fcn, "power") == 0)
{
result = (*comp[1]) (paramX, paramY);
}
else if(strcmp(fcn, "fibonacci") == 0)
{
result = (*comp[2]) (paramX, paramY);
}
else if(strcmp(fcn, "product") == 0)
{
result = (*comp[3]) (paramX, paramY);
}
char finalOut[40] = " ";
char finalOut2[] = " Result is: ";
char finalRes[5] = " ";
sprintf(finalRes, "%d", result);
strcat(finalOut, ready[i]);
strcat(finalOut, finalOut2);
strcat(finalOut, finalRes);
send(finalOut);
And all the code before it is kinda the exact same type of stuff. Constant re-assignment and reuse of the same variables at each iteration. Parsing substrings from the original substring and then using that data to make a new string to be sent to a new array. Keep in mind this is just a piece of what lies inside of a giant for loop. Other variables shown like "fcn" is what was declared before this excerpt by parsing a substring of the original string.
Excerpt of how im writing the contents of the new array to the new text file. This might actually be the issue.
pthread_mutex_lock(&mutex1);
FILE *f = fopen(outFile, "wb+");
fwrite(fifo, 100, sizeof(fifo), f);
fclose(f);
pthread_mutex_unlock(&mutex1);
To be frank, at the time of writing this code I was very unclear about what value should be passed in as the 2nd parameter of the fwrite method
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论