C 控制台游戏的动态大小输入缓冲区

发布于 2024-11-04 13:31:51 字数 816 浏览 0 评论 0原文

嘿,我正在尝试用 C++ 重写代码以在 C 中工作。我基本上只是想在 C 中找到 new 和 delete 的等效项,但它不太工作,这是我的代码:

这是 C++ 中的代码:

  // Gets the number of events
ReadConsoleInput(rHnd, eventBuffer, numEvents, &numEventsRead);
  // Sizes the eventbuffer based on the number of events
INPUT_RECORD *eventBuffer = new INPUT_RECORD[numEvents];

  // Removes from memory:
delete[] eventBuffer;

这是什么到目前为止,我在 C:

  // Event buffer 
INPUT_RECORD *eventBuffer;
  // Gets the number of events
ReadConsoleInput(rHnd, eventBuffer, numEvents, &numEventsRead);
  // Sizes the event buffer based on the number of events.
eventBuffer = malloc(numOfEvents * sizeof(*eventBuffer));
  // Removes from memory:
free(eventBuffer);

上面的代码几乎可以运行,但有一个错误: 错误:“void *”类型的值无法分配给“INPUT_RECORD *”类型的实体

Hey, I'm trying rewrite code in C++ to work in C. I'm basically just trying to find an equivalent for new and delete in C but it's not quite working, here is my code:

Here's the code in C++:

  // Gets the number of events
ReadConsoleInput(rHnd, eventBuffer, numEvents, &numEventsRead);
  // Sizes the eventbuffer based on the number of events
INPUT_RECORD *eventBuffer = new INPUT_RECORD[numEvents];

  // Removes from memory:
delete[] eventBuffer;

Here's what I have so far in C:

  // Event buffer 
INPUT_RECORD *eventBuffer;
  // Gets the number of events
ReadConsoleInput(rHnd, eventBuffer, numEvents, &numEventsRead);
  // Sizes the event buffer based on the number of events.
eventBuffer = malloc(numOfEvents * sizeof(*eventBuffer));
  // Removes from memory:
free(eventBuffer);

The code above almost works with one error:
Error: a value of type "void *" cannot be assigned to an entity of type "INPUT_RECORD *"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

轻拂→两袖风尘 2024-11-11 13:31:51

你只需要强制转换它——

eventBuffer = (INPUT_RECORD*) malloc(numOfEvents * sizeof(*eventBuffer));

当然,有人会说标准规定你不必强制转换“malloc”的结果。显然,在这种情况下,标准是无关紧要的:)

You just have to cast it --

eventBuffer = (INPUT_RECORD*) malloc(numOfEvents * sizeof(*eventBuffer));

Of course, someone is going to come along and say that the standard says you don't have to cast the result of "malloc". Obviously, in this case, the standard is irrelevant :)

紫罗兰の梦幻 2024-11-11 13:31:51

你的C++代码不起作用。您将 eventBuffer 传递给 ReadConsoleInput(),但直到稍后才声明它:

  // Gets the number of events
ReadConsoleInput(rHnd, eventBuffer, numEvents, &numEventsRead);
  // Sizes the eventbuffer based on the number of events
INPUT_RECORD *eventBuffer = new INPUT_RECORD[numEvents];

如果 ReadConsoleInput() 需要 eventBuffer对于某些东西,您需要在调用函数之前声明它。

不管怎样,等效的 C 代码是:

INPUT_RECORD* eventBuffer;
ReadConsoleInput(rHnd, eventBuffer, numEvents, &numEventsRead);
eventBuffer = (INPUT_RECORD*) malloc(numOfEvents * sizeof(INPUT_RECORD));

Your C++ code doesn't work. You pass eventBuffer to ReadConsoleInput() but it's only later that you declare it:

  // Gets the number of events
ReadConsoleInput(rHnd, eventBuffer, numEvents, &numEventsRead);
  // Sizes the eventbuffer based on the number of events
INPUT_RECORD *eventBuffer = new INPUT_RECORD[numEvents];

If ReadConsoleInput() needs eventBuffer for something, you'll need to declare it before calling the function.

Anyway, the equivalent C code would be:

INPUT_RECORD* eventBuffer;
ReadConsoleInput(rHnd, eventBuffer, numEvents, &numEventsRead);
eventBuffer = (INPUT_RECORD*) malloc(numOfEvents * sizeof(INPUT_RECORD));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文