缺少空闲时 C++ 程序不会在 Cygwin 中退出
C++ program not exiting in Cygwin when free is missing
我写了一个小的 C++ 程序来向朋友说明一些事情,并注意到奇怪的行为:当在 cygwin 中 运行 时,程序在第二次打印后没有退出或对 CTRL+C 做出反应,但作为当 运行 从 powershell 或在 linux.
上重新编译&运行 时预期
当我取消注释行 //free(map);
时,程序正常退出。
我想知道为什么 cygwin 冻结,所以我必须关闭 mintty window - 如果这是我机器的问题,如何修复它。
#include<iostream> // for std::cout
#include<cstdlib> // for atoi, uses input 0 for invalid numbers
#include<stdio.h> // for printf
// typedef for clearer code
typedef int matrixcontent; // change 'int' to whatever kind of elements there are in the matrix
// declarations first, so I can write main whereever I want to
void printMatrix(matrixcontent* map, size_t dimX, size_t dimY) ;
void fillMatrix(matrixcontent* map, size_t dimX, size_t dimY) ;
int main(int argc, char** argv){
if(argc!=3){
printf("%s", "please give exactly two arguments.");
return 1;
}
// arg to int
int y = atoi(argv[1]);
int x = atoi(argv[2]);
// create matrix
// allocate memory space
matrixcontent* map = (matrixcontent*) malloc(sizeof(matrixcontent)*x*y);
if(map==NULL){
printf("%s", "Not enough memory");
}
// print current (random) content
printMatrix(map, x, y);
// fill matrix
fillMatrix(map, x, y);
// print the modified matrix again
printMatrix(map, x, y);
//free(map);
return 0;
}
void printMatrix(matrixcontent* map, size_t dimX, size_t dimY) {
for(size_t row=0; row<dimY; row++){
for(size_t col=0; col<dimX; col++){
std::cout << map[row*dimY+col] << ",\t";
}
std::cout << std::endl;
}
std::cout << std::endl << std::endl;
}
void fillMatrix(matrixcontent* map, size_t dimX, size_t dimY) {
for(size_t row=0; row<dimY; row++){
for(size_t col=0; col<dimX; col++){
map[row*dimY+col] = col+row; // fill with something
}
}
}
我在 win10 的 cygwin x64 中用 g++ --std=c++11 -Wall -Wextra -O0 -g example.cpp -o example
编译了它。
我试过的
Debian,按预期工作
Win10、cygwin x64、台式电脑、描述的奇怪行为
Win10、cygwin x64、笔记本电脑、./example 10 5
描述的奇怪行为,而不是 ./example 10 20
.
Win10、powershell、笔记本电脑,可按预期用于 ./example 10 5
和 ./example 10 20
。
这不是 Cygwin 的东西。这是索引溢出。
[row*dimY+col]
使索引超出范围 x * y
。
我想你应该使用更好的
[row*dimX+col]
访问 map
.
我写了一个小的 C++ 程序来向朋友说明一些事情,并注意到奇怪的行为:当在 cygwin 中 运行 时,程序在第二次打印后没有退出或对 CTRL+C 做出反应,但作为当 运行 从 powershell 或在 linux.
上重新编译&运行 时预期
当我取消注释行 //free(map);
时,程序正常退出。
我想知道为什么 cygwin 冻结,所以我必须关闭 mintty window - 如果这是我机器的问题,如何修复它。
#include<iostream> // for std::cout
#include<cstdlib> // for atoi, uses input 0 for invalid numbers
#include<stdio.h> // for printf
// typedef for clearer code
typedef int matrixcontent; // change 'int' to whatever kind of elements there are in the matrix
// declarations first, so I can write main whereever I want to
void printMatrix(matrixcontent* map, size_t dimX, size_t dimY) ;
void fillMatrix(matrixcontent* map, size_t dimX, size_t dimY) ;
int main(int argc, char** argv){
if(argc!=3){
printf("%s", "please give exactly two arguments.");
return 1;
}
// arg to int
int y = atoi(argv[1]);
int x = atoi(argv[2]);
// create matrix
// allocate memory space
matrixcontent* map = (matrixcontent*) malloc(sizeof(matrixcontent)*x*y);
if(map==NULL){
printf("%s", "Not enough memory");
}
// print current (random) content
printMatrix(map, x, y);
// fill matrix
fillMatrix(map, x, y);
// print the modified matrix again
printMatrix(map, x, y);
//free(map);
return 0;
}
void printMatrix(matrixcontent* map, size_t dimX, size_t dimY) {
for(size_t row=0; row<dimY; row++){
for(size_t col=0; col<dimX; col++){
std::cout << map[row*dimY+col] << ",\t";
}
std::cout << std::endl;
}
std::cout << std::endl << std::endl;
}
void fillMatrix(matrixcontent* map, size_t dimX, size_t dimY) {
for(size_t row=0; row<dimY; row++){
for(size_t col=0; col<dimX; col++){
map[row*dimY+col] = col+row; // fill with something
}
}
}
我在 win10 的 cygwin x64 中用 g++ --std=c++11 -Wall -Wextra -O0 -g example.cpp -o example
编译了它。
我试过的
Debian,按预期工作
Win10、cygwin x64、台式电脑、描述的奇怪行为
Win10、cygwin x64、笔记本电脑、./example 10 5
描述的奇怪行为,而不是 ./example 10 20
.
Win10、powershell、笔记本电脑,可按预期用于 ./example 10 5
和 ./example 10 20
。
这不是 Cygwin 的东西。这是索引溢出。
[row*dimY+col]
使索引超出范围 x * y
。
我想你应该使用更好的
[row*dimX+col]
访问 map
.