这是我的代码:
#include <iostream>
#include <typeinfo>
using namespace std;
int main() {
const int size = 10;
char test[size];
char test2[size];
cout << "the size of test is " << sizeof(test) << endl;
cout << "input a sentence:" << endl;
cin.getline(test, 50);
cout << "your input is: " << test << endl;
cout << "the size of test is " << sizeof(test) << endl;
cout << "-----stop-----" << endl;
return 0;
}
我使用 MinGw-w64 3.1 在Clion进行了测试,结果如下:
the size of test is 10
input a sentence:
this is a very long sentence
this is a very long sentence
your input is: this is a very long sentence
the size of test is 10
-----stop-----
Process finished with exit code -1073741819 (0xC0000005)
在我完成所有输出之前它没有停止。我试图读取阵列测试的那一刻不应该停止吗?毕竟,我声称尺寸为10,但试图读取尺寸50。
然后我在Ubuntu下使用 gcc 5.4.0 测试了它,它没有发出任何错误消息!我想知道它为什么不停止?
答案 0 :(得分:1)
因为你(非)幸运!
这是我得到的:
from turtle import Turtle, Screen
TEXT = "Penny for your thoughts" # arbitrary text
POSITION = (150, 150) # arbitrary position
FONT_SIZE = 36 # arbitrary font size
FONT = ('Arial', FONT_SIZE, 'normal')
X, Y = 0, 1
def box(turtle, lower_left, upper_right):
""" same as above example """
screen = Screen()
marker = Turtle(visible=False)
marker.penup()
marker.speed('fastest')
marker.fillcolor('pink')
marker.setx(screen.window_width() + 1000)
start = marker.position()
marker.write(TEXT, align='center', move=True, font=FONT)
end = marker.position()
marker.undo() # clean up after ourselves
marker.speed('normal')
marker.goto(POSITION)
# Since it's centered, the end[X] represents 1/2 the width
half_width = end[X] - start[X]
marker.begin_fill()
box(marker, (POSITION[X] - half_width, POSITION[Y]), (POSITION[X] + half_width, POSITION[Y] + FONT_SIZE))
marker.end_fill()
marker.write(TEXT, align='center', font=FONT)
screen.exitonclick()
您目击的是未定义的行为,这意味着它未定义代码执行时会发生什么。