473,407 Members | 2,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

calloc.... Why?

Is there something I'm missing? Isn't it just:
inline void* calloc ( const size_t& num, const size_t& size )
{
return malloc(num * size);
}
-JKop
Jul 22 '05 #1
14 1435
Le dimanche 18 juillet 2004 à 15:15:58, JKop a écrit dans
comp.lang.c++*:
Is there something I'm missing? Isn't it just:

inline void* calloc ( const size_t& num, const size_t& size )
{
return malloc(num * size);
}


You missed the memset(...,0,num * size); part.

--
___________ 2004-07-18 15:32:04
_/ _ \_`_`_`_) Serge PACCALIN -- sp ad mailclub.net
\ \_L_) Il faut donc que les hommes commencent
-'(__) par n'être pas fanatiques pour mériter
_/___(_) la tolérance. -- Voltaire, 1763
Jul 22 '05 #2
sorry, only a test

--
ÐÂÎÅ×éÀïÓÐÂÒÂ룿ij¸ö³£ÓÃÃüÁîÔڲ˵¥ÖÐÒþ²ØÌ«ÉOEµÄ ¿ì½Ý¼ü²»¹»Óã¿

ÊDz»ÊǾ*³£ÔÚÐÂÎÅ×éÀï´ò¿ªÒ»¸öÌû×Ó£¬È´·¢ÏÖÒ»¶ÑÂÒÂ룿 ÓÚÊǺõ£¬Ç§ÐÁÍò¿àµÄÈ¥µã»÷²Ë
µ¥,ÔÚÄÇô¶à±àÂëÖÐÑ°ÕÒGB2312£¿ ÏÖÔÚºÃÁË¡£ºÙºÙ¡£ ÓÃÕâ¸ö OEPartner À´¶¨ÖÆÒ»¸ö¿ì
½Ý¼ü¡£ÇáËÉÒ»µã£¬È«²¿¸ã¶¨£¡
Çëµ½ http://rokia.vicp.net ÏÂÔØʹÓá£:)

"JKop" <NU**@NULL.NULL> дÈëÏûÏ¢ÐÂÎÅ:iK*****************@news.indigo.ie...
Is there something I'm missing? Isn't it just:
inline void* calloc ( const size_t& num, const size_t& size )
{
return malloc(num * size);
}
-JKop

Jul 22 '05 #3
JKop <NU**@NULL.NULL> wrote:
Is there something I'm missing?
As always, yes. Please read the FAQ before posting. (section
7.31 of the comp.lang.c FAQ). Even a glance at the Standard
or your compiler documentation would have explained what
'calloc' does.
Isn't it just:

inline void* calloc ( const size_t& num, const size_t& size )
No, it is:
void *calloc(size_t nmemb, size_t size)
{
return malloc(num * 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.

[#3] The calloc function returns either a null pointer or a
pointer to the allocated space.
Jul 22 '05 #4
inline void* calloc ( const size_t& num, const size_t& )


I use const references because it's inline.
-JKop
Jul 22 '05 #5
JKop wrote:
inline void* calloc ( const size_t& num, const size_t& )

I use const references because it's inline.

Well calloc() is a standard library function with a specific function
signature, so your above is not a calloc() implementation (and calloc()
also zeros all bytes).


Regards,

Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #6

DUH? what has const references got to do with
being inlined ?
-JKop "JKop" <NU**@NULL.NULL> wrote in message
news:Te*****************@news.indigo.ie...
inline void* calloc ( const size_t& num, const size_t& )


I use const references because it's inline.
-JKop

Jul 22 '05 #7
Dave Townsend posted:

DUH? what has const references got to do with
being inlined ?
-JKop

"JKop" <NU**@NULL.NULL> wrote in message
news:Te*****************@news.indigo.ie...
> inline void* calloc ( const size_t& num, const size_t& )


I use const references because it's inline.
-JKop



Consider this: if you have a function that takes in two vectors, you're
going to use const references instead of by-value, because then everything
doesn't have to get copied. But... if you're taking in two ints, then it's
more efficent to pass them by-value. But... when a function is inlined,
there's no need for hidden pointers, so the const references are once again
more efficent.

This is all ofcourse assuming a stack and registers system.

-JKop
Jul 22 '05 #8
In message <jz*****************@news.indigo.ie>, JKop <NU**@NULL.NULL>
writes
Dave Townsend posted:

DUH? what has const references got to do with
being inlined ?

"JKop" <NU**@NULL.NULL> wrote in message
news:Te*****************@news.indigo.ie...

> inline void* calloc ( const size_t& num, const size_t& )

I use const references because it's inline.


Consider this: if you have a function that takes in two vectors, you're
going to use const references instead of by-value, because then everything
doesn't have to get copied. But... if you're taking in two ints, then it's
more efficent to pass them by-value. But... when a function is inlined,
there's no need for hidden pointers, so the const references are once again
more efficent.

This is all ofcourse assuming a stack and registers system.


And ignoring the "as-if" rule. Nothing _has_ to be copied.

--
Richard Herring
Jul 22 '05 #9
On Tue, 20 Jul 2004 12:14:36 +0100, Richard Herring <ju**@[127.0.0.1]>
wrote:
In message <jz*****************@news.indigo.ie>, JKop <NU**@NULL.NULL>
writes
Dave Townsend posted:

DUH? what has const references got to do with
being inlined ?

"JKop" <NU**@NULL.NULL> wrote in message
news:Te*****************@news.indigo.ie...

> inline void* calloc ( const size_t& num, const size_t& )

I use const references because it's inline.


Consider this: if you have a function that takes in two vectors, you're
going to use const references instead of by-value, because then everything
doesn't have to get copied. But... if you're taking in two ints, then it's
more efficent to pass them by-value. But... when a function is inlined,
there's no need for hidden pointers, so the const references are once again
more efficent.

This is all ofcourse assuming a stack and registers system.


And ignoring the "as-if" rule. Nothing _has_ to be copied.


And using references doesn't make the optimizer's job any easier IMHO.
But it's likely that both versions will generate identical code when
inlined in the same context.

Tom
Jul 22 '05 #10
Richard Herring posted:
And ignoring the "as-if" rule. Nothing _has_ to be

copied.
void Blah(std::vector a, std::vector b)
{
//Change a and b
}
Still sure nothing has to be copied?
-JKop

Jul 22 '05 #11
tom_usenet posted:
And using references doesn't make the optimizer's job any easier IMHO. But it's likely that both versions will generate identical code when inlined in the same context.

Tom


It's not optimization at all, it's simply what inline
functions are all about. When a function is "outline",
arguments have to be passed to it in a certain way (stack
and registers), but when the function's inline, the stack
and registers aren't bothered with. So... with an inline
function; if you use const references, the compiler has no
optimation to do, it simply uses the objects in the calling
function. But if the arguments are by-value, then the
compiler actually would have to do an optimization to
realize that it can use the objects from the calling
function. Alternatively you could pass by-value and have
the objects const in the inline function, then the compiler
would know to use the objects from the calling function. I
prefer the const references, as it's very clear what's
going on.
-JKop
Jul 22 '05 #12
JKop wrote:

Richard Herring posted:
And ignoring the "as-if" rule. Nothing _has_ to be

copied.

void Blah(std::vector a, std::vector b)
{
//Change a and b
}

Still sure nothing has to be copied?


Ok. Now do the same with references (const or not const).
That is: call that function, let the function alter the
passed parameters, but the caller should not be aware
of that change. You have to copy the passed arguments.

Back to your assumption, which was
You use a const reference because the function may inline
that function.

Well.
If you have

int foo( int a, int b)
{
return 2 * a + b;
}

int main()
{
int c = 3, d = 4;

cout << foo( c, d );
}

AND the compiler inlines the function, then it most
likely will *not* create copies for c and d. The data
flow analysis of the compiler will show, that a and b
never get altered inside the functions body and thus
the compiler can use c and d directly.

It effectively boils down to

int main()
{
int i = 5;
int j = i;

cout << j;
}

where the compiler is able to wipe out variable j
completely and use i instead.

Of course there is no requirement for the compiler to make
a data flow analysis, but almost all compilers do it. It
would be impossible to sell such a masterpiece on the market
without such an analysis.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #13
In message <hb*****************@news.indigo.ie>, JKop <NU**@NULL.NULL>
writes
Richard Herring posted:
And ignoring the "as-if" rule. Nothing _has_ to becopied.
void Blah(std::vector a, std::vector b)
{
//Change a and b
}


Not inline, according to a comment modifies its parameters (for no
apparent reason, since they're passed by value) but no code to
illustrate the fact.

Compare with
inline void* JKopsNotQuiteCalloc( const size_t& num, const size_t& size)
{ return malloc(num * size); }

or
inline void* JKopsNotQuiteCalloc(size_t num, size_t size)
{ return malloc(num * size); }

Inline, don't modify their parameters.
Still sure nothing has to be copied?


Quite sure, thanks.

What was your function Blah supposed to be illustrating?

Does "//Change a and b" have any effect on the observable behaviour of
the program?

--
Richard Herring
Jul 22 '05 #14
On Tue, 20 Jul 2004 12:30:03 GMT, JKop <NU**@NULL.NULL> wrote:
tom_usenet posted:
And using references doesn't make the optimizer's job anyeasier IMHO.
But it's likely that both versions will generate

identical code when
inlined in the same context.

Tom


It's not optimization at all, it's simply what inline
functions are all about. When a function is "outline",
arguments have to be passed to it in a certain way (stack
and registers), but when the function's inline, the stack
and registers aren't bothered with.


inline is only a hint to the compiler, and a way to avoid violating
the one definition rule. The compiler can (and does) choose to inline
apparently non-inline functions, and to put inline functions out of
line.

So... with an inlinefunction; if you use const references, the compiler has no
optimation to do, it simply uses the objects in the calling
function.
But it introduces aliasing problems, making optimization harder, and
can even change semantics. I'll give you an example:

int foo = 10;

inline int f(int const& ref)
{
foo = ref;
++foo;
return foo + ref;
}

int main(int argc, char** argv)
{
int const& i = argc > 1? foo: 10;
f(foo); //hmmm.
}

Because a reference is used, the compiler doesn't know whether that
reference is an alias for "foo", so it has to assume it is, and
therefore produce suboptimal code. If you change the semantics to:

inline int f(int ref)
{
foo = ref;
++foo;
return foo + ref;
}

then the compiler can optimize it much better, since it knows "ref" is
free from aliases (since it is a local variable).

But my basic point is that references can be bad since they are
aliases, and aliases hurt optimization, since you never know what they
are aliasing.

On another note, your concept of "use the objects in the calling
function" doesn't really make sense. Remember that CPUs have
registers, and often variables have their values both in "main" memory
and in a register. When inlining a function, whether using by-ref or
by-val parameter passing, the compiler optimizes things to work out
whether it already has the relevent value in registers, or whether
they have to be reloaded, or whatever. Using references vs values may
actually harm this process, since the optimizer is generally happier
dealing with local, independant variables.

Still, this is a complex issue, and examining output assemblies is
probably the best way to experiment.

But if the arguments are by-value, then thecompiler actually would have to do an optimization to
realize that it can use the objects from the calling
function. Alternatively you could pass by-value and have
the objects const in the inline function, then the compiler
would know to use the objects from the calling function. I
prefer the const references, as it's very clear what's
going on.


However, it goes against all style guidelines I've read, and will
therefore confuse anyone reading your code (as you have already seen
with this thread).

Tom
Jul 22 '05 #15

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

Similar topics

5
by: Koster | last post by:
Sorry for the re-post, but my previous question was left unanswered. I have a question about the appropriateness of calloc. Consider an array of pointers to structs which need to be allocated...
29
by: David Hill | last post by:
Is there a difference between: /* code 1 */ struct sample test; test = malloc(sizeof(struct sample)); memset(&test, 0, sizeof(test)); /* code 2 */ struct sample test; test = calloc(1,...
26
by: dagger | last post by:
Hi there. I'm using C under FreeBSD with the gcc compiler and am having a bit of trouble using the calloc and realloc calls. As an example the code snippet: #include <stdio.h> int main() {...
16
by: laberth | last post by:
I've got a segmentation fault on a calloc and I don'tunderstand why? Here is what I use : typedef struct noeud { int val; struct noeud *fgauche; struct noeud *fdroit; } *arbre; //for those...
37
by: Harsimran | last post by:
Can any one explain what are far pointers and what is the difference between malloc and calloc .Which is better ?
40
by: boris | last post by:
Hi! I'm seeking some answers about what seems to be a memory leak. I have a loop that looks much like this: double *largeArray = (double*) calloc(); for (...) { printf("iteration #...\n");...
11
by: lohith.matad | last post by:
Hi all, Though the purpose of both malloc() and calloc() is the same, and as we also know that calloc() initializes the alloacted locations to 'zero', and also that malloc() is used for bytes...
2
by: chingfulan | last post by:
I have the following code and I can not figure out why Stg2In returns a null pointer? "Stg2In = (float *)calloc(9*DataLen, sizeof(float)); " while "Stg2Out = (float *)calloc(9*DataLen,...
8
by: venkatesh | last post by:
hai to everybody, i am having doubt in difference between the malloc and calloc function. please tell where to use and when to use those functions?
14
by: Roka100 | last post by:
Hi all, I tried 2 programs : #include <stdio.h> #include <string.h> 1, int main(void){ char *str = NULL;
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...
0
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,...

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.