在C中的结构中有数组时的最佳实践是什么? [英] What's the best practice when you have an array in a struct in C?

查看:89
本文介绍了在C中的结构中有数组时的最佳实践是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义结构,将用于通过TCP连接发送数据.在此struct中声明数组的最佳方法是什么?会是:

I have a custom struct which I'm gonna use to send data over a TCP connection. What would be the best way of declaring an array inside this struct ? Would it be :

typedef struct programData {
    int* dataArray;
    size_t numberofelements;
} pd;
// ...
pd data = {0};
data.dataArray = malloc(5*sizeof(int));
// put content in array ...
data.numerofelements = 5;

或者是这样:

typedef struct programData {
    int dataArray[5];
} pd;
// ...
pd data = {0};
data.dataArray[0] = ...;
// ...
data.dataArray[4] = ...;

我是出于习惯在C语言中使用malloc()的习惯,但不认为该数组的内容实际上会传递给连接另一端的客户端,因为dataArray实际上会指向服务器内存中的内存地址的指针.还是send(2)实际使用它发送数组的内容?

I did the first way out of habit of using malloc() in C, but don't think the contents of the array would actually be passed on to the client on the other side of the connection since dataArray would actually be a pointer to a memory address inside the server's memory. Or would send(2) actually send the contents of the array with it ?

edit:由于我的代码中的复制粘贴而导致的一些不连贯性

edit : some incoherences due to copy pasting from my code

推荐答案

send不是用于传输复合数据结构的服务,包括解释指针和连接的数据的含义.它是用于发送原始字节的服务.使用send时,必须将数据转换为可以发送的原始字节.接收者必须根据这些字节构造自己的数据结构.这意味着您必须创建一种使用字节表示数据的方案.

send is not a service for transmitting compound data structures, including interpreting the meanings of pointers and connected data. It is a service for sending raw bytes. When using send, you must transform your data into raw bytes that can be sent. The receiver must construct their own data structures from those bytes. This means you must create a scheme for representing your data using bytes.

当将结构的原始字节发送到另一个系统,并且接收系统使用这些相同的原始字节来表示结构时,由于以下原因,数据的最终含义可能会有所不同:

When the raw bytes of a structure are sent to another system, and the receiving system uses those same raw bytes to represent a structure, the resulting meaning of the data may differ for reasons including:

  • 系统表示对象(例如整数),其字节顺序不同.
  • 系统在结构中插入不同数量的填充字节,以保持硬件所需或首选的对齐方式.
  • 系统对字符或浮点数据使用不同的编码.
  • 系统上的类型是不同的,其中一个可能将两个字节用于int,而另一个可能使用四个字节.
  • 一个系统上的指针在另一个系统上毫无意义,因为它们指向的数据从未传输到另一个系统,并且包含与另一个系统上的地址布局无关的地址.
  • The systems represent objects (such as integers) with bytes in different orders.
  • The systems insert different numbers of padding bytes in the structure to maintain alignment required or preferred by hardware.
  • The systems use different encodings for characters or floating-point data.
  • The types on the system are different, as where one may use two bytes for int while the other uses four.
  • Pointers on one system are meaningless on the other system, as they point to data that was never transmitted to the other system and that contain addresses that are not relevant to the address layout on the other system.

使用简单的数据结构,可以定义用于传输原始字节以发送代表数据结构的实际字节的协议.如果发送和接收系统使用相同的硬件和软件,则尤其如此.但是,即使在这种情况下,也应明确规定协议:每个元素有多大,使用什么数据编码,每个元素中的字节按什么顺序等等.

With a simple data structure, it is possible to define the protocol for transmitting raw bytes to send the actual bytes that represent the data structure. This is especially true if the sending and receiving systems use the same hardware and software. However, even in such cases, the protocol should be clearly specified: How big is each element, what data encodings are used, what order are the bytes within each element in, and so on.

假定您具有简单的数据结构,并使用简单的协议发送代表数据的实际字节,那么当然,声明结构内部的数组是最简单的.如果数组很小或通常接近满,那么通过存储和传输未使用的数据将仅产生少量浪费,那么在结构内部声明数组可能是一个很好的解决方案.

Assuming you have simple data structures and use a simple protocol of sending the actual bytes that represent the data, then of course declaring an array inside the structure is the simplest. If the array is small or is usually nearly full, so that only a small amount of waste will occur by storing and transmitted unused data, then declaring an array inside the structure may be a fine solution.

如果阵列中所需的数据量变化不大,那么从资源效率的角度来看,通常最好动态分配阵列.如您的问题所示,该结构可能包含一个指针,该指针填充有数组数据的地址.

If the amount of data needed in the array will vary more than slightly, then it is usually preferred to allocate the array dynamically, as a matter of resource efficiency. As shown in your question, the structure may contain a pointer, which is filled in with the address of the array data.

当一个结构包含这样的指针时,您不能使用send发送该指针(不付出额外的努力来提供其解释).相反,您将需要使用一个或多个send调用来发送结构中的其他数据,然后您将需要另一个send调用来发送数组中的数据.而且,当然,您用于传输数据的协议必须包括一种方法来传达要发送的数组元素的数量.

When a structure contains such a pointer, you cannot send the pointer with send (without making additional efforts to provide for its interpretation). Instead, you will need to use one or more send calls to send the other data in the structure, and then you will need another send call to send the data in the array. And, of course, your protocol for transmitting the data must include a way to communicate the number of array elements being sent.

另一个选项混合了对数组的动态分配空间和将数组包括在结构中:结构的最后一个元素可以是灵活的数组成员.这是在结构中声明为Type dataArray[];的数组.它必须是结构的最后一个元素.它没有固有的大小,但是,当为结构分配空间时,您将为数组添加额外的空间.在这种情况下,代替具有指向数组的指针的结构,该数组跟随内存中结构的基础部分.可以在单个send调用中发送带有数组的这种结构,前提是要提供上述注意事项:接收系统必须能够正确解释字节,并且必须传达数组的大小.

One more option mixes both dynamic allocation of space for the array and including the array in the structure: The last element of a structure may be a flexible array member. This is an array declared within the structure as Type dataArray[];. It must be the last element of the structure. It has no intrinsic size, but, when allocating space for the structure, you would add additional space for the array. In this case, instead of the structure having a pointer to an array, the array follows the base portion of the structure in memory. Such a structure with its array could be sent in a single send call, provided the cautions above are provided for: The receiving system must be able to interpret the bytes correctly, and the size of the array must be communicated.

这篇关于在C中的结构中有数组时的最佳实践是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆