通过struct分配数组 [英] Array assignment via struct

查看:56
本文介绍了通过struct分配数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!


我正在研究自动生成的代码,需要分配数组。

memcpy是一个明显的解决方案,但它变成了很难在

上下文中使用,我正在研究,即:我可以使用它,但我不想这样做。

数组无法分配C,但结构可以,所以我编码了

以下:


#include< stdlib.h>


int main(void)

{

void * a = malloc(10); / *显然应该检查malloc

失败* /

void * b = malloc(10); / *这也是...... * /


*((struct copy {unsigned char t [10];} *)a)= *((struct copy *)b);


免费(一);

免费(b);

返回0;

}


我不会认为这会编译,更别提没有给出一个

单警告(gcc -W -Wall -pedantic)。它甚至可以工作:-),因为我试着复制并显示字符串
。我可以在我的代码中轻松编写代码

generator(实际上是一个翻译器),我想它甚至会让我将
表达式转换为数组!


我想知道,因为这不是一个很糟糕的解决方案,

是:

1)这个解决方案是否常见?

2)这是便携式吗?

3)这是否真的尊重C标准? (如果不是,为什么?)

4)如果不使用函数改进这个赋值的任何方法?


仅供参考,我尝试使用匿名结构:

*((struct {unsigned char t [10];} *)a)= *((struct {unsigned char

t [10];} *)b);

但编译器抱怨不兼容的类型,我知道原因。
了解原因。不过我不得不尝试一下。


谢谢

-

Eric Laberge

Hi!

I''m working on automatically generated code, and need to assign arrays.
memcpy is an obvious solution, but it becomes complicated to use in the
context I''m working on, ie.: I could use it but I don''t want to.
Arrays cannot be assigned in C, but structs can, so I coded the
following:

#include <stdlib.h>

int main(void)
{
void* a = malloc(10); /* This obviously should be checked for malloc
failure */
void* b = malloc(10); /* This too... */

*((struct copy {unsigned char t[10];}*) a) = *((struct copy*) b);

free(a);
free(b);
return 0;
}

I wouldn''t have tought that this would compile, let alone not give a
single warning (gcc -W -Wall -pedantic). It even works :-), as I tried
copying and displaying a string. I can easily code that in my code
generator (a translator, actually) and I suppose it would even let me
cast expressions to an array!

What I''m wondering, since this is not too bad looking as a solution,
is:
1) Is this solution common?
2) Is this portable?
3) Does this actually respects the C standard? (if not, why?)
4) Any way to improve this assign without using a function?

FYI, I tried using anonymous structures:
*((struct {unsigned char t[10];}*) a) = *((struct {unsigned char
t[10];}*) b);
but the compiler complained about incompatible types, which I
understand the cause. I had to try it, though.

Thanks
--
Eric Laberge

推荐答案

在文章< 11 ********************* @ g47g2000cwa.googlegroups中。 com>,

Eric Laberge< de ******** @ myrealbox.com>写道:
In article <11*********************@g47g2000cwa.googlegroups. com>,
Eric Laberge <de********@myrealbox.com> wrote:

*((struct copy {unsigned char t [10];} *)a)= *((struct copy *)b);

我在想什么,因为这不是一个糟糕的解决方案,
是:
1)这个解决方案是否常见?


将结构指针的void指针强制转换为*非常常见;然而,

在声明中声明一个新的结构......那部分是不寻常的。

2)这是可移植的吗?


我想是的。我尝试了三个完全独立的

编译器,他们都在c89和c99模式下接受它。

3)这是否真的尊重C标准? (如果不是,为什么?)


Dunno。希望有人能够更加流利地使用这些标准吗?b
表示它是否符合要求以及达到什么级别(并且

恢复我对我最喜欢的三个编译器的信心!)。 br />
4)如何在不使用函数的情况下改进此赋值?

*((struct copy {unsigned char t[10];}*) a) = *((struct copy*) b);

What I''m wondering, since this is not too bad looking as a solution,
is:
1) Is this solution common?
Casting void pointers to struct pointers is *very* common indeed; however,
declaring a new struct in the middle of a statement...that part is unusual.
2) Is this portable?
I think so. I tried it on three completely independent
compilers and they all accepted it in c89 and c99 modes.
3) Does this actually respects the C standard? (if not, why?)
Dunno. Hopefully someone more fluent with the standard(s) can
indicate whether it is conforming and to what level (and
restore my confidence in my three favorite compilers!).
4) Any way to improve this assign without using a function?




我最后一次移动大小未知的小块数据我是个b $ b但可以通过程序知道,我有一个不同的处理方式的菜单:


*小件1件, 2,4或8个字节被转换为内置类型的

适当的大小(例如,通过双倍的字符)。

*奇怪的大小的东西,未对齐的数据,以及更大的物体刚刚通过

到memcpy()。

*重叠数据当然得到memmove()。


做一个源代码中的struct copy可能会映射到二进制文件中的memcpy(或更糟糕的是,

memmove)。你将不得不检查程序集

输出,甚至可能对代码进行分析,以便处理最佳的b $ b $(这似乎是你想要避免

memcpy)。


在做其他事情的过程中声明一个结构是

至少在视觉上有点刺耳。我可能会将结构

声明向上和向下移动,或者至少用


替换声明

do {

struct copy {unsigned char t [10];};

*((struct copy *)a)= *((struct copy *)b);

}而(0)


并且可能跳过do while(0)的东西它是直线生成的

代码,但为结构分配唯一的名称。

-

7842 ++



The last time I had to move small chunks of data of size unknown-by-me
but knowable-by-the-program, I had a menu of different ways to handle it:

* Small items of 1, 2, 4, or 8 bytes were cast to built-in types of
the appropriate size (e.g. char thru double).
* Odd sized things, misaligned data, and larger objects were just passed
to memcpy().
* Overlapping data of course gets memmove().

Doing a struct copy in the source code might map to a memcpy (or worse,
memmove) in the binary anyway. You''ll have to examine the assembly
output and maybe even profile the code to get a handle on what''s
optimal (which seems to be the subtext of your wanting to avoid
memcpy).

Declaring a struct in the middle of doing something else is
at least visually jarring. I''d probably move the struct
declaration up and out, or at least replace the statement
in place with

do {
struct copy {unsigned char t[10];};
*((struct copy *) a) = *((struct copy*) b);
} while (0)

and maybe skip the do while(0) stuff it''s straight-line generated
code, but assign unique names to the structs.
--
7842++


On Thu, 2005年8月4日15:41:10 -0700,Eric Laberge写道:
On Thu, 04 Aug 2005 15:41:10 -0700, Eric Laberge wrote:
我正在研究自动生成的代码,需要分配数组。
memcpy是一个显而易见的解决方案,但在我正在研究的上下文中使用起来很复杂,即:我可以使用它但我不想这样做。
数组不能用C语言分配,但结构可以,所以我编码了以下内容:

#include< stdlib.h>

int main(void)
{
void * a = ma LLOC(10); / *这显然应该检查malloc
失败* /
void * b = malloc(10); / *这也是...... * /

*((struct copy {unsigned char t [10];} *)a)= *((struct copy *)b);

免费(一);
免费(二);
返回0;
}
I''m working on automatically generated code, and need to assign arrays.
memcpy is an obvious solution, but it becomes complicated to use in the
context I''m working on, ie.: I could use it but I don''t want to.
Arrays cannot be assigned in C, but structs can, so I coded the
following:

#include <stdlib.h>

int main(void)
{
void* a = malloc(10); /* This obviously should be checked for malloc
failure */
void* b = malloc(10); /* This too... */

*((struct copy {unsigned char t[10];}*) a) = *((struct copy*) b);

free(a);
free(b);
return 0;
}




< snip>


我能看到的唯一问题是结构不一定与数组大小相同 - 编译器可以自由添加填充到< br $>
结构。


最好先申报结构并在其上做尺寸检查。


不太便携因为在不同的平台上可能会添加不同数量的
填充。 Memcpy可能更简单。



<snip>

The only problem I can see is that the structure is not necessarily
the same size as the array - the compiler is free to add padding to the
structure.

Better to declare the structure first and do sizeof on it to check.

Not really portable because on different platforms different amounts of
padding may be added. Memcpy is probably simpler.


Netocrat写道:
Netocrat wrote:

2005年8月4日星期四15:41:10 -0700,Eric Laberge写道:

On Thu, 04 Aug 2005 15:41:10 -0700, Eric Laberge wrote:
我正在研究自动生成的代码,
需要分配数组。
memcpy是一个明显的解决方案,<但是在我工作的
上下文中使用它变得很复杂,即:我可以使用它但我不想这样做。
数组不能用C赋值,但结构可以,所以我编码了以下内容:

#include< stdlib.h>

int main(void)
{
void * a = malloc(10);
/ *这显然应该检查malloc
失败* /
void * b = malloc(10); / *这也是...... * /

*((struct copy {unsigned char t [10];} *)a)= *((struct copy *)b);

免费(一);
免费(二);
返回0;
}
I''m working on automatically generated code,
and need to assign arrays.
memcpy is an obvious solution,
but it becomes complicated to use in the
context I''m working on, ie.: I could use it but I don''t want to.
Arrays cannot be assigned in C, but structs can, so I coded the
following:

#include <stdlib.h>

int main(void)
{
void* a = malloc(10);
/* This obviously should be checked for malloc
failure */
void* b = malloc(10); /* This too... */

*((struct copy {unsigned char t[10];}*) a) = *((struct copy*) b);

free(a);
free(b);
return 0;
}



< snip>

我能看到的唯一问题是结构不一定与数组大小相同
- 编译器可以自由地为结构添加填充。
最好首先声明结构并在其上执行sizeof检查。



<snip>

The only problem I can see is that the structure is not necessarily
the same size as the array
- the compiler is free to add padding to the structure.

Better to declare the structure first and do sizeof on it to check.




对malloc调用中的参数使用sizeof结构。


/ * BEGIN new.c * /


#include< stdlib.h>

#include< string.h>

#include< stdio.h>


struct copy {

unsigned char t [10];

};


int main(无效)

{

void * a = malloc(sizeof(struct copy) );

void * b = malloc(sizeof(struct copy));


if(a == NULL || b == NULL){

put(购物中心oc problem");

退出(EXIT_FAILURE);

}

strcpy(b," 123456789");

*(struct copy *)a = *(struct copy *)b;

free(b);

puts(a);

免费(a);

返回0;

}


/ * END new.c * /

-

pete



Use sizeof structure for the argument in the call to malloc.

/* BEGIN new.c */

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

struct copy {
unsigned char t[10];
};

int main(void)
{
void *a = malloc(sizeof(struct copy));
void *b = malloc(sizeof(struct copy));

if (a == NULL || b == NULL) {
puts("malloc problem");
exit(EXIT_FAILURE);
}
strcpy(b, "123456789");
*(struct copy *)a = *(struct copy *)b;
free(b);
puts(a);
free(a);
return 0;
}

/* END new.c */
--
pete


这篇关于通过struct分配数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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