Post by r***@gmail.comIf I am allocating memory of 38 bytes to an integer pointer, what will
be the memory actually allocated?
You mean if you write
int *malloced = malloc( 38 );
in a function (with all suitable #includes and whatever)?
You will get a pointer to 38 bytes of (uninitialised) memory [1]. What this
means is that those 38 bytes can be written to and (then) read from,
but that trying to access bytes after (or before) those 38 gets you
undefined behaviour, as does computing the address of any bytes outside
the 38 (+1 for the Special Exception) [2].
I note that if sizeof(int) doesn't exactly divide 38, and you really wanted
to use the memory for ints, you'll have some useless bytes hanging around
at the end.
Post by r***@gmail.comWill there be any memory alignment concept in malloc?
The Standard requires that the memory returned by malloc is suitably aligned
for any C data-type. How the implementation achieves this is left to it
(but each implementation of course knows what the alignment requirements
of its data-types are, so it's not hard to do, just not portable).
[1] Or null.
[2] The implementation of undefined behaviour need not /do/ anthing; it
could quite cheerfully read from random other variables (Bad) or
write to them (Very Extremely Horribly RunAway! Bad).
--
Chris "running is for the jetpackless" Dollin
Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England