在C中动态创建对象

发布于 2024-12-31 21:00:49 字数 199 浏览 1 评论 0原文

哈喽,伙计们!

我熟悉 JavaScript 和 PHP,但对 C 不太熟悉。 我正在尝试使用 C 和 craete 碰撞算法中的图形。现在,我需要动态创建对象,就像在更高级的语言中一样。例如,我需要通过自己的函数创建一个多边形,并使其成为整个脚本可见的对象。我想,需要一个结构。

据我所知,函数中声明的所有内容都保留在函数中。如何动态声明全局结构?

Hello, guys!

I'm familiar with JavaScript and PHP, but new to C.
I am trying to play around with graphics in C and craete colision algorithm. Now, I need to create objects dynamically, just like in more advanced languages. For example, I need to create a polygon via my own function and make it an object that would be visible to the whole script. I assume, a struct would be needed.

As far as I know, everything declared in a function stays in a function. How can I dynamically declare global structs?

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

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

发布评论

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

评论(4

只涨不跌 2025-01-07 21:00:49

C 是一种相当静态的语言。我所说的静态是指,您可以在运行时创建内存,但您需要指针来寻址在编译时声明的内存。也就是说,如果您在运行时需要内存并且不想在编译时声明它,则需要使用 malloc 和 free (当您使用完内存时)。

要创建一个在运行时创建其内存的全局结构,您至少需要一个在编译时指向结构的指针。如果您需要其中多个结构,则可以创建多个结构的内存,但是如果没有这些结构的数组,则遍历这些结构将是乏味的。在编译时您将需要指向结构的指针数组。有一些方法可以使其更加动态,但在我使用 C 和 C++ 的十年左右的时间里,我们从未遇到过其他方法,包括在设备驱动程序中。

当您说在 C 中创建对象时,除了通过对库的函数调用或从堆创建内存,然后通过在其上重叠结构或数组指针来解释该内存之外,您实际上没有可以创建的对象。

如果这些参数是通过引用(指向参数的指针)传入的,则函数可以更改参数,并且函数可以不返回任何内容或返回单个数据原子、char、整数、smallint 或指针。

C is a fairly static language. By static I mean, you can create memory during run-time, but you will need pointers to address that memory declared at compile time. That is if you are going to need memory during run-time and do not want to declare it during compile time, you will need to use malloc and free (when you've finished with the memory).

To create a global structure whose memory you would create at run time, you would minimally need a pointer to a structure at compile time. If you need several of those structures, you could create several structures' worth of memory, but traversing the structures would be tedious without having an array of those structures. You would need that array of pointers to structures at compile time. There are some ways to make this more dynamic, but in decade or so I used C and C++, we never ran into those other ways, including in device drivers.

When you say create objects in C, you really have no objects you can create other than those created by a function call to a library or creating memory from the heap, and then interpreting that memory by overlapping structure or array pointers over it.

Functions can alter parameters if those parameters are passed in by reference (a pointer to the parameter), and functions can return nothing or return a single atom of data, a char, integer, smallint, or pointer.

眼角的笑意。 2025-01-07 21:00:49

一个。函数可以返回值。

b.您可以使用全局变量。

c(可能是最有用的)。动态分配内存(使用malloc等)并返回指向它的指针。 (使用后记得释放)

a. function can return value.

b. you can use global variables.

c (and probably the most useful). dynamically allocate memory (using malloc,etc) and return pointer to it. (And remember to free it after using)

浅笑依然 2025-01-07 21:00:49

您需要有一个结构或更复杂的抽象数据类型(ADT)来保存动态创建的变量。一旦你有了这个,你就可以通过 malloc() 创建你想要的任何对象,并将其存储在那里。

正如我之前提到的,强烈建议您查看 ADT 并学习如何使用它们。这将允许您创建任何复杂的数据结构,例如队列或链接列表,以便更加面向 OOP 工作。

You need to have a struct or a more complex abstract data type (ADT) to hold your dynamically created variables. Once you have this, you can create the any object you want via malloc(), and store it in there.

As I mentioned earlier, it would be highly recommended to have a look at the ADTs and learn how to work with them. This will allow you to create any complex data structure like queues or linked lists in order to work a little more OOP oriented.

遥远的绿洲 2025-01-07 21:00:49

声明与结构体类型相同的全局指针(指针数组)。使用malloc等函数动态分配内存并将其分配给指针。

declare global pointers(array of pointers) of the same type as the structure. Use the functions like malloc etc. to dynamically allocate memory and assign it to the pointers.

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