64,654
社区成员
发帖
与我相关
我的任务
分享
void draw(int m,int n)
{
COORD cor;
cor.X=m;
cor.Y=n;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), cor);
}
char read(int m,int n)
{
HANDLE hOut=GetStdHandle(STD_OUTPUT_HANDLE);
COORD rcd={m,n};
static char *ch=new char;
DWORD read;
if(ReadConsoleOutputCharacter(hOut,ch,1,rcd,&read))
return *ch;
else return '#';
}
这是我写的代码,因为这个函数调用频繁,所有用了static保证只申请一次内存,但不释放他,请问我的个函数可以吗?会有漏洞吗?#include <stdio.h>
#include <windows.h>
#define COLUMN_SIZE 80
#define ROW_SIZE 50
CHAR_INFO Char_Info[COLUMN_SIZE * ROW_SIZE];
char GetConsoleChar(int x, int y)
{
HANDLE hConsole;
COORD coIn, coUpperLeft;
SMALL_RECT Rect;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
coIn.X = ROW_SIZE;
coIn.Y = COLUMN_SIZE;
coUpperLeft.X = 0;
coUpperLeft.Y = 0;
Rect.Left = 0;
Rect.Top = 0;
Rect.Right = COLUMN_SIZE;
Rect.Bottom = ROW_SIZE;
ReadConsoleOutput(hConsole, Char_Info, coIn, coUpperLeft, &Rect);
return Char_Info[COLUMN_SIZE * (x - 1) + y - 1].Char.AsciiChar;
}
int main()
{
int i;
printf("test output.\n2nd line.\n");
for(i = 1; i < 10; i++)
putchar(GetConsoleChar(1, i));
return 0;
}