Re: poniter problem

1999-03-28 Thread Glynn Clements


Amol Mohite wrote:

 So when gcc pads it to 8 bytes, can we say for sure that next allocation
 might be at 8byte boundary ?

No. You can be sure that it will satisfy any CPU-imposed alignment
requirements, but that's all.

-- 
Glynn Clements [EMAIL PROTECTED]



Re: poniter problem

1999-03-27 Thread Amol Mohite


Hi!

So when gcc pads it to 8 bytes, can we say for sure that next allocation
might be at 8byte boundary ?




Re: poniter problem

1999-03-27 Thread Amol Mohite


Thanks 

I meant something like :

*i = ((int *)c++);

I guess this will work like doing (int *)++c.

Sorry i meesd the brackets.

will be more clear next time.

Thank anyway.





Re: poniter problem

1999-03-26 Thread Glynn Clements


Amol Mohite wrote:

 say I have a char *c pointing to an array of 10 bytes.
 
 When i do (int *)c++ (increment before typecast -- forget the brackets for
 nw), and equate it t  an int *i then
 
 *i will return 2nd to fifth bytes as integer. Is this correct ?

No. `(int *)++c' or `(int *)(c+1)' would do that. `(int *)c++' stores
the result before the increment occurs.

 And supose the typecase is beore the incerenent like so :
 ((int *)c)++ then *i will return 5th to 8th byes. is this correct ?

++((int *)c) will do that, but again `((int *)c)++' stores the result
before the increment occurs.

 aslo how is exception handling implemented ? Does the processor have
 exception registers ?

What sort of exception? Hardware exceptions (e.g. segfault) result in
the processor jumping to a defined location. The details depend upon
the particular type of processor.

 Also if I have allocated a struct liek :
 
 struct {
   int i;
   char b;
 }str;
 
 which is obviously 5 butes large.

1. The ANSI standard doesn't specify that `sizeof(int) == 4', so you
shouldn't assume that it is. If you want an integer of a specific
size, use the types from sys/types.h, e.g. int32_t.

2. It's 8 actually bytes on x86 using gcc. The structure is padded to
a whole number of words. If you wanted to pack the structure, use:

struct {
int i;
char b;
} __attribute__ ((packed)) str;

[NB: this is a gcc extension.]

-- 
Glynn Clements [EMAIL PROTECTED]



Re: poniter problem

1999-03-26 Thread James

On Fri, 26 Mar 1999, Amol Mohite wrote:

# say I have a char *c pointing to an array of 10 bytes.

you mean:

char myarray[10], *c;

c = myarray;

# When i do (int *)c++ (increment before typecast -- forget the brackets for
# nw), and equate it t  an int *i then

please re-read what you type. this is full of typos and makes it really
difficult to read. Also include example code as it makes it more clear.

(int *)c++; doesn't do much except make c point at myarray[1] and return
the value typecast as an int (thus returning the character's ascii code).
 
# *i will return 2nd to fifth bytes as integer. Is this correct ?

no, first make i point to the array. typecasting in that way doesn't
convert the types of the elements in that array. To get the 2nd - 5th
bytes (elements) as integers you'd have to:

int cnt;

for (cnt = 1; cnt  5; cnt++)
printf ("%d\n", (int *)(c+cnt)); /* you have to do something with */
 /* the typecast variables */

except why bother with pointers to array elements?

for (cnt = 1; cnt  5; cnt++)
printf ("%d\n", (int *)myarray[cnt]);

# And supose the typecase is beore the incerenent like so :
# ((int *)c)++ then *i will return 5th to 8th byes. is this correct ?

no. that would cast c to an integer and then return the next one, which is
of type char.
 
 
# aslo how is exception handling implemented ? Does the processor have
# exception registers ?

what do you mean by exceptions? things like running out of memory and
opening non-existant files, etc? to deal with these the functions that
do the file i/o, memory allocation, whatever. return values that you
check.
 
# Also if I have allocated a struct liek :
# 
# struct {
#   int i;
#   char b;
# }str;
# 
# which is obviously 5 butes large.

not always. it will be

sizeof (int) + sizeof (char)

which on my machine (P233) is 5 bytes. Someone running a Sun or Alpha may have
different sized variables.
 
# Than whenre will the proceessor allocate the next bit of memory ?
# Directly after the char b byte?

it doesn't matter where it goes. All you need to know, as a C programmer,
is that you have some struct called str which contains an integer and a
character.

Do you own a good C book? (the Kernighan  Ritchie ANSI C Programming Book
is a good one. Ones that mention Turbo C, or other DOS things, aren't - for
Linux programming that is)

-- 
+++  If at first you don't succeed, you must be a programmer +++
[EMAIL PROTECTED] http://www.users.globalnet.co.uk/~kermit