Harsimran <sainiharsimran @yahoo.co.in> scribbled the following:[color=blue]
> Can any one explain what are far pointers and what is the difference
> between malloc and calloc .Which is better ?[/color]
Far pointers are not an ISO standard C concept and are thus off-topic
here. There are two differences between malloc and calloc:
1) malloc excepts one parameter, calloc excepts two. The size that
calloc allocates is the parameters multiplied together.
2) calloc automatically zeroes the allocated memory, malloc does not.
--
/-- Joona Palaste (palaste@cc.hel sinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"How can we possibly use sex to get what we want? Sex IS what we want."
- Dr. Frasier Crane
In article <news:b829bfa4. 0408112239.2a9f 242d@posting.go ogle.com>
Harsimran <sainiharsimran @yahoo.co.in> wrote:[color=blue]
>Can any one explain what are far pointers ...[/color]
There are no such things. (See the comp.lang.c FAQ, question 19.40d.)
[color=blue]
>and what is the difference between malloc and calloc .Which is better ?[/color]
Which is better, chocolate or strawberry; coconut or soy sauce;
a hammer or a wrench?
A call of the form malloc(n) returns either NULL (failure) or a
pointer to the first of n contiguous bytes. A call of the form
calloc(nitems, size) returns either NULL (failure) or a pointer
to the first of nitems*size contiguous bytes, after also calling
memset() to set all those bytes to '\0'.
If you need bytes pre-set to '\0' you can use calloc(); if you just
need bytes, use malloc().
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
"Chris Torek" <nospam@torek.n et> wrote in message
news:cff49j0o52 @news3.newsguy. com...[color=blue]
> In article <news:b829bfa4. 0408112239.2a9f 242d@posting.go ogle.com>
> Harsimran <sainiharsimran @yahoo.co.in> wrote:[color=green]
> >and what is the difference between malloc and calloc .Which is better ?[/color]
>
> Which is better, chocolate or strawberry; coconut or soy sauce;
> a hammer or a wrench?[/color]
I'd rather be hammered than wrenched...
Coconut sauce???
[color=blue]
> If you need bytes pre-set to '\0' you can use calloc(); if you just
> need bytes, use malloc().[/color]
Right, if you are going to malloc() some space then set it all to zero, you
might as well just use calloc(). This can sometimes be the "coward's way
out". If you don't think you can handle using your memory properly, and have
errors in calculating addresses where you are "one off", it's nice to have
the safety net of a zeroed out chunk of memory - no random values. For
instance, when storing strings in malloc'ed memory, if you forget the
terminating '\0', the calloc assures it is already there. So what I'm saying
is calloc() can hide subtle bugs and overruns, so the program may work, but
still have bugs.
I sometimes used calloc in beginning development, to get going, and switched
back to malloc to find bugs and overruns for the real code. But that was a
long time ago - now I want to know right away.
Can anyone come up with a good reason to zero out "raw" memory before you've
even used it? ...maybe to make sure there are zeroes in the empty
byte-aligned null space???
Mabden <mabden@sbc_glo bal.net> wrote:[color=blue]
> Can anyone come up with a good reason to zero out "raw" memory before you've
> even used it? ...maybe to make sure there are zeroes in the empty
> byte-aligned null space???[/color]
Needing an array of ints (or long ints) with all elements initialized
to 0 is more or less the only time I use calloc().
pete wrote on 12/08/04 :[color=blue][color=green]
>> Can any one explain what are far pointers[/color]
>
> It may have something to do with Borland.[/color]
Not only. It has somehing to do with x86 arch in real mode.
Jens.Toerring@p hysik.fu-berlin.de wrote:[color=blue]
> Mabden <mabden@sbc_glo bal.net> wrote:
>[color=green]
>> Can anyone come up with a good reason to zero out "raw" memory
>> before you've even used it? ...maybe to make sure there are
>> zeroes in the empty byte-aligned null space???[/color]
>
> Needing an array of ints (or long ints) with all elements
> initialized to 0 is more or less the only time I use calloc().[/color]
While that may work for you, it is not guaranteed by the standard.
--
Chuck F (cbfalconer@yah oo.com) (cbfalconer@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Joona I Palaste wrote:[color=blue]
> Harsimran <sainiharsimran @yahoo.co.in> scribbled the following:
>[color=green]
>> Can any one explain what are far pointers and what is the difference
>> between malloc and calloc .Which is better ?[/color]
>
> Far pointers are not an ISO standard C concept and are thus off-topic
> here. There are two differences between malloc and calloc:
> 1) malloc excepts one parameter, calloc excepts two. The size that[/color]
^^^^^^^ ^^^^^^^
accepts :-)
[color=blue]
> calloc allocates is the parameters multiplied together.
> 2) calloc automatically zeroes the allocated memory, malloc does not.[/color]
--
Chuck F (cbfalconer@yah oo.com) (cbfalconer@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
On Thu, 12 Aug 2004, CBFalconer wrote:[color=blue]
>
> Jens.Toerring@p hysik.fu-berlin.de wrote:[color=green]
>> Needing an array of ints (or long ints) with all elements
>> initialized to 0 is more or less the only time I use calloc().[/color]
>
> While that may work for you, it is not guaranteed by the standard.[/color]
Amplification (sorry!;) : memsetting an 'int' to zero is not
guaranteed by the Standard to set the actual /value/ of that 'int'
to the integer zero. (But I think there's some debate about that;
I forget the details.) So what Jens is suggesting doesn't work.
However, unsigned types are guaranteed to have pure binary
representations , which means that memsetting an 'unsigned int' to
zero /will/ set its value to the unsigned integer zero! Ditto
'unsigned char', 'unsigned long', et cetera. (And I think ditto
the new C99 fixed-width types: 'int32_t' and 'int_least8_t' and
friends.)
I often use 'calloc' in image processing; for example, to get
a grayscale image of size w*h initialized to black, I'll write
unsigned char *im = calloc(w*h, 1);
So 'calloc' does have its uses; they're just rare.
CBFalconer <cbfalconer@yah oo.com> wrote:[color=blue]
> Jens.Toerring@p hysik.fu-berlin.de wrote:[color=green]
>> Mabden <mabden@sbc_glo bal.net> wrote:
>>[color=darkred]
>>> Can anyone come up with a good reason to zero out "raw" memory
>>> before you've even used it? ...maybe to make sure there are
>>> zeroes in the empty byte-aligned null space???[/color]
>>
>> Needing an array of ints (or long ints) with all elements
>> initialized to 0 is more or less the only time I use calloc().[/color][/color]
[color=blue]
> While that may work for you, it is not guaranteed by the standard.[/color]
Mmm. Because there can be platforms where an int with a value
of 0 hasn't all bits set to zero or because there could be
platforms where an int doesn't use all the bits of the bytes
it consists of and an all bits zero initilization of these
bytes could result in a trap representation? Could you tell me
where I find that in the standard? And doesn't that necessarily
mean that using calloc() for anything else than char arrays
(or could '\0' also be something other than all bits zero?)
would be unportable (same for e.g. memset()?)
Harsimran wrote:
[color=blue]
> Can any one explain what are far pointers and what is the difference
> between malloc and calloc .Which is better ?[/color]
Far pointers can be discussed in news:comp.arch. embedded.
I'm sure you will get a wide range of answers. The basic
answer is that some processors have special facilities
for addresses that are near to the program counter and
other facilities for address that are far from the
program counter. For portable code, one should refrain
from any pointer quantifications .
As for malloc vs. calloc, I don't know which is "better"
because "better" is an opinionated word. In my 30 years
of programming, I have never used calloc(). If you don't
need the extra features of calloc() then use malloc().
In some applications, the extra features of calloc()
eat up precious processing time or code space.
Emmanuel Delahaye wrote:
[color=blue]
> pete wrote on 12/08/04 :
>[color=green][color=darkred]
>>> Can any one explain what are far pointers[/color]
>>
>>
>> It may have something to do with Borland.[/color]
>
>
> Not only. It has somehing to do with x86 arch in real mode.
>[/color]
or in *286* compatible protected mode.
In article <cff49j0o52@new s3.newsguy.com> , Chris Torek <nospam@torek.n et> writes:[color=blue]
> In article <news:b829bfa4. 0408112239.2a9f 242d@posting.go ogle.com>
> Harsimran <sainiharsimran @yahoo.co.in> wrote:
>[color=green]
> >and what is the difference between malloc and calloc .Which is better ?[/color]
>
> If you need bytes pre-set to '\0' you can use calloc(); if you just
> need bytes, use malloc().[/color]
IMHO, calloc is better avoided. Since all-bits-zero isn't guaranteed
to be meaningful, much less what the programmer likely expected, for
floating-point and pointer types, it's of less utility than it might
seem. Also, I've seen a lot of code that uses calloc and then goes
on to initialize the entire allocated area anyway, so calloc's zero-
fill is wasted.
In other words, calloc saves you a multiplication and a memset; the
former can trivially be done in the equivalent malloc call, and the
latter may not be useful (and can trivially be done after the malloc
call, if it succeeds).
I will shoue the world one of the grate Wonders of the world in 15
months if Now man mourders me in Dors or out Dors
-- "Lord" Timothy Dexter, _A Pickle for the Knowing Ones_
In article <411B703C.7B916 3A2@yahoo.com>, CBFalconer <cbfalconer@yah oo.com> writes:[color=blue]
> Jens.Toerring@p hysik.fu-berlin.de wrote:[color=green]
> > Mabden <mabden@sbc_glo bal.net> wrote:
> >
> > Needing an array of ints (or long ints) with all elements
> > initialized to 0 is more or less the only time I use calloc().[/color]
>
> While that may work for you, it is not guaranteed by the standard.[/color]
Isn't it? I thought we had agreed that in effect the only integer
representations that met the standard's requirements were sign-
magnitude, one's-complement, and two's-complement. In all three,
all-bits-zero is a representation for value 0; the first two can
also express "negative zero", but I was under the impression that
the rule requiring same representation for corresponding signed and
unsigned types ruled out "negative zero" as the canonical represen-
tation for value 0.
In short, initializing an array of ints or long ints to all-bits-
zero should, AFAICT, initialize each member of the array to value
0 on any conforming implementation. What am I missing?
Comment