[archimedes] Re: Fragen zum C-Programm Uhrzeit

  • From: Alexander Ausserstorfer <bavariasound@xxxxxxxxxxxxxxx>
  • To: archimedes@xxxxxxxxxxxxx
  • Date: Wed, 04 Jan 2017 10:19:32 +0200

In message <766437f855.Alex@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>
          Alexander Ausserstorfer <bavariasound@xxxxxxxxxxxxxxx> wrote:

Insbesondere habe ich da noch ein Verständnisproblem. Im Quellcode
findet man die Zeilen

| oswordreadclock_local_string_block *systemclock;
| systemclock = malloc(sizeof(oswordreadclock_local_string_block));
| systemclock->op = oswordreadclock_OP_LOCAL_STRING;
| oswordreadclock_local_string (systemclock);

Ich wollte es eigentlich so machen:

| oswordreadclock_local_string_block systemclock;
| systemclock.op = oswrodreadclock_OP_LOCAL_STRING;
| oswordreadclock_local_string (&systemclock);

Letzteres funktioniert jedoch nicht. Ist hier das Problem, dass
&systemclock kein Zeiger ist, sondern eine Adresse liefert? Gibt es
sonst noch eine Möglichkeit, malloc(...) zu umgehen? Ich dachte
eigentlich, wenn ich eine Struktur

oswordreadclock_local_string_block systemclock;

anlege, so müsste diese bereits den richtigen Speicher für sich
reservieren?

Dazu habe ich inzwischen eine Antwort in comp.sys.acorn.programmer
(Usenet) erhalten:

| Neither version seems to allocate enough memory for the string.
|
| On my system oslib.h.osword defines oswordreadclock_local_string_block
| as
|
| union oswordreadclock_local_string_block
|    {
|     oswordreadclock_op op;
|     struct
|     { char c [UNKNOWN];
|     }
|     string;
|    };
|
| The constant UNKNOWN is defined as 1 in oslib.h.types.
| So only one byte is being allocated to the string.
| In your local declaration the value returned from
| the call to oswordreadclock_local_string(systemclock)
| is probably getting overwritten, and you get away with
| the malloc version because the returned value is just
| written past the end of the allocated block in the malloc
| heap.
|
| One quick solution, but maybe not the best, might
| be to declare an osword_block (oswdblk) and set the pointer
| systemclock to &oswdblk.string since the osword_block is a
| union and there is a sixty four byte block of chars declared
| in it (char reserved[64];). So there will be at least 64
| bytes allocated for the osword_block.

Scheinbar ist die Struktur von OSlib nicht richtig definiert worden oder
kann nicht richtig definiert werden. Folglich wird nicht genug Speicher
für die Aufnahme der Uhrzeit in Textform reserviert. Zumindest verstehe
ich das so. Das ist jetzt erstmal blöd.

A.

-- 
http://home.chiemgau-net.de/ausserstorfer/
Egal was, Hauptsache studiert!

Other related posts: