Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » fread/fwrite problems!

This thread is locked; no one can reply to it. rss feed Print
 1   2 
fread/fwrite problems!
Karadoc ~~
Member #2,749
September 2002
avatar

Yes, members of structs are never reordered; polymorphism in C relies on it!

struct BASE_CLASS
{
  int a, b, c;
};
struct DERIVED_CLASS
{
  int a, b, c;
  float extra_data;
};

Polymorhism in C is done by type casting to and from stuff that looks like that.

eg.

float my_function(struct BASE_CLASS *foo)
{
  float bar = foo->a * foo->b;
  if (foo->c == 5)
  {
    bar*=((DERIVED_CLASS*)foo)->extra_data;
  }
  return bar;
}

-----------

Michael Jensen
Member #2,870
October 2002
avatar

Erm that doesn't seem relibable. Did I miss something or is the struct automatically padded? And how much padding is it safe to rely on, is there?

1struct sta
2{
3 int a, b, c;
4};
5 
6struct stb
7{
8 int a, b, c;
9 int d[5120];
10};
11 
12void this_shouldnt_work_should_it() //?
13{
14 sta A;
15 stb * B = (stb)&A;
16 
17 B->d[4000] = 15;
18}

How can one be gauranteed that enough padding will be added?

HoHo
Member #4,534
April 2004
avatar

That one definitely shouldn't work. If it compiles then it should segfault if you try writing to the char array

__________
In theory, there is no difference between theory and practice. But, in practice, there is - Jan L.A. van de Snepscheut
MMORPG's...Many Men Online Role Playing Girls - Radagar
"Is Java REALLY slower? Does STL really bloat your exes? Find out with your friendly host, HoHo, and his benchmarking machine!" - Jakub Wasilewski

Bob
Free Market Evangelist
September 2000
avatar

Quote:

Since doubles/floats are defined specifically by IEEE, why arn't they platform/etc dependant?

In addition to what gillius said, not all computers use IEEE floats.

--
- Bob
[ -- All my signature links are 404 -- ]

Michael Jensen
Member #2,870
October 2002
avatar

Quote:

should segfault

Right, that's what I'd expact.

By those principals -- Why can what Karadoc~~ posted be guaranteed to work?

da_flo
Member #1,907
February 2002
avatar

Karadoc~~'s code is garanteed to work if c==5 only the type of the object is DERIVED_CLASS. It would be more clear if the variable was named 'type'. It's just a matter of design. It's up to the programmer to ensure the types are right.
To perform polymorphism in C you have to keep track of the type of the object anyway.

Michael Jensen
Member #2,870
October 2002
avatar

Quote:

if (foo->c == 5)

Ahh, I didn't see that before.

Evert
Member #794
November 2000
avatar

Quote:

Why can what Karadoc~~ posted be guaranteed to work?

You can acces the elements common to sta and stb, but you cannot acces elements specific to stb by casting sta to stb.

Think about it: sta is the base, stb is a derived.

Michael Jensen
Member #2,870
October 2002
avatar

Right that's what I thought, then I saw Karadocs code and I thought he was implying that you could -- I didn't see the c==5 thing....

 1   2 


Go to: