English 中文(简体)
1. 点光器而不是肉类
原标题:Swap pointers instead of memcpy

www.un.org/Depts/DGACM/index_spanish.htm 我对我手法中的错误感到担忧,现在我看到这两种产出都是一样的。 下面是经过编辑的版本。

我要说的是,我有一个结构:

typedef struct
{
    char m[5];
    char f[6];
} COUPLE;

我读成一个阵列的文件:

char *data = malloc(11);
FILE *f = fopen("myfile", "rb");
fread(data, 1, 11, f);
fclose(f);

我总是在需要从一阵列中填满我的结构时使用这一守则:

COUPLE titanic;
memcpy(&titanic, data, sizeof(data));
printf("%s and %s", titanic.m, titanic.f);

这一工作非常出色,但事实上,我的边际阵列可能非常大,因此,我试图优化我的法典。

COUPLE *titanic = (COUPLE *)data;
printf("%s and %s", titanic->m, titanic->f);

因此,我的问题是:

  1. (obsolete) Why do I get different outputs?
  2. (obsolete) How can I fill a structure just by casting from an array?
  3. Should I avoid this kind of optimization?
  4. Are there possible pitfalls in it?
最佳回答

When made my comment to your question, I didn t have the time to elaborate, so here s an attempt to answer. The code has changed since, and I m not sure I would add that comment to the code as it is now.

Nevertheless, the underlying reason that made me add that comment still stands: Unless you measured and found that the code in question does indeed have a significant negative impact on performance, don t attempt to optimize. Instead, strive to make your code as readable as possible.

我严重怀疑,在将数据从磁盘复制到记忆之后,复制数据将产生重大的业绩影响。 然而,由于你的法典对记忆中的结构布局做了假设,直接读到结构中,这实际上使该守则无法读用(或不太容易受该排位变化的影响):

COUPLE titanic;
FILE *f = fopen("myfile", "rb");
fread(&titanic, sizeof(titanic), 1, f);

或者,如果你确实有阵列,直接读到阵列中:

COUPLE titanic[SIZE];
FILE *f = fopen("myfile", "rb");
fread(&titanic, sizeof(titanic), SIZE, f);

根据<代码>SIZE,后者确实可以在业绩方面产生巨大变化。 从总体上来说,比较小的草坪使用磁盘的速度更快。 (ough 软盘ach可能缓解这种情况)

问题回答

你所说的话并不真实。 如果用<代码>memcpy在座标上填满一个指示,你将不可行。 首先,当你这样做时

memcpy(&titanic, data, sizeof(data));

你们正在复制12个果园。 同时,该结构不能保证为12个大tes(总面积只有11个),因此,一般情况下会造成记忆超支。

其次,<代码>titanic.m中的焦炭阵列将不终止。 <代码>titanic.f也是如此,因此,您的<编码> 印本/<代码>绝对不改变产出编号<代码>、Romeo和Juliet><<>/code>,这是你不实的说法。

你声称,你的第一个实例是“工作罚款”(不问),而实际上第一个例子根本不可行,即使它“工作”受到与你第二个例子非常相似的问题(但一般情况下,具体问题是无法预测的)。

你试图使用的方法并不可行。 你们可以产生一种正确的结构,从一种原体中找到两条。 无法做到这一点的一个明显原因是,在两处,你需要两个零点,而你最初的扼杀只有一人。

  1. Because it s the members start at a different position. Printf() print a null () terminated sentence.
  2. You can do it like that. But you need to pay attention to the types and how to use them.
  3. It can be good if you know what you re doing.
  4. Lot s of pitfalls if you don t know what you re doing... There s no type safety.

我认为,唯一的解决办法是从档案到结构直接阅读的解决办法。 这可能是这样的。

typedef struct
{
    char m[6];
    char f[7];
} COUPLE;

COUPLE titanic;

FILE *f = fopen("myfile", "rb");
fread(titanic.m, 1, 5, f);
titanic.m[5] =   ;
fread(titanic.f, 1, 6, f);
titanic.f[6] =   ;
fclose(f);

printf("%s and %s", titanic.m, titanic.f);

当然,这个例子可能并不理想,因为我认为,听透镜和薄膜比2只好。 这种做法或另外两种解决办法都非常不现实,因为问题不现实——他们需要结构,储存5和6个字长。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签