作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在用 C 语言编写这段代码,它计算并绘制 6 级流水线和超标量架构上的周期总数。代码编译得很好,但是当我运行它时,出现段错误。
我在此选项中遇到段错误。我输入我的选择为 1,然后输入总周期为 5。用户还应输入指令,例如r0 = r1 + r2 但我不知道如何提示
void EnterInst(){
char instr_string[9];
int i;
printf("Enter total number of instructions: ");
scanf("%d\n", n);
set = (instr*) malloc ((n+1) * sizeof(instr));
set[0].dest = -1;
for(i = 1; i <= n; i++){
printf("%d", i);
scanf("%s", instr_string);
set[i].dest = instr_string[1]-'0';
set[i].src1 = instr_string[4]-'0';
set[i].src2 = instr_string[7]-'0';
}
}
当我选择 2 作为我的选择时,我还会遇到另一个段错误。以下代码适用于第二个选项
void pipelined(){
int overlap = 0;
int delay =0;
set[1].delay = 0;
int i;
for(i = 2; i <= n; i++){
if((set[i-2].dest == set[i].src1) || (set[i-2].dest == set[i].src2)){
if(overlap == 0){
delay = 1;
overlap = 1;
}
else{
delay = 0;
overlap = 0;
}
}//if RAW dep.
else{ overlap = 0; }
if((set[i-1].dest == set[i].src1) || (set[i-1].dest == set[i].src2)){
delay = 2;
overlap = 1;
}
set[i].delay = delay + 1 + set[i-1].delay;
}//end for-loop
//calculate total delay
total_delay = set[n].delay;
printf("Total number of cycles: %d", total_delay);
printf("%d",print_Chart());
}
我真的很想知道为什么会发生段错误以及如何修复它。这实在是令我烦恼。任何帮助是极大的赞赏。
最佳答案
suggest showing definition of 'n' and 'instr'
as that would greatly help with the debugging.
this line: char instr_string[9];
only has room for 8 characters plus the string terminator
however, the example input is 12 characters plus the string terminator
this is undefined behaviour and probably is
the root cause of the seg fault event in the EnterInst() function
set[i].dest = instr_string[1]-'0'; <-- gets 0x30 - 0x30 so set[i].dest = 0
set[i].src1 = instr_string[4]-'0'; <-- gets 0x20 - 0x30 so set[i].src1 = -16
set[i].src2 = instr_string[7]-'0'; <-- gets 0x20 - 0x30 so set[i].src2 = -16
that is probably not what you want.
however, scanf stops on any white space (like a space and newline), so this line:
scanf("%s", instr_string);
will fail to input anything beyond the "r0"
therefore, the next calls, in the loop, to scanf will get nothing.
The above is a very good reason to always check the returned code from any
input function and to use a leading ' ' in scanf format strings
suggest using fgets to input a whole line into a buffer
then parse the data from that buffer
for several reasons, the returned value from the malloc family
should not be cast
I.E.
set = (instr*) malloc ((n+1) * sizeof(instr));
should be:
set = malloc ((n+1) * sizeof(instr));
and the returned value should be checked, I.E.
if( NULL == (set = malloc ((n+1) * sizeof(instr)) )
{
perror( "malloc failed" );
exit( EXIT_FAILURE );
}
the code might prompt as follows for the instruction:
int regValue = -1;
do
{
printf( "input destination register number (0...9):" )
if( 1 != scanf( " %d", ®Value ) )
{ // then, scanf failed
perror( "scanf failed for destination register number" );
exit(EXIT_FAILURE);
}
while( (0 <= regValue) && (9 >= regValue) );
set[0].dest = regValue;
with similar statements for the other registers, and the operator
the variable 'set' is a action verb, probably good as the first part of
a function name, but does not give a good representation
of a variable name, especially as it yields no indication
of its' contents.
in C, array references begin with 0 and end with 1 less than the array size
so to avoid skipping the first 'instr' entry in the 'set' array of structs
use:
for(i = 0; i < n; i++){ rather than: for(i = 1; i <= n; i++){
关于c - 流水线/超标量性能代码段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27533733/
我是一名优秀的程序员,十分优秀!