malloc vs calloc

R

Raman

Hi All,

As per my understanding, malloc and calloc differs in the way they
allocate memory( malloc give a contigous block, But calloc may give
Non-contigous block as well).
Now consider this program.

int main()
{
char *p;
p=(char *) calloc(10,sizeof(char));

*(p+2)='O';
*(p+8)='S';



return (0);
}
suppose calloc returns space in two blocks( 5 byte at address
starting at1000 and rest 5 bytes at 2000).
Now (p+2) is 1002.
(p+8) is 1008

But 1008 is not the part of space allocated to p.
I dont know my interpertation of non-contigous blocks is right or not.
Please help.



Thanks
Raman Chalotra
 
I

Ian Collins

Raman said:
Hi All,

As per my understanding, malloc and calloc differs in the way they
allocate memory( malloc give a contigous block, But calloc may give
Non-contigous block as well).

Says who? An array in C is is contiguous.
Now consider this program.

int main()
{
char *p;
p=(char *) calloc(10,sizeof(char));
How many times must people here have to say "do not cast the return
value of calloc"? Does anyone read the archive before they post?
 
R

Richard

Raman said:
Hi All,

As per my understanding, malloc and calloc differs in the way they
allocate memory( malloc give a contigous block, But calloc may give
Non-contigous block as well).

Where did you hear this? I wonder if it is just English language issues
confusing the point in hand.
Now consider this program.

int main()
{
char *p;
p=(char *) calloc(10,sizeof(char));

*(p+2)='O';
*(p+8)='S';



return (0);
}
suppose calloc returns space in two blocks( 5 byte at address
starting at1000 and rest 5 bytes at 2000).
Now (p+2) is 1002.
(p+8) is 1008

But 1008 is not the part of space allocated to p.
I dont know my interpertation of non-contigous blocks is right or not.
Please help.



Thanks
Raman Chalotra

--
 
R

Richard

Ian Collins said:
Says who? An array in C is is contiguous.

How many times must people here have to say "do not cast the return
value of calloc"? Does anyone read the archive before they post?

No they don't. Often people do not read an entire archive for a problem
they did not even realise existed.

I would suggest that if you are tired of pointing out such corrections
then you need to take a break from offering advice. Which would be a
shame as you are normally polite and constructive. There are plenty of
others more than willing to correct the famous malloc cast issue - God
knows they do it enough.
 
C

Christopher Layne

Ian said:
How many times must people here have to say "do not cast the return
value of calloc"? Does anyone read the archive before they post?

I suspect the book Raman is reading is either sadistic and/or he is
masochistic. Either way he's coming up with some pretty twisted stuff.
 
L

Lane Straatman

Richard said:
[casting calloc]
How many times must people here have to say "do not cast the return
value of calloc"? Does anyone read the archive before they post?

No they don't. Often people do not read an entire archive for a problem
they did not even realise existed.

I would suggest that if you are tired of pointing out such corrections
then you need to take a break from offering advice. Which would be a
shame as you are normally polite and constructive. There are plenty of
others more than willing to correct the famous malloc cast issue - God
knows they do it enough.
Maybe Ian's question was rhetorical. He's given me pointers many times. I
wish I knew how to read the archives of a ng, but my google savvy is
lacking. LS
 
R

Richard

Lane Straatman said:
Richard said:
[casting calloc]
How many times must people here have to say "do not cast the return
value of calloc"? Does anyone read the archive before they post?

No they don't. Often people do not read an entire archive for a problem
they did not even realise existed.

I would suggest that if you are tired of pointing out such corrections
then you need to take a break from offering advice. Which would be a
shame as you are normally polite and constructive. There are plenty of
others more than willing to correct the famous malloc cast issue - God
knows they do it enough.
Maybe Ian's question was rhetorical. He's given me pointers many times. I
wish I knew how to read the archives of a ng, but my google savvy is
lacking. LS

Yes. It was rhetorical. Hence my reply.
 
R

Raman

Hi All,
Where did you hear this? I wonder if it is just English language issues
confusing the point in hand.

I find it at some of the goole group.
Thanks
Raman Chalotra
 
S

Samuel Stearley

Raman said:
As per my understanding, malloc and calloc differs in the way they
allocate memory( malloc give a contigous block, But calloc may give
Non-contigous block as well).

I'll explain what contiguous means. On any modern OS your application
is going to be accessing the returned memory with virtual addressing.
So it's perspective is that the memory is continuous even if the
'continuous' virtual memory is made up of multiple pages whose real
addresses are not continuous. If you are writing a device driver and
need memory that is continuous for the purpose of DMA by chips that
apply addresses directly to the BUS (PCI, 60x, etc) then you need an
allocation that guarantees contiguousness (these addresses are applied
to the bus directly so they don't go through virtual=>physical
translation).
 
J

jaysome

I find it at some of the goole group.

I suspect you found it at some of "the Google group"? Note that
"goole" is not a word, but "Google" is.

Regardless of where you found it, whoever said that (a successful
result from) calloc() may give a non-contiguous block of memory is
dead wrong--the C Standards (both of them) guarantee that dynamic
memory allocations give you contiguous blocks of memory (providing of
course that such calls to these functions are successful).

Checking for a successful return from malloc() or calloc() is
simple--just check for a NULL pointer, e.g.:

x = malloc(sizeof *x);
if ( x )
{
/* success */
}
else
{
/* fail */
}
 
R

Richard Heathfield

jaysome said:
I suspect you found it at some of "the Google group"? Note that
"goole" is not a word, but "Google" is.

Goole is a town in the UK, not far from Scunthorpe and Kingston-upon-Hull.
 
J

Joachim Schmitz

Richard Heathfield said:
jaysome said:


Goole is a town in the UK, not far from Scunthorpe and Kingston-upon-Hull.
So goole still is not a word, Google and Goole are... ;-)


Bye, Jojo
 
L

Laurent Deniau

jaysome said:
I suspect you found it at some of "the Google group"? Note that
"goole" is not a word, but "Google" is.

Regardless of where you found it, whoever said that (a successful
result from) calloc() may give a non-contiguous block of memory is
dead wrong--the C Standards (both of them) guarantee that dynamic
memory allocations give you contiguous blocks of memory (providing of
course that such calls to these functions are successful).

Checking for a successful return from malloc() or calloc() is
simple--just check for a NULL pointer, e.g.:

This is not enough...

x = malloc(n * sizeof *x);

if ( n && !x ) {
// failure
}

is more portable.

a+, ld.
 
C

CBFalconer

Richard said:
.... snip ...

No they don't. Often people do not read an entire archive for a
problem they did not even realise existed.

I would suggest that if you are tired of pointing out such corrections
then you need to take a break from offering advice. Which would be a
shame as you are normally polite and constructive. There are plenty of
others more than willing to correct the famous malloc cast issue - God
knows they do it enough.

Another valueless foolish post. Into the sin bin you go.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
 
C

CBFalconer

Richard said:
jaysome said:
.... snip ...

Goole is a town in the UK, not far from Scunthorpe and
Kingston-upon-Hull.

Sounds like it should be a sister to Moose Jaw, which is far from
anything. Does it have hooligoolians?

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
 
C

CBFalconer

Samuel said:
I'll explain what contiguous means. On any modern OS your application
is going to be accessing the returned memory with virtual addressing.
So it's perspective is that the memory is continuous even if the
'continuous' virtual memory is made up of multiple pages whose real
addresses are not continuous. If you are writing a device driver and
need memory that is continuous for the purpose of DMA by chips that
apply addresses directly to the BUS (PCI, 60x, etc) then you need an
allocation that guarantees contiguousness (these addresses are applied
to the bus directly so they don't go through virtual=>physical
translation).

Which has nothing to do with the C language, the topic of this
newsgroup. The language knows nothing about DMA, pages, device
drivers, PCI, etc. You want a newsgroup that deals with your
peculiar system, whatever that may be. It may well have callable
functions to perform that function. Note also [OT] that many
systems (especially x86 based) have strict limitations on where DMA
addressable memory can be located. This is due to the DMA
controller. [/OT]

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top