铸造“BigStruct”到一个“SmallStruct”在 C 中(具有不同大小的静态数组的类似结构)
假设由于某种原因只允许在 C 程序中使用静态内存。 我有一个在几个地方使用的基本结构,定义如下:
#define SMALL_STUFF_MAX_SIZE 64
typedef struct {
/* Various fields would go here */
...
double data[SMALL_STUFF_MAX_SIZE]; /* array to hold some data */
} SmallStuff;
现在,我被要求添加一个新功能,该功能导致我需要相同的结构但具有更大的数组的特殊情况。由于内存太紧张,我无法承担最大化 SmallStuff 结构的数组的费用。因此,我制作了一个如下定义的结构的特殊版本,当调用需要指向 SmallStuff 结构的指针的函数时,我最终将其转换为 (SmallStuff*) (“数据”的实际大小在这些函数中得到了正确处理)
#define BIG_STUFF_MAX_SIZE 1000000
typedef struct {
/* Various fields, identical to the ones in SmallStuff would go here */
...
double data[BIG_STUFF_MAX_SIZE]; /* array to hold some data */
} BigStuff;
显然,正确的方法是动态分配内存,但如上所述我不能使用动态内存分配。
我应该考虑任何副作用吗? 或者有更好的方法来处理此类问题?
提前致谢。
Supposed that for some reason you are only allowed to use static memory in a C program.
I have a basic structure that I am using in several places defined as below:
#define SMALL_STUFF_MAX_SIZE 64
typedef struct {
/* Various fields would go here */
...
double data[SMALL_STUFF_MAX_SIZE]; /* array to hold some data */
} SmallStuff;
Now, I have been asked to add a new feature that lead to a particular case where I need the same structure but with a much larger array. I can't afford to max up the array of the SmallStuff structure as memory is too tight. So I made a special version of the struct defined as below that I eventually cast to a (SmallStuff*) when calling functions that expect a pointer to a SmallStuff structure (the actual size of 'data' is properly handled in these functions)
#define BIG_STUFF_MAX_SIZE 1000000
typedef struct {
/* Various fields, identical to the ones in SmallStuff would go here */
...
double data[BIG_STUFF_MAX_SIZE]; /* array to hold some data */
} BigStuff;
Obviously, the proper way to do it would be to dynamically allocate the memory but as said above I can't use dynamic memory allocation.
Are there any side-effects that I should consider?
Or better ways to deal with this kind of problem?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你所做的很好,尽管它往往会吓到那些对指针和转换感到不舒服的人。
您的问题的一般解决方案是摆脱 BigStuff 和 SmallStuff 并创建一个带有 size 成员和指向您选择的数组的双 *data 的单个 Stuff 结构,而不是冒着稍后代码中潜在错误的风险或必须当您发现您还需要 MediumStuff 时,请更改您的功能。这使您可以灵活地使用任何合适的尺寸。
What you're doing is fine, though it tends to scare people who are uncomfortable with pointers and casting.
The general solution for your problem is to get rid of BigStuff and SmallStuff and make a single Stuff structure with a size member and a double *data that points to an array of your choosing, instead of risking potential miscasts in your code later or having to change your functions when you discover you also need MediumStuff. This gives you the flexibility of using whatever sizes are appropriate.
然后,如果 x 是 SmallStuff 或 BigStuff,您可以将 &x.header 传递给可以采用其中任何一个的例程。
Then if x is either a SmallStuff or BigStuff, you can pass &x.header to routines that can take either.
尽管其代码由于复杂性而丑陋,但不应该存在任何运行时问题,因为大小是硬编码的。
处理它的更好方法是使用不需要您拥有两个仅大小不同的单独结构的算法。不过,我不了解你的应用,所以你最清楚如何处理这个问题。
Although its ugly code because of the complexity, there should not be any runtime problems because the sizes are hard-coded.
A better way to deal with it is to have algorithms which didnt need you to have 2 separate structs which only differ by size. However, I dont know your application, so you know best how to deal with this problem.