0
我使用libtrace編寫了此代碼,但是出現了段錯誤。我把所有的代碼放在這裏作爲澄清。任何人都可以幫助我解決問題! 我也應該提到,代碼要抽樣一些特殊的數據包的平方根系數。使用libtrace編寫C代碼中的Seg錯誤
int main(int argc,char **argv)
{
while(trace_read_packet(trace,packet)> 0){
if (filter && !trace_apply_filter(filter,packet))
continue;
if (packet->type < TRACE_RT_DATA_SIMPLE)
/* Ignore RT messages */
continue;
numpackets ++;
if (flag == true)
{
first =trace_get_seconds(packet);
flag = false;
}
sizes[i] = trace_get_capture_length(packet);
i++;
randnumber = (double)rand()/(double)RAND_MAX;
if(randnumber <= probability)
{
//trace_dump_packet(packet);
trace_write_packet(output, packet);
sampledpackets++;
}
next = trace_get_seconds(packet);
if (double_t(next - first)== blocktime)
{
scv = var(sizes, i);
scv = a_scv * scv + scv_error;
m = a_m * m + m_error;
n = 1 - (n/2);
probability = (ltqnorm(n)*e*scv)/m;
i =0;
flag = true;
}
}
printf("\n");
printf("number of packets is : %llu\n",numpackets);
printf("number of sampled packet: %llu\n",sampledpackets);
if (trace_is_err(trace)) {
trace_perror(trace, "trace_read_packet");
}
trace_destroy(trace);
trace_destroy_output(output);
}
return 0;
}
此外,這是我的記憶輸出:
7fff18b1b000-7fff18b3c000 rw-p 00000000 00:00 0 [stack]
7fff18bfe000-7fff18c00000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
你的變量在哪裏申報? – kbshimmyo
,但我沒有把它放在這裏 – user3120628
嘗試使用'-Wall -g'編譯並通過[Valgrind](http://valgrind.org/docs/manual/quick-start.html)運行它。 – kbshimmyo