473,748 Members | 4,697 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
"Arthur J. O'Dwyer" <aj*@nospam.and rew.cmu.edu> wrote in message news:<Pi******* *************** ************@un ix48.andrew.cmu .edu>...
On Thu, 12 Aug 2004, CBFalconer wrote:

Je***********@p hysik.fu-berlin.de 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().


While that may work for you, it is not guaranteed by the standard.


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.

-Arthur

I had gone through three books (just basics ones) but didnt find
memset memmove ect can any one plz explain it .
Nov 14 '05 #31
Harsimran wrote on 13/08/04 :
I had gone through three books (just basics ones) but didnt find
memset memmove ect can any one plz explain it .


A good reference for C functions:

http://www.dinkumware.com/refxc.html

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

"C is a sharp tool"

Nov 14 '05 #32
Harsimran <sa************ @yahoo.co.in> scribbled the following:
"Arthur J. O'Dwyer" <aj*@nospam.and rew.cmu.edu> wrote in message news:<Pi******* *************** ************@un ix48.andrew.cmu .edu>...
On Thu, 12 Aug 2004, CBFalconer wrote:
>
> Je***********@p hysik.fu-berlin.de 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().
>
> While that may work for you, it is not guaranteed by the standard.
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.

I had gone through three books (just basics ones) but didnt find
memset memmove ect can any one plz explain it .


They must be pretty crappy books then. memset is a function for setting
every byte in a range of bytes to a specific value. memmove is for
moving a range of bytes into another location in memory. Does this
answer your question?

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Show me a good mouser and I'll show you a cat with bad breath."
- Garfield
Nov 14 '05 #33
Joona I Palaste wrote on 13/08/04 :
memmove is for
moving a range of bytes into another location in memory.


Actually, no. memmove() is used to copy bytes in overlapping memory.

char s[] = "hello world";

memmove (s, s + 6, 5);

produces "world world"

Due to overlapping,

/* Don't do that */
memcpy (s, s + 6, 5);

would have produced an Undefined Behaviour.

(I'm quite sure you was aware of that...)

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

"C is a sharp tool"

Nov 14 '05 #34
On 13 Aug 2004 04:57:51 -0700
sa************@ yahoo.co.in (Harsimran) wrote:

<snip>
I had gone through three books (just basics ones) but didnt find
memset memmove ect can any one plz explain it .


I can offer three possible explanations.

1) The books were bad.
2) The books were aimed only at getting you just about started and
deliberately left out those functions.
3) You didn't read the books very carefully.

I would recommend that you get The C Programming Language 2nd edition by
Kernighan & Ritchie.
--
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 #35
Edd wrote:

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?

Note that the description of malloc() states it returns a "block in
memory". I can't think of any definition of "block" that would allow it
to be non-contiguous. I believe the author just used slightly different
phrasing.
Brian Rodenborn
Nov 14 '05 #36
Joona I Palaste wrote:
Harsimran <sa************ @yahoo.co.in> scribbled the following:
"Arthur J. O'Dwyer" <aj*@nospam.and rew.cmu.edu> wrote:
On Thu, 12 Aug 2004, CBFalconer wrote:
Je***********@p hysik.fu-berlin.de 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().

While that may work for you, it is not guaranteed by the standard.

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.

I had gone through three books (just basics ones) but didnt find
memset memmove ect can any one plz explain it .


They must be pretty crappy books then. memset is a function for setting
every byte in a range of bytes to a specific value. memmove is for
moving a range of bytes into another location in memory. Does this
answer your question?


and there is also memcpy() in the same group. Read all their
specifications.

--
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 #37
Harsimran wrote:
Can any one explain what are far pointers and what is the difference
between malloc and calloc .Which is better ?


Others have answered very well about the difference between malloc and
calloc, but like most C programmers who are anal rententive (myself
included) did not answer your question about far pointers. The only
reference they gave you was that far pointers were off-topic since they
are not ANSI C. Here is the long and short of it.

In the bad old MS-DOS days the X86 processors used 16 bit registers and
pointers which only allowed addressing 64K of memory. Intel included a
cludge where two registers (pointers) could be used to address 1 meg of
memory. The two pointers were called segment and offset respectively.
To address 1 meg the segment address was internally expanded to 20 bits
and shifed left four bits. The offset address was then added to render
the final 20 bit address. Ultimately it was a real mess and fostered a
lot of unportable code. I was so much happier when I started
programming in UNIX and did not have to mess with memory models and far
pointers.

--
Regards,
Stan Milam.
-----------------------------------------------------------------------------
My life is a song. I live to be sung. I sing with all my heart - John
Denver.
-----------------------------------------------------------------------------
Nov 14 '05 #38

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
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...
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
8237
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4593
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
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.