473,748 Members | 5,849 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

far pointers

Can any one explain what are far pointers and what is the difference
between malloc and calloc .Which is better ?
Nov 14 '05
37 2575
CBFalconer <cb********@yah oo.com> wrote:
Michael Wojcik wrote:
CBFalconer <cb********@yah oo.com> writes:
... snip ...

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?

Those are value bits. There may be trap bits, which are forbidden
in unsigned char objects.


But wouldn't it be reasonable to assume that "The space is initialized
to all bits zero." sentence from the standard about calloc() only
refers to these value bits? They are the only ones the programmer is
ever going to "see" and there's the footnote explicitely warning "Note
that this need not be the same as the representation of floating-point
zero or a null pointer constant." which I would tend to interpret that
for int's and long int's that problem does not exist.

Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@p hysik.fu-berlin.de
\______________ ____________ http://www.toerring.de
Nov 14 '05 #21
On Thu, 12 Aug 2004 08:52:50 GMT, in comp.lang.c , "Mabden"
<mabden@sbc_glo bal.net> wrote:
"Chris Torek" <no****@torek.n et> wrote in message
news:cf******* *@news3.newsguy .com...
In article <news:b8******* *************** ****@posting.go ogle.com>
Harsimran <sa************ @yahoo.co.in> wrote:
>and what is the difference between malloc and calloc .Which is better ?


Which is better, chocolate or strawberry; coconut or soy sauce;
a hammer or a wrench?


I'd rather be hammered than wrenched...

Coconut sauce???


Chicken Korma.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt >
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 14 '05 #22
On 12 Aug 2004 21:07:05 GMT
Je***********@p hysik.fu-berlin.de wrote:
CBFalconer <cb********@yah oo.com> wrote:
Michael Wojcik wrote:
CBFalconer <cb********@yah oo.com> writes:
... snip ...

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?

Those are value bits. There may be trap bits, which are forbidden
in unsigned char objects.


But wouldn't it be reasonable to assume that "The space is initialized
to all bits zero." sentence from the standard about calloc() only
refers to these value bits? They are the only ones the programmer is
ever going to "see"


The programmer might access the calloced area as unsigned char, in which
case ALL bits are value bits, so every bit needs to be initialised to 0
in a calloc call. In fact, I have code at work which callocs a buffer
then accesses the data as unsigned char.
and there's the footnote explicitely warning "Note
that this need not be the same as the representation of floating-point
zero or a null pointer constant." which I would tend to interpret that
for int's and long int's that problem does not exist.


The footnotes are not normative text, so that doesn't prove anything
unfortunately.
--
Flash Gordon
Sometimes I think shooting would be far too good for some people.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #23
Flash Gordon <sp**@flash-gordon.me.uk> wrote:
On 12 Aug 2004 21:07:05 GMT
Je***********@p hysik.fu-berlin.de wrote:
CBFalconer <cb********@yah oo.com> wrote:
> Michael Wojcik wrote:
>> CBFalconer <cb********@yah oo.com> writes:
>>
> ... snip ...
>>
>> 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?

> Those are value bits. There may be trap bits, which are forbidden
> in unsigned char objects.


But wouldn't it be reasonable to assume that "The space is initialized
to all bits zero." sentence from the standard about calloc() only
refers to these value bits? They are the only ones the programmer is
ever going to "see" The programmer might access the calloced area as unsigned char, in which
case ALL bits are value bits, so every bit needs to be initialised to 0
in a calloc call. In fact, I have code at work which callocs a buffer
then accesses the data as unsigned char. and there's the footnote explicitely warning "Note
that this need not be the same as the representation of floating-point
zero or a null pointer constant." which I would tend to interpret that
for int's and long int's that problem does not exist.

The footnotes are not normative text, so that doesn't prove anything
unfortunately.


Thanks. That of course leaves one wondering why calloc() takes two
arguments at all when it actually can be called safely only in
cases were the second one is 1 - but that could be due to historic
reasons, i.e. it was originally meant to allow initialization of
e.g. int arrays but for some reasons that couldn't be kept that
way.

But then there's still the memset() function. Do the same restrictions
apply as for calloc() or can it be safely used for intitialization of
an int array with zeros? The "into each of the first n characters" bit
seems to hint at the same problem as with calloc()...

Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@p hysik.fu-berlin.de
\______________ ____________ http://www.toerring.de
Nov 14 '05 #24
Edd
Harsimran wrote:
Can any one explain what are far pointers and what is the difference
between malloc and calloc .Which is better ?


I believe others have answered your question perfectly well, but while we're on
the subject I'd like to ask about something that's been niggling me for a while now.

One of my C programming books (A Book On C 4th edition, Kelley & Pohl) has the
following to say about malloc and calloc (page 663):
void *calloc(size_t n, size_t el_size);
Allocates contiguous space in memory for an array of n elements, with each
element requiring el_size bytes. The space is initialized with all bits set to
zero. A successful call returns the base address of the allocated space;
otherwise, NULL is returned.

void *malloc(size_t size);
Allocates a block in memory consisting of size bytes. The space is not
initialized. A successful call returns the base address of the allocated space;
otherwise, NULL is returned.
The niggle of which I speak is with respect to the "contiguous " part of the
calloc description; is the space allocated by a successful malloc call not
necessarily required to be contiguous?

I don't have a copy of the standard (yet) and so I'd really appreciate it if
someone could clear this up for me. As far as I can see, it would be daft for
malloc-ated space not to be contiguous?

Thanks.

Edd
Nov 14 '05 #25
Edd wrote on 13/08/04 :
void *malloc(size_t size);
Allocates a block in memory consisting of size bytes. The space is not
initialized. A successful call returns the base address of the allocated
space; otherwise, NULL is returned.

The niggle of which I speak is with respect to the "contiguous " part of the
calloc description; is the space allocated by a successful malloc call not
necessarily required to be contiguous?


No. Like for calloc() and realloc(), it has to be contiguous and well
aligned.

<ISO/IEC 9899:1999 (E)>
7.20.3 Memory management functions
1 The order and contiguity of storage allocated by successive calls to
the calloc,
malloc, and realloc functions is unspecified. The pointer returned if
the allocation
succeeds is suitably aligned so that it may be assigned to a pointer to
any type of object
and then used to access such an object or an array of such objects in
the space allocated
(until the space is explicitly deallocated). The lifetime of an
allocated object extends
from the allocation until the deallocation. Each such allocation shall
yield a pointer to an
object disjoint from any other object. The pointer returned points to
the start (lowest byte
address) of the allocated space. If the space cannot be allocated, a
null pointer is
returned. If the size of the space requested is zero, the behavior is
implementationd efined:
either a null pointer is returned, or the behavior is as if the size
were some
nonzero value, except that the returned pointer shall not be used to
access an object.
7.20.3.1 The calloc function
Synopsis
1 #include <stdlib.h>
void *calloc(size_t nmemb, size_t size);
Description
2 The calloc function allocates space for an array of nmemb objects,
each of whose size
is size. The space is initialized to all bits zero.252)
Returns
3 The calloc function returns either a null pointer or a pointer to the
allocated space.
</>

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

"C is a sharp tool"

Nov 14 '05 #26
In article <news:cf******* ***@news7.svr.p ol.co.uk>
Edd <ed*@NOSPAMHERE nunswithguns.ne t> wrote:
... while we're on the subject I'd like to ask about something
that's been niggling me for a while now. One of my C programming books (A Book On C 4th edition, Kelley &
Pohl) has the following to say about malloc and calloc (page 663):
[wording snipped, but calloc()'s description contains the word
"contiguous " while malloc()'s does not]
The niggle of which I speak is with respect to the "contiguous " part of the
calloc description; is the space allocated by a successful malloc call not
necessarily required to be contiguous?


Both return a contiguous memory region.

The calloc() description is "more deserving" of the extra word for
one simple reason: malloc() gets only one parameter -- the number
of bytes to allocate -- while calloc() gets two. Since "there is
no magic" (or was, at least, when C was first being written -- ISO
now allows arbitrary amounts of magic to occur between the compiler
and its Standard C library), a call of the form:

malloc(32)

is unable to tell whether you wanted 8 four-byte objects (e.g.,
malloc(n_obj * sizeof *obj) where n_obj is 8 and sizeof *obj is 4)
or whether you wanted a single 32-byte object (e.g.,
malloc(sizeof *obj2) where sizeof *obj2 is 32). On the other
hand, calloc gets a separate "number of objects" and "size of
each object":

calloc(n_obj, sizeof *obj) /* for the first case */
vs calloc(1, sizeof *obj2) /* for the second case */

Obviously calloc() has more information than malloc() -- so it
might, conceivably, be able to use that to call malloc() N times
instead of just once (and indeed, there was once a cfree() that
was to be used on calloc()'ed memory, and perhaps cfree() might
then call free() N times as well). If calloc() were allowed to
do that, it could get N discontiguous regions, and perhaps
chain them together into a linked list or something.

It does not do that, of course; and you free() the pointers you
get from calloc() just as you free() those from malloc(), so calloc()
just has to multiply its two parameters together (and check for
overflow). But because calloc() has, at first glance, enough
information to malloc() N times, the extra word "contiguous " has
some reason for being there, however weak that reason turns out to
be in the end.
--
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.
Nov 14 '05 #27
Je***********@p hysik.fu-berlin.de wrote:

CBFalconer <cb********@yah oo.com> wrote:
Michael Wojcik wrote:
CBFalconer <cb********@yah oo.com> writes:

... snip ...

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?

Those are value bits. There may be trap bits, which are forbidden
in unsigned char objects.


But wouldn't it be reasonable to assume that "The space is initialized
to all bits zero." sentence from the standard about calloc() only
refers to these value bits? They are the only ones the programmer is
ever going to "see" and there's the footnote explicitely warning "Note
that this need not be the same as the representation of floating-point
zero or a null pointer constant." which I would tend to interpret that
for int's and long int's that problem does not exist.


But calloc initializes bytes. Integers may occupy an integral
number of bytes, but they do NOT have to use all of their bits as
value bits. Admittedly such an actual machine would be unusual
and probably have marketing problems, but it is not foreclosed by
the C standard.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 14 '05 #28
Je***********@p hysik.fu-berlin.de wrote:
.... snip ...
But then there's still the memset() function. Do the same restrictions
apply as for calloc() or can it be safely used for intitialization of
an int array with zeros? The "into each of the first n characters" bit
seems to hint at the same problem as with calloc()...


No. It is not presently guaranteed.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 14 '05 #29
CBFalconer <cb********@yah oo.com> wrote:
Je***********@p hysik.fu-berlin.de wrote:

CBFalconer <cb********@yah oo.com> wrote:
> Michael Wojcik wrote:
>> CBFalconer <cb********@yah oo.com> writes:
>>
> ... snip ...
>>
>> 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?
> Those are value bits. There may be trap bits, which are forbidden
> in unsigned char objects.


But wouldn't it be reasonable to assume that "The space is initialized
to all bits zero." sentence from the standard about calloc() only
refers to these value bits? They are the only ones the programmer is
ever going to "see" and there's the footnote explicitely warning "Note
that this need not be the same as the representation of floating-point
zero or a null pointer constant." which I would tend to interpret that
for int's and long int's that problem does not exist.

But calloc initializes bytes. Integers may occupy an integral
number of bytes, but they do NOT have to use all of their bits as
value bits. Admittedly such an actual machine would be unusual
and probably have marketing problems, but it is not foreclosed by
the C standard.


Thanks for your (and everyone else's) replies.

Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@p hysik.fu-berlin.de
\______________ ____________ http://www.toerring.de
Nov 14 '05 #30

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
3407
by: Susan Baker | last post by:
Hi, I'm just reading about smart pointers.. I have some existing C code that I would like to provide wrapper classes for. Specifically, I would like to provide wrappers for two stucts defined as ff: typedef struct { float *data ; int count ;
3
3451
by: ozbear | last post by:
This is probably an obvious question. I know that pointer comparisons are only defined if the two pointers point somewhere "into" the storage allocated to the same object, or if they are NULL, or one-past the end of the object as long as it isn't dereferenced. I use "object" in the standard 'C' sense. Is there some special dispensation given to comparing two pointers
9
5063
by: Mikhail Teterin | last post by:
Hello! I'd like to have a variable of a pointer-to-function type. The two possible values are of type (*)(FILE *) and (*)(void *). For example: getter = straight ? fgetc : gzgetc; nextchar = getter(file); What type should I give to `getter' so that the compiler does not issue
12
4093
by: Lance | last post by:
VB.NET (v2003) does not support pointers, right? Assuming that this is true, are there any plans to support pointers in the future? Forgive my ignorance, but if C# supports pointers and C# and VB.NET get compiled into the same code, then I don't understand why VB.NET can't support pointers Thanks for any info Lance
14
2834
by: Alf P. Steinbach | last post by:
Not yet perfect, but: http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01.pdf http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01_examples.zip To access the table of contents, use the "Bookmarks" tab in Adobe Acrobat. Comments, corrections, praise, nits, etc., are still welcome!
92
5114
by: Jim Langston | last post by:
Someone made the statement in a newsgroup that most C++ programmers use smart pointers. His actual phrase was "most of us" but I really don't think that most C++ programmers use smart pointers, but I just don't know. I don't like them because I don't trust them. I use new and delete on pure pointers instead. Do you use smart pointers?
4
3511
by: Josefo | last post by:
Hello, is someone so kind to tell me why I am getting the following errors ? vector_static_function.c:20: error: expected constructor, destructor, or type conversion before '.' token vector_static_function.c:21: error: expected constructor, destructor, or type conversion before '.' token
25
13046
by: J Caesar | last post by:
In C you can compare two pointers, p<q, as long as they come from the same array or the same malloc()ated block. Otherwise you can't. What I'd like to do is write a function int comparable(void *p, void *q) that will take any two pointers and decide whether they can be compared or not. I really can't think how to do this - any suggestions?
54
12002
by: Boris | last post by:
I had a 3 hours meeting today with some fellow programmers that are partly not convinced about using smart pointers in C++. Their main concern is a possible performance impact. I've been explaining the advantages of smart pointers endlessly (which are currently used in all our C++ software; we use the Boost smart pointers) as I'm seriously concerned that there is a shift to raw pointers. We are not developing system software but rather...
2
2985
by: StevenChiasson | last post by:
For the record, not a student, just someone attempting to learn C++. Anyway, the problem I'm having right now is the member function detAddress, of object controller. This is more or less, your standard dynamic address book program. Adding, and listing work just fine. However, deleting, editing and viewing relies on member function retAddress. This function returns an array of pointers that are pointing to created objects. In action, all it...
0
8984
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8823
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9530
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9363
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9312
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9238
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6793
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
3300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.