Re: memcpy() where assignment would do?
- From: kuyper <kuyper@xxxxxxxxxx>
- Date: Fri, 24 Aug 2007 20:11:39 -0700
neildfergu...@xxxxxxxxxxx wrote:
On Thu, 23 Aug 2007 08:27:34 -0700, kuyper <kuyper@xxxxxxxxxx> wrote:
I've run across some rather peculiar code; here are the relevant lines
that left me confused :
unsigned char cr_file[384];
unsigned char num_char[0];
Note: this declaration actually works on our compiler, and it appears
to be equivalent to giving a length of 1. The developer inserted
compiler options into the make file to turn off the relevant warning
messages. Sadly, this is not the most confusing part of the code. This
is an example of the confusing part:
num_char[0] = 1;
memcpy(&cr_file[147], num_char, 1);
num_char is used only in this fashion; its value after the call to
memcpy() has no bearing on the behavior of the program. I may be
missing something, but it seems to me that this code is therefore
exactly equivalent to
cr_file[147] = 1;
In fact, I would expect that some compilers would generate identical
code for both ways of writing it.
Am I missing something? If not, could someone at least suggest a
plausible reason why the developer might write such bizarre code? I
can't ask the developer, he died recently, which is how I became
responsible for this code.
Here's a very off-topic possibility: the variables are defined as they are in
order to appear in particular segments of the linker's memory map, with
particular symbolic identification, so that the memory's addresses can be
associated with particular hardware I/O operations.
Ingenious possibility, but unfortunately not a plausible explanation
for this program. It's only purpose is breaking up a large partitioned
data set into several smaller files, which is non-trivial only because
the records are variable length, the files have to be split at the
boundary between two records, and each output file requires a seperate
header. cr_file is the array containing that header. It's not a very
complicated program. As a result, this wierd way of filling in the
headers adds significantly and unnecessarily to the total complexity.
For now, I'm assuming that he didn't have a valid reason for writing
the code this way. I suspect that I'll probably never learn what the
invalid reason was that motivated him to do so.
.
- References:
- memcpy() where assignment would do?
- From: kuyper
- Re: memcpy() where assignment would do?
- From: neildferguson
- memcpy() where assignment would do?
- Prev by Date: Re: HeathField Strange Ideas!
- Next by Date: Re: How to find the alignment requirements of a give type in the C99 standard?
- Previous by thread: Re: memcpy() where assignment would do?
- Next by thread: Re: memcpy() where assignment would do?
- Index(es):
Relevant Pages
|