阵列做什么? [英] What does the array do?

查看:108
本文介绍了阵列做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我遇到了很多结构,我觉得很有意思

有趣:


typedef struct whatever_tag

{

int member1;

char member2;

// ...


char last_member [1]; //有趣的线

}无论如何;


我的问题是,为什么一个大小为1的数组而不仅仅是一个对象?

它看起来像C代码而不是C ++,希望它不是偏离主题的。


Ben

Recently I have come across a number of struct''s that I think is quite
interesting:

typedef struct whatever_tag
{
int member1;
char member2;
// ...

char last_member[1]; // interesting line
} whatever;

My quetions is, why an array of size 1 instead of just a single object?
It looks like C code instead of C++ though, hope it''s not off-topic.

Ben

推荐答案

* benben:
最近我遇到了一些我认为非常有趣的结构:

typedef struct whatever_tag
{member member;
char member2;
// ...

char last_member [1]; //有趣的线
}无论如何;

我的问题是,为什么一个大小为1的数组而不仅仅是一个对象?
它看起来像C代码而不是C ++,希望它不是偏离主题。
Recently I have come across a number of struct''s that I think is quite
interesting:

typedef struct whatever_tag
{
int member1;
char member2;
// ...

char last_member[1]; // interesting line
} whatever;

My quetions is, why an array of size 1 instead of just a single object?
It looks like C code instead of C++ though, hope it''s not off-topic.




这确实是C代码。我的想法是做一些类似


无论* w = malloc(offsetof(无论如何,last_member)+ n_items));

w-> last_member [ n_items-1] =某事;


在C ++中,使last_member成为std :: vector,例如。


-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么这么糟糕?

A:热门帖子。

问:usenet和电子邮件中最烦人的事情是什么?



It is indeed C code. The idea is to do something like

whatever* w = malloc( offsetof( whatever, last_member ) + n_items ) );
w->last_member[n_items-1] = something;

In C++ make last_member a std::vector, for example.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


benben写道:
最近我遇到了一些我认为非常有趣的结构:

typedef struct whatever_tag
{
int member1;
char member2;
// ...

char last_member [1]; //有趣的线
}无论如何;

我的问题是,为什么一个大小为1的数组而不仅仅是一个
对象?它看起来像C代码而不是C ++,希望它不是主题。
Recently I have come across a number of struct''s that I think is quite
interesting:

typedef struct whatever_tag
{
int member1;
char member2;
// ...

char last_member[1]; // interesting line
} whatever;

My quetions is, why an array of size 1 instead of just a single
object? It looks like C code instead of C++ though, hope it''s not
off-topic.




在comp.lang中提问通常更好.c关于任何C构造。


在过去很久以前,要创建一个具有动态结构的结构。部分你将

声明一个1的数组,如上所述,然后分配(使用''malloc'')更多

比(sizeof(无论如何))一定数量的字节数。一旦分配,

这样的结构将被认为包含一个''某个数字

of bytes''的数组,而不是一个1的数组。不是
数组语义的一部分,而是一个指针的东西,你可以写:


无论* pw =(无论*)malloc(sizeof(无论如何) )+ 150);

pw-> last_member [100] = 42;


,没关系。


这种做法基本上被C ++中的几种技术所取代。

你可以拥有一个模板参数大小的模板,你可以

有一个动态分配部分内容的类等等。


使用这个旧的C技巧的优点是那个

数组的内存就在那里,基本上_in_对象(方便用于

目的,比如序列化)。缺点是你有

来存储对象大小。


V

-

请在邮寄回复时从我的地址中删除资金



It is often better to ask in comp.lang.c about any C construct.

In good old times, to create a structure with "dynamic" part you would
declare an array of 1, like above, then allocate (using ''malloc'') more
than (sizeof(whatever)) by a certain number of bytes. Once allocated,
such structure would be considered to contain an array of ''certain number
of bytes'' at its end, and not an array of 1. Since indexing is not part
of array semantics, but instead a pointer thing, you could write:

whatever *pw = (whatever*) malloc(sizeof(whatever) + 150);
pw->last_member[100] = 42;

and it would be OK.

This practice has been essentially replaced by several techniques in C++.
You can have a template with a template-argument-sized array, you can
have a class that dynamically allocates part of it, etc.

The advantage of using this old C trick is that the memory for that
array is right there, essentially _in_ the object (convenient for the
purposes of, say, serialization). The disadvantage is that you have
to store the size of the object in the object.

V
--
Please remove capital As from my address when replying by mail


* Victor Bazarov,做C:
* Victor Bazarov, doing C:

无论* pw =(无论*)malloc(sizeof(无论如何)+ 150);

whatever *pw = (whatever*) malloc(sizeof(whatever) + 150);




在C中,转换malloc的结果是邪恶的。一个原因是如果省略了与malloc相关的#include #include,则演员可能会做非常好的事情,其中​​malloc结果指针被转换为int

又转换为类型指针,没有诊断。在

C ++中我们有更强的输入,因此在C ++中,演员应该在那里,如果出于任何原因我们选择使用C malloc而不是C ++ new,那么



干杯,


- ALf


-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么这么糟糕?

A:热门发布。

问:usenet和电子邮件中最烦人的事情是什么?



In C, casting the result of malloc is Evil. One reason is that if the
relevant #include for malloc has been omitted, the cast may do Very
Unholy Things, where the malloc result pointer is converted to an int
which is in turn converted to a typed pointer, with no diagnostic. In
C++ we have stronger typing and hence in C++ the cast should be there,
if for any reason we''d choose to use C malloc instead of C++ new.

Cheers,

- ALf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


这篇关于阵列做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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