Advertisement

General questions

Started by July 10, 2000 01:01 PM
4 comments, last by braves 24 years, 5 months ago
I just have a few general questions if someone don''t mind helping me. 1) I always see these special compiler functions in code, functions like __declspec (). Now whenever I read C books(I write C not C++) I never see these functions. So how do people learn about these functions? Do they just read the entire help files to find them? Do you really need to know these special compiler functions to become a good programmer? 2) What is meant by bytes being aligned? Does it matter if your bytes are aligned and not alighed? How do you make them alighed? 3) I seen someone talking about "guard bytes", what the heck is a guard byte? 4) I seen alot of people talking about BeOS but no drivers for it. Can you download the operating system for free and what''s the recommended cpu speed and RAM for it? If anyone can help, thank you very much.
>> 1) I always see these special compiler functions in code, functions like __declspec (). Now whenever I read C books(I write C not C++) I never see these functions. So how do people learn about these functions? Do they just read the entire help files to find them? Do you really need to know these special compiler functions to become a good programmer? <<

You don't need to know all of those special things, but it might help. You can learn about them by reading the compilers manual (they should be listed there).

- Muzzafarath

Mad House Software
The Field Marshals

Edited by - Muzzafarath on July 10, 2000 3:13:49 PM
I'm reminded of the day my daughter came in, looked over my shoulder at some Perl 4 code, and said, "What is that, swearing?" - Larry Wall
Advertisement
2> Byte alignment can speed up certain memory accesses. For example if something is aligned to 32bits (4bytes) the starting memory location should be divisable by 4. This will, in general speed up accesses. Some processors will actually cause an exception if it tries to read a long word (4bytes) that isn''t aligned to 4bytes. Typically malloc, new etc will supply you with a memory address that is aligned for optimal speed on the host processor. Making sure you''re structures are aligned to 4bytes can increase access speed also.

eg.
typedef struct _tagRGB
{
char red;
char green;
char blue;
} RGB, *LPRGB;

is three bytes, a more optimum layout might be...

typedef struct _tagRGB
{
char red;
char green;
char blue;
char pad; // Align to four bytes
} RGB, *LPRGB;

Note this is just an example, before I get picked at

This last structure forces an array of RGB''s to be on a 4byte alignment. Sometimes speeding up processing. The optimum alignment of data can vary depending on the processor. It will need to be looked up. There isn''t much point in aligning a single byte to 4bytes as it is only accessing one byte, the same for double bytes.

3> Guard bytes are usually some kind of indentifier placed at the start and end of memory blocks. If these ever change from their initial value then you can tell that something has gone wrong with the memory access (either flooding past the end of data, or the begining).

4) You can download BeOS for free, but unfortunately thats as far as my knowledge of it goes
HTH

n!
Ok, I understand the part with the struct's. Say you just have a bunch of variables. Is it better to do char a,b,c,d; rather than char a,b,c;? Is it also better to use int a; because it's 32 bits rather than short a; because it's only 16 bits?
Another question. I am about to make my first graphics program and i was wondering if it would be better to use somethinlg like OpenGL or DirectX or use DOS and make all your functions like blit so you also get the background?
Thanks,
braves



Edited by - braves on July 10, 2000 4:23:45 PM
nope, because char''s take up 1byte (on all the systems I''ve used anyway ). Accessing a single char is a single byte read. So you can just define them as char a,b,c;
However, when declaring local variables (local''s are usually stored on the stack) this may unalign a following long.
However Most compilers will make sure that longs are aligned, meaning there would be an empty byte.

so, locally (and sometimes even globally), the following may take up the same amount of memory...

char a,b,c; // Three bytes (compiler generated alignment byte (depending on compiler/platform))
char a,b,c,d; // Four bytes, No extra alignment

You don''t have to declare the d unless you have a use for it

A reasonable compiler will support the following too:

char a,b,c; // Three bytes, extra byte needed for alignment
char d; // Oh hang on, this aligns for us.

It''s all compiler specific, really. And for the majority of your definition you can probably ignore it. Structures that are accessed a lot are more important.

struct _tagEXAMPLE
{
char a;
int i; // Erk, alignment error, slower access
} EXAMPLE, *LPEXAMPLE;

struct _tagEXAMPLE
{
char a, pad[ 3 ];
int i;
} EXAMPLE, *LPEXAMPLE;

would be a better layout. You should expect the compiler to align global single definitions really. Some compilers have options to align the above structure also, however I prefer to specify the padding. So the compiler doesn''t confuse me with invisible data.
HTH

n!
Any compiler worth its salt should pad out and align structures anyway, without you having to do anything. You should use ints over shorts whenever possible because this will be the size of integer that your computer''s best at handling (usually 32 bits). Chars are a different issue though. It depends on the context of the problem.

Regarding BeOS... It''s lovely. How practical it is to use will obviously depend on what you want to use it for. Yes it''s free, and easy to download and install. Minimum spec is a low-end Pentium-a-like (I think) with 16MB of RAM and at least 500MB of hard disk space. The download''s a lot less than 500MB though. www.be.com

Cheers,


=> Arfa <=
=> Arfa <=

This topic is closed to new replies.

Advertisement