Я получаю ошибку сегментации в программе C. Это не происходит каждый раз, когда приложение работает. Случается нечасто, как раз в неделю или около того. Кажется, я не могу понять, почему это происходит. Аргумент Const char *str, наверняка не null. < /P>
Минимальный воспроизводимый пример: < /p>
#include
#include
#include
#include
#define MAXSTRLEN 256
#define LOG_BUF_SZ 4096 * 10
int run_sec = 0;
int default_s = 20;
float temp = 40.0;
unsigned int I_Dt[4] = {0, 0, 0, 0};
/* prototype */
void dbg_log(const char *str);
void test_rd(const char *str, int nl);
void log_data(int tsts);
char log_buffer[LOG_BUF_SZ];
char *b_log_bptr = log_buffer;
FILE *b_log_file = NULL;
int main() {
int tsts = 0;
memset (log_buffer, 0, sizeof (log_buffer));
if ((b_log_file = fopen("record_log.txt", "w+")) == NULL) {
dbg_log("Failed to open file");
exit(EXIT_FAILURE);
}
while (1) {
run_sec++;
log_data(tsts);
sleep(1);
if (tsts == 0)
tsts = 1;
else
tsts = 0;
}
return 0;
}
void dbg_log(const char *str) {
fprintf(stderr, "%s\n", str);
fflush(stderr);
}
void test_rd(const char *str, int nl) {
extern char *b_log_bptr;
extern FILE *b_log_file;
int i;
if (b_log_file != NULL) {
if (strlen(b_log_bptr) > (LOG_BUF_SZ - 4096)) {
dbg_log("Warning: size limit reached");
return;
}
fprintf(b_log_file, "%s", str);
strcat(b_log_bptr, str);
for (i = 1; i 1)
fflush(b_log_file);
} else {
dbg_log("Warning: no FILE open");
}
}
void log_data(int t_sts) {
char str[MAXSTRLEN];
char lg_str[100], g_str[100];
memset(g_str, 0, sizeof(g_str));
memset(lg_str, 0, sizeof(lg_str));
if (t_sts == 0 || t_sts == 1) {
if (t_sts == 0)
sprintf(g_str, "AAAA ");
else
sprintf(g_str, "%03d,", (int)run_sec + default_s);
sprintf(str, "%2.02f", temp);
strcat(g_str, str);
strcpy(lg_str, g_str);
sprintf(str, ",%8u,%8u,%8u,%8u", I_Dt[0], I_Dt[1], I_Dt[2], I_Dt[3]);
strcat(lg_str, str);
test_rd(lg_str, 1);
}
}
< /code>
Это исходный вопрос, который я разместил: < /p>
extern char *b_log_bptr;
extern FILE *b_log_file;
void test_rd( const char *str, int nl ) {
int i;
if (b_log_file != NULL) {
if (strlen(b_log_bptr) > (LOG_BUF_SZ - 4096)) {
dbg_log("Warning: size limit reached");
return;
}
fprintf(b_log_file, "%s", str);
strcat(b_log_bptr, str);
for(i = 1 ; i 1)
fflush(b_log_file);
} else {
dbg_log("Warning: no FILE open");
}
}
< /code>
Вот Backtrace, когда это происходит: < /p>
/usr/local/bin/processor_srv(write_backtrace+0x64)[0x5560592b94]
/usr/local/bin/processor_srv(+0x24268)[0x5560574268]
linux-vdso.so.1(__kernel_rt_sigreturn+0x0)[0x7f8fa53810]
/lib/libc.so.6(fputs+0x24)[0x7f8f06c104]
/usr/local/bin/processor_srv(test_rd+0x54)[0x55605843e4]
/usr/local/bin/processor_srv(log_data+0x438)[0x5560585b58]
/usr/local/bin/processor_srv(run_test_tr+0x124)[0x5560569144]
/usr/local/bin/processor_srv(run_tasks_tr+0x58)[0x5560568428]
/usr/local/bin/processor_srv(main+0x1718)[0x5560564d98]
/lib/libc.so.6(+0x284f8)[0x7f8f0284f8]
/lib/libc.so.6(__libc_start_main+0x9c)[0x7f8f0285dc]
/usr/local/bin/processor_srv(_start+0x30)[0x55605652f0]
Подробнее здесь: https://stackoverflow.com/questions/797 ... ng-fprintf
Разлом сегментации в FPUTS при выполнении FPRINTF [закрыто] ⇐ Linux
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Очень неожиданная производительность fprintf против std::ofstream (fprintf очень медленный)
Anonymous » » в форуме C++ - 0 Ответы
- 86 Просмотры
-
Последнее сообщение Anonymous
-