473,442 Members | 1,985 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

C needs a BOOST

It would be really nice if C could adopt a really nice algorithms
library like C++'s STL + BOOST.

The recent "reverse the words in this sentence" problem posted made me
think about it.
It's like 5 lines to do it in C++ because of all the nifty algorithms
that come with the language (I think BOOST is going to get bolted on
to the C++ language like STL did).

It's a lot more work in C than C++. Why doesn't C have stacks,
dequeues, and other common, simple tool sets already in its standard
library?

Opinions? Is keeping the language tiny worth the cost of C
programmers having to constantly reinvent the wheel?

Oct 3 '07
259 6850
In data Sat, 06 Oct 2007 13:37:28 +0200, André Gillibert scrisse:
>Many languages follow the three steps:
Design by one creator (it may "look" good, but be either unusable or too
platform dependent... Sometimes, it's right from the start), cahotic
evolution by implementors, and final standardization.
yes, it is not something that goes from hight
Oct 7 '07 #201
In data Sat, 06 Oct 2007 13:10:44 -0700, Keith Thompson scrisse:
>"Malcolm McLean" <re*******@btinternet.comwrites:
>"Keith Thompson" <ks***@mib.orgwrote in message
>>So you provide a function that converts an integer to a string:

"hello " + audience + "time left " + image(hours) + image(minutes) + "\n";
Now you've introduced a requirement for automatic garbage collection.
It's the approach I used in MiniBasic, but then it was never sold as a
fast, low-level language.

If you're going to introduce an operator that does string
concatenation, you already have a requirement for some kind of
automatic storage management (either automatic garbage collection or
automatic destructors for the temporary string objects). And you
probably need an actual string type, something that C doesn't have.
sstring globalvalue;

so where is the problem if a+b return the partial (and at end the
total) result in "globalvalue"?

is the problem if "globalvalue" no resized to 0 memory very ofthen?

than there is the problem ("123"+"457") + ("123"+"456")
if the 2 () have the same precedence
Oct 7 '07 #202

"¬a\/b" <al@f.gwrote in message
news:6l********************************@4ax.com...
>
than there is the problem ("123"+"457") + ("123"+"456")
if the 2 () have the same precedence
Can't see anything difficult about that. Parentheses however are superfluous
beacuse concatenation is inherently associative.

("Fred" + "is") + "dead" = "Fred" + ("is" + "dead")

for all values of Fred.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Oct 7 '07 #203
Hello,

Stan Milam wrote:
user923005 wrote:
[...]
>Opinions? Is keeping the language tiny worth the cost of C
programmers having to constantly reinvent the wheel?

This is what libraries are for. If you are like me you have built up a
"toolbox" of useful routines over the years.
But this might open a legal problem: If you have written some part of
the toolbox while working for one company, are you allowed to reuse this
for the next one? In many cases, this is not the case, thus, you have to
rewrite your toolbox once you change the company.

Not to take into account the problems that arise if you are working on
open-source and non-open-source projects...

Just my 2 cents worth.

Regards,
Spiro.

--
Spiro R. Trikaliotis http://opencbm.sf.net/
http://www.trikaliotis.net/ http://www.viceteam.org/
Oct 7 '07 #204
In data Sun, 7 Oct 2007 09:15:04 +0100, Malcolm McLean scrisse:
>"¬a\/b" <al@f.gwrote in message
news:6l********************************@4ax.com.. .
>>
than there is the problem ("123"+"457") + ("123"+"456")
if the 2 () have the same precedence
Can't see anything difficult about that. Parentheses however are superfluous
beacuse concatenation is inherently associative.

("Fred" + "is") + "dead" = "Fred" + ("is" + "dead")

for all values of Fred.
in how i see the thing (don't know if compiler follow me)
in ("123"+"457") + ("123"+"456")

first there is
tm1=("123"+"457") , tm1=("123"+"456")
than
return tm1+tm1

so if there is only one golbal variable "tm1" for doing the sum
at the end there will be a wrong result

but with one only variable tm1 all is ok with
"123"+"457" + "123" + "456";
because it is ((("123"+"457") + "123") + "456");
tm1=("123"+"457");
tm1+="123";
tm1+="456";
return tm1;
Oct 7 '07 #205
"Malcolm McLean" <re*******@btinternet.comwrote in message
news:Wq******************************@bt.com...
>
"Chris Thomasson" <cr*****@comcast.netwrote in message
news:qf******************************@comcast.com. ..
>"Chris Thomasson" <cr*****@comcast.netwrote in message
news:1p******************************@comcast.com ...
[...]>Well, how crappy is it?
>>

static clc_stack the_stack = CLC_STACK_STATIC_INIT();

read my 10 Rules of Programming before going any further.
I need to use CLC as a macro namespace... I need to use STACK as a micro
namespace... Anything else is a function of the micro... Therefore:
>
This one breaks the rule of two. For something as fundamental as a stack,
the rule of two is very important.
[...]

CLC_STACK_STATICINIT();

should satisfy you? BTW, thanks for taking a look!

Oct 7 '07 #206
In article <13*************@corp.supernews.com>, Ed Jensen
<ej*****@visi.comwrites
>Ian Collins <ia******@hotmail.comwrote:
>>
Ed Jensen wrote:
>>In comp.lang.c Ian Collins <ia******@hotmail.comwrote:
Which raises the question why hasn't anyone written such a popular
library for C? C has been around a decade or more longer than C++,
plenty of time for a "killer library" to appear, but it hasn't.

My conclusion has to be that the demand isn't there.

I think your conclusion is wrong.

My conclusion is that many developers chose to move to less primitive
languages with more comprehensive standard libraries instead.

It what way does that contradict my conclusion?

The demand was, and still is, there; however, since the powers-that-be
aren't evolving C to meet those demands, developers simply end up
leaving the language.
Developers are using C95
--
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\ Chris Hills Staffs England /\/\/\/\/
/\/\/ ch***@phaedsys.org www.phaedsys.org \/\/\
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/

Oct 7 '07 #207

"Chris Thomasson" <cr*****@comcast.netwrote
>
BTW, do middle names confuse some people?

Chris Thomasson is my name:

Christopher Michael Thomasson is also my name...
So be it.
They are not used. Your name is Christopher Michael Thomasson but you post
as Chris Thomasson. Instinctively people use a binomial system. That's the
rule of two.
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Oct 7 '07 #208
Malcolm McLean wrote, On 07/10/07 15:49:
>
"Chris Thomasson" <cr*****@comcast.netwrote
>>
BTW, do middle names confuse some people?

Chris Thomasson is my name:

Christopher Michael Thomasson is also my name...
So be it.
They are not used. Your name is Christopher Michael Thomasson but you
post as Chris Thomasson. Instinctively people use a binomial system.
That's the rule of two.
Some people *do* use there middle name. Also, as you should be well
aware, a number of people have double barrelled surnames, so even if
they do not use there middle name they are still using three names.

Personally, I have always used two names and my middle initial on
written stuff everywhere except on Usenet, and it has never confused people.

I think you vastly underestimate people with your "rule of two" and that
the name Christopher used was fine.
--
Flash (M.J.) Gordon
Oct 7 '07 #209
Flash Gordon said:

<snip>
I think you vastly underestimate people with your "rule of two" and that
the name Christopher used was fine.
Have you read the "rule of four" and the "rule of seven"?

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Oct 7 '07 #210
"Richard Heathfield" <rj*@see.sig.invalidwrote in message
news:Fa******************************@bt.com...
Flash Gordon said:

<snip>
>I think you vastly underestimate people with your "rule of two" and that
the name Christopher used was fine.

Have you read the "rule of four" and the "rule of seven"?
:^0

Oct 7 '07 #211
Richard Heathfield wrote, On 07/10/07 19:39:
Flash Gordon said:

<snip>
>I think you vastly underestimate people with your "rule of two" and that
the name Christopher used was fine.

Have you read the "rule of four" and the "rule of seven"?
Well, I know a "rule of seven" and I suppose inflation could have hit
the "rule of three" that I know...
--
Flash Gordon
Oct 7 '07 #212
Chris Thomasson said:
"Malcolm McLean" <re*******@btinternet.comwrote in message
news:Wp*********************@bt.com...
<snip>
>That's the rule of two.

I find that I need to break the rule of two from time to time, for
practical reasons...
Practically, there is no "rule of two" anyway. It's just something that
Malcolm has made up. A "rule of two" assumes that the number two not only
exists but is a significant and therefore reasonable number. But in fact
there are only four reasonable numbers. They are 0, 1, 4, and infinity (if
we can temporarily consider infinity to be a number).

0 is reasonable because it makes sense that you can't (or mustn't) do
something. (For example: how many leading underscores may you use in
external identifiers? Answer: None.)
1 is reasonable because it makes sense that you can do something only once.
(For example: how many definitions of main can your program have? Answer:
One.)
Infinity is reasonable because it makes sense that you can do something
lots of times. (For example: how many different unique external identifier
names are legal in C? Answer:
49684374567448041538025252381239447556479204485973 162548 - which is
*effectively* infinite!)

And of course 4 is reasonable because it makes sense that the number of
reasonable numbers in existence should itself be reasonable. :-)
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Oct 7 '07 #213
"Richard Heathfield" <rj*@see.sig.invalidwrote in message
news:EL*********************@bt.com...
Chris Thomasson said:
>"Malcolm McLean" <re*******@btinternet.comwrote in message
news:Wp*********************@bt.com...

<snip>
>>That's the rule of two.

I find that I need to break the rule of two from time to time, for
practical reasons...

Practically, there is no "rule of two" anyway. It's just something that
Malcolm has made up. A "rule of two" assumes that the number two not only
exists but is a significant and therefore reasonable number. But in fact
there are only four reasonable numbers. They are 0, 1, 4, and infinity (if
we can temporarily consider infinity to be a number).
I like to think of an infinite number as a simple monotonic counter, that
never overflows...
0 is reasonable because it makes sense that you can't (or mustn't) do
something. (For example: how many leading underscores may you use in
external identifiers? Answer: None.)

1 is reasonable because it makes sense that you can do something only
once.
(For example: how many definitions of main can your program have? Answer:
One.)
Infinity is reasonable because it makes sense that you can do something
lots of times. (For example: how many different unique external identifier
names are legal in C? Answer:
49684374567448041538025252381239447556479204485973 162548 - which is
*effectively* infinite!)
effectively infinite until one infinite counter syncs with another...
And of course 4 is reasonable because it makes sense that the number of
reasonable numbers in existence should itself be reasonable. :-)
;)

Oct 8 '07 #214
"André Gillibert" <ta*****************@yahodeletethato.fra écrit dans le
message de news: op.tzrw8qv87pu1mk@andre...
Malcolm McLean wrote:
>Some things can't be done by committee. For instance I don't think there
is a single example of a famous poem written by a committee of people.

Yes, you're most probably right for poems.
Actually, the French Surréalists invented a game of producing phrases by a
group of people: http://fr.wikipedia.org/wiki/Cadavre_exquis
http://en.wikipedia.org/wiki/Exquisite_corpse
The very name of the game is the first phrase they produced:
"Le cadavre - exquis - boira - le vin - nouveau"

It does not strictly qualify as a poem, but definety as poetry.

La révolution surréaliste - Cadavres exquis (1927) Extraits :
L'amour ornera le peuple.
Les femmes blessées faussent la guillotine aux cheveux blonds.
La colombe des branches contamine la pierre lamartinienne.
L'hippogriffe frisé poursuit la biche noire.
La grève des étoiles corrige la maison sans sucre.
Le mille-pattes amoureux et frêle rivalise de méchanceté avec le cortège
languissant.
La vapeur ailée séduit l'oiseau fermé à clé.

--
Chqrlie.

PS: pardon my French ;-)
Oct 8 '07 #215
"Chris Thomasson" <cr*****@comcast.neta écrit dans le message de news:
Ur******************************@comcast.com...
OOPS! wrong link:
>http://groups.google.com/group/comp....45c17cdd3c04a3

Refer to the last paragraph (e.g. at bottom of post):

http://groups.google.com/group/comp....44cdcc315a5f07

Sorry for any confusions!
Thank you for such a great illustration of what we do not want C to become.

--
Chqrlie.
Oct 8 '07 #216
On Sat, 6 Oct 2007, jacob navia wrote:

jn>Ian Collins wrote:
jn>jacob navia wrote:
jnConstructors and destructors look simple but are HELL to implement:
jn>
jnint foo(int n)
jn{
jn int arg;
jn // ...
jn if (arg 0)) {
jn struct foo;
jn // ... some code
jn goto exit;
jn // ... some code
jn }
jnexit:
jn return 56;
jn}
jn>
jnThe destructor of foo needs to be called before the goto gets
jnexecuted... This was a bug still in MSVC some years ago.
jn>
jn>The simple solution to that is to get rid of that decrepit relic from a
jn>past age, goto!
jn>>
jn>The only possible justification for using goto is cleanup code, which
jn>can be cleanly handled with destructors.
jn>>
jn>
jn>How do you get rid of the goto?
jn>
jn>int Search_byName(char *name,int HowtoSearch)
jn>{
jn> switch(HowtoSearch) {
jn> // ...
jn> case 42:
jn> for (int i=0; i<Len; i++) {
jn> if (!strcmp(name,nameTab[i]))
jn> goto found; // <<<<--
jn> }
jn> fprintf(stderr,
jn> "Not found\,%s is an illegal name);
jn> return 0;
jn> case 65:
jn> // ...
jn> }
jn> return -1;
jn>found:
jn> // Some code
jn> return 1;
jn>}
jn>
jn>The break statement has no arguments, and for breaking out
jn>of several nested scopes you *have* to use gotos.
jn>
jn>We have discussed this in comp.std.c several times. There were proposals
jn>to give an argument to break, like in this case
jn> break found;
jn>
jn>but it was argued that this would be identical to goto...

break, continue and return are all forms of hidden gotos (and very useful
as is goto). Go and write the above in Pascal. I mean the original Pascal,
not Turbo Pascal.

I think bashing goto nowadays is just silly...

harti
Oct 8 '07 #217
Hartmut Brandt wrote:
break, continue and return are all forms of hidden gotos (and very
useful as is goto). Go and write the above in Pascal. I mean the
original Pascal, not Turbo Pascal.

I think bashing goto nowadays is just silly...
goto isn't inherently bad, but it is very easy for someone to abuse it
when better control flow methods are available.

Bashing goto is silly, as is singing it's praises.

Oct 8 '07 #218
jxh
On Oct 8, 3:14 am, "Charlie Gordon" <n...@chqrlie.orgwrote:
"jxh" <j...@despammed.coma écrit dans le message de news:
1191631033.397358.38...@o3g2000hsb.googlegroups.co m...
template <typename T>
struct T_listnode {
T value;
struct T_listnode<T*next;
};
would work both in C++ and in the hypothetical C template
implementation.

Why use this horrible syntax ?
As stated in the quoted material, it is for compatibility with
C++, but also to lessen the burden of introducing the feature
into C compilers.

-- James

Oct 8 '07 #219
jxh wrote:
On Oct 8, 3:14 am, "Charlie Gordon" <n...@chqrlie.orgwrote:
>Why use this horrible syntax ?

As stated in the quoted material, it is for compatibility with
C++, but also to lessen the burden of introducing the feature
into C compilers.
I guess you are refering to environments providing tightly cooperating
C++ and C compilers.
That's what some people call C/C++ compilers, but I don't like the latter
term as it gives the wrong feeling that C/C++ is a language.

--
If you've a question that doesn't belong to Usenet, contact me at
<ta*****************@yahoDELETETHATo.fr>
Oct 8 '07 #220
jxh
On Oct 8, 2:09 pm, "André Gillibert"
<tabkanDELETETHIS...@yahodeletethato.frwrote:
I guess you are refering to environments providing tightly cooperating
C++ and C compilers.
That's what some people call C/C++ compilers, but I don't like the latter
term as it gives the wrong feeling that C/C++ is a language.
I guess we are in complete agreement.

-- James

Oct 9 '07 #221
In data Sun, 07 Oct 2007 12:23:46 +0200, ¬a\/b scrisse:
>In data Sun, 7 Oct 2007 09:15:04 +0100, Malcolm McLean scrisse:
>>"¬a\/b" <al@f.gwrote in message
news:6l********************************@4ax.com. ..
>>>
than there is the problem ("123"+"457") + ("123"+"456")
if the 2 () have the same precedence
Can't see anything difficult about that. Parentheses however are superfluous
beacuse concatenation is inherently associative.

("Fred" + "is") + "dead" = "Fred" + ("is" + "dead")

for all values of Fred.

in how i see the thing (don't know if compiler follow me)
in ("123"+"457") + ("123"+"456")

first there is
tm1=("123"+"457") , tm1=("123"+"456")
than
return tm1+tm1

so if there is only one golbal variable "tm1" for doing the sum
at the end there will be a wrong result

but with one only variable tm1 all is ok with
"123"+"457" + "123" + "456";
because it is ((("123"+"457") + "123") + "456");
tm1=("123"+"457");
tm1+="123";
tm1+="456";
return tm1;
for handle all this i use a queue of static objets with a global index
someting like

obj * function(char *a)
{obj* tmp= & (obj[ index++ % 16 ]);
*tmp+=a;
return tmp;
}

and limit the level of parentesis (or number of returned function) to
15 level max
don't remember if i add code for resize the 16 global array objects
in somewhere
Oct 9 '07 #222
user923005 <dc*****@connx.comwrote:
It would be really nice if C could adopt a really nice algorithms
library like C++'s STL + BOOST.
One of the most important differences between C and many other
languages, including C+++Boost, is that C does not believe the marketing
line that "One size fits all". This is a Good Thing. It would be nicest
if it stayed that way.

Richard
Oct 9 '07 #223
Keith Thompson wrote:
... jacob has argued against C++-style destructors
because they're difficult to implement; the point is to make the
compiler, rather than the programmer, do that hard work.
Note also that this was raised in connection with GC, which is
quite analogous: make the compiler keep track, rather than the
programmer.
Oct 9 '07 #224
"Richard Heathfield" <rj*@see.sig.invalidwrote in message
Chris Thomasson said:
>"Malcolm McLean" <re*******@btinternet.comwrote in message
news:Wp*********************@bt.com...

<snip>
>>That's the rule of two.

I find that I need to break the rule of two from time to time, for
practical reasons...

Practically, there is no "rule of two" anyway. It's just something that
Malcolm has made up. A "rule of two" assumes that the number two not only
exists but is a significant and therefore reasonable number.
It's a psychological rule. As are all the ten rules of programming.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm
Oct 9 '07 #225
"Malcolm McLean" <re*******@btinternet.comwrites:
"Richard Heathfield" <rj*@see.sig.invalidwrote in message
>Practically, there is no "rule of two" anyway. It's just something that
Malcolm has made up. A "rule of two" assumes that the number two not only
exists but is a significant and therefore reasonable number.
It's a psychological rule. As are all the ten rules of programming.
Shouldn't there only be two rules of programming?
--
"All code should be deliberately written for the purposes of instruction.
If your code isn't readable, it isn't finished yet."
--Richard Heathfield
Oct 9 '07 #226
Ben Pfaff said:
"Malcolm McLean" <re*******@btinternet.comwrites:
>"Richard Heathfield" <rj*@see.sig.invalidwrote in message
>>Practically, there is no "rule of two" anyway. It's just something that
Malcolm has made up. A "rule of two" assumes that the number two not
only exists but is a significant and therefore reasonable number.
It's a psychological rule. As are all the ten rules of programming.

Shouldn't there only be two rules of programming?
The two rules of programming are:

1 : Don't do it;

2 (for experts only) : Don't do it yet.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Oct 9 '07 #227
jxh
On Oct 5, 3:41 pm, Ian Collins <ian-n...@hotmail.comwrote:
Note I'm not proposing the addition, it's just that RAII is
the one thing I miss most when swapping from C++ to C. Just
about everything else can be worked around in standard C. The
closest equivalent I know of is pthreads cleanup handlers.
Maybe something along those lines might be a more acceptable
extension to C?
Not exactly RAII, but it gives the feel of an "atreturn"
mechanism. First a sample program that uses it:

#include <stdio.h>
#include <stdlib.h>
#include "atreturn.h"

int main (void) {
void *p = malloc(10);
if (p == 0) {
fprintf(stderr, "malloc failed\n");
atreturn EXIT_FAILURE;
}
atreturn_begin {
puts("in atreturn");
free(p);
} atreturn_end;
printf("p is %p\n", p);
atreturn 0;
}
I define the atreturn word to signal the intention of using
an atreturn block, because I couldn't figure how to write a
macro that defined return itself that wouldn't break if there
was no atreturn block defined.

The implementation uses setjmp/longjmp, which is kind of
heavy duty for a "within function" application, but it seemed
the only reasonable way to deal with multiple atreturn
statements from the same function. It also made writing an
atreturn macro that emulated a single statement easier.

The "atreturn.h" header file follows:

#ifndef X_ATRETURN_H_X
#define X_ATRETURN_H_X

#include <setjmp.h>

static jmp_buf x_atreturn_jmp_buf_x;

#define atreturn \
if (setjmp(x_atreturn_jmp_buf_x) == 0) \
goto x_atreturn_begin_x; \
else return

#define atreturn_begin \
do { \
goto x_atreturn_end_x; \
x_atreturn_begin_x: \
do

#define atreturn_end \
while (0); \
longjmp(x_atreturn_jmp_buf_x, 1); \
x_atreturn_end_x: (void)0; \
} while (0)

#endif /* X_ATRETURN_H_X */
-- James

Oct 10 '07 #228
user923005 <dc*****@connx.comwrote:
On Oct 9, 6:42 am, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
user923005 <dcor...@connx.comwrote:
It would be really nice if C could adopt a really nice algorithms
library like C++'s STL + BOOST.
One of the most important differences between C and many other
languages, including C+++Boost, is that C does not believe the marketing
line that "One size fits all". This is a Good Thing. It would be nicest
if it stayed that way.

Do you honestly believe that having a huge, debugged, efficient
toolkit at your disposal is a *detraction*?
I never used the word detraction, and I don't see what it would detract
from. I _do_ see that C is mostly a systems language, not a bells-and-
whistles already-slow application language. Because of that, many C
programmers will want to use ADTs which have been tuned to their
application, and not a one-size-fits-all solution which does in fact
_not_ fit 90% of all cases very well.
IOW: many, perhaps most, C programmers are going to either ignore or
hack around in any semi-standard ADT libraries anyway. This is in fact
what many programmers now do with non-standard ADT libraries already.
Therefore, the advantage of a semi-standard ADT library over, say, the
old Snippets libraries (remember those?) would be near nil.

Richard
Oct 10 '07 #229
jxh
On Oct 10, 4:55 am, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
>
I never used the word detraction, and I don't see what it
would detract from. I _do_ see that C is mostly a systems
language, not a bells-and- whistles already-slow application
language. Because of that, many C programmers will want to use
ADTs which have been tuned to their application, and not a
one-size-fits-all solution which does in fact _not_ fit 90% of
all cases very well.
This is where templates could have a positive impact. Most of the
limitations of generic containers and algorithms in C now are the
void * basis, which requires an extra allocation to use, or a mess
of macros, which are difficult to maintain. The availability of
templates would provide a mechanism to write near optimal general
containers, and used in more optimal ways (for example, ADTs that
can be allocated from the stack instead of dynamically).

For a different example, a template function based quick sort
implementation could allow the compiler to inline the call to the
comparison function.

-- James

Oct 10 '07 #230

jxh wrote:
For a different example, a template function based quick sort
implementation could allow the compiler to inline the call to the
comparison function.
/* generic_qsort.c */
PREFIX(qsort)(TYPE *arr, size_t len) {
...
if (LESS(*right, pivot)) {...}
...
}
/* int_qsort.c */
#define PREFIX(s) int_##s
typedef int TYPE;
static inline int LESS(int a, int b) {return a < b;}
#include "generic_qsort.c"
#undef PREFIX

not much longer than c++ templates imho
macros are ugly though

Oct 10 '07 #231
On Oct 10, 12:26 pm, Szabolcs Nagy <nszabo...@gmail.comwrote:
jxh wrote:
For a different example, a template function based quick sort
implementation could allow the compiler to inline the call to the
comparison function.

/* generic_qsort.c */
PREFIX(qsort)(TYPE *arr, size_t len) {
...
if (LESS(*right, pivot)) {...}
...

}

/* int_qsort.c */
#define PREFIX(s) int_##s
typedef int TYPE;
static inline int LESS(int a, int b) {return a < b;}
#include "generic_qsort.c"
#undef PREFIX

not much longer than c++ templates imho
macros are ugly though
I used this very technique in the source code for chapter 13 of "C
Unleashed".
I called it PRELUDE instead of PREFIX.

Oct 10 '07 #232
jxh
On Oct 10, 12:26 pm, Szabolcs Nagy <nszabo...@gmail.comwrote:
/* generic_qsort.c */
PREFIX(qsort)(TYPE *arr, size_t len) {
...
if (LESS(*right, pivot)) {...}
...

}

/* int_qsort.c */
#define PREFIX(s) int_##s
typedef int TYPE;
static inline int LESS(int a, int b) {return a < b;}
#include "generic_qsort.c"
#undef PREFIX

not much longer than c++ templates imho
macros are ugly though
This is nice general way for explicit instantiation of a
routine.

However, for each new array of things to be sorted, a new
explicit instantiation block will need to be added, and the
caller will need to remember what prefix he used to properly
call the routine. A different developer may not know that
an instantiation already exists, and may create another
instantiation. For compound types, different developers may
create duplicate instantiations that use different prefix
names, although the actual underlying type is the same.

I am not sufficiently clever enough to devise on the spot a
useful example that templates could solve that cannot be
mimic'd in some way with C macros. But, I am certain that
*someone* would be able to, if not on the spot, then in the
near future. And, the availability of templates would increase
the chances of efficient generic data structures and algorithms
being available in a future C standard (which is how it
happened for C++).

-- James

Oct 10 '07 #233
user923005 wrote:
On Oct 10, 12:26 pm, Szabolcs Nagy <nszabo...@gmail.comwrote:
>jxh wrote:
>>For a different example, a template function based quick sort
implementation could allow the compiler to inline the call to the
comparison function.
/* generic_qsort.c */
PREFIX(qsort)(TYPE *arr, size_t len) {
...
if (LESS(*right, pivot)) {...}
...

}

/* int_qsort.c */
#define PREFIX(s) int_##s
typedef int TYPE;
static inline int LESS(int a, int b) {return a < b;}
#include "generic_qsort.c"
#undef PREFIX

not much longer than c++ templates imho
macros are ugly though

I used this very technique in the source code for chapter 13 of "C
Unleashed".
I called it PRELUDE instead of PREFIX.
Interesting , Mr "user923005". So you are Heathfield. It seemed to me
that both "you" and "he" are the same. I wonder how many of the
c.l.c "regulars" are just the same guy!

--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
Oct 10 '07 #234
jacob navia said:
user923005 wrote:
<snip>
>I used this very technique in the source code for chapter 13 of "C
Unleashed".
I called it PRELUDE instead of PREFIX.
Interesting , Mr "user923005". So you are Heathfield.
Uninterestingly, you are wrong - as usual.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Oct 10 '07 #235
jacob navia <ja***@nospam.orgwrites:
user923005 wrote:
[...]
>I used this very technique in the source code for chapter 13 of "C
Unleashed".
I called it PRELUDE instead of PREFIX.
Interesting , Mr "user923005". So you are Heathfield. It seemed to me
that both "you" and "he" are the same. I wonder how many of the
c.l.c "regulars" are just the same guy!
You might take note of the fact that amazon.com lists 16 authors for
"C Unleashed":

Richard Heathfield
Lawrence Kirby
Mike Lee
Mathew Watson
Ben Pfaff
>>Dann Corbit
Peter Seebach
Brett Fishburne
Scott Fluhrer
Ian Woods
Sam Hobbs
Ian Kelly
Mike Wright
Chad Dixon
Stephan Wilms
Jack Klein

and that user923005's e-mail address is <dc*****@connx.com>.

Ironically, all the regulars *except* user923005 are really Richard
Heathfield. Richard Heathfield himself is of course Spartacus.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"I'm Richard Heathfield and so's my wife!"
-- Keith "Richard Heathfield" Thompson
Oct 10 '07 #236
Paul Hsieh wrote:
....
You know there are some people in this world who think C++ is a bit
too heavy, onerous, and just not quite right (did you know that STL's
vectors require that your class have an empty constructor and that
this empty constructor will be called on free spaces in the vector
By "empty constructor" do you mean a "default constructor"? That's only
required if you use one of the vector constructors that uses the default
constructor. If you create a vector that is empty, or initialized with N
copies of a specified object, or is initialized from an iterator range,
the default constructor is not required.
Oct 11 '07 #237
On Oct 10, 6:36 pm, "James Kuyper Jr." <jameskuy...@verizon.net>
wrote:
Paul Hsieh wrote:
...
You know there are some people in this world who think C++ is a bit
too heavy, onerous, and just not quite right (did you know that STL's
vectors require that your class have an empty constructor and that
this empty constructor will be called on free spaces in the vector

By "empty constructor" do you mean a "default constructor"? That's only
required if you use one of the vector constructors that uses the default
constructor. If you create a vector that is empty, or initialized with N
copies of a specified object, or is initialized from an iterator range,
the default constructor is not required.
And what if you need to add entries to this vector over time? Looking
at the *implementations* of a few STLs, it seems to me that for
efficiency reasons, they pretty much need to default constructor to be
there. This is one of the problems with RAII -- you cannot prepare
space for your objects asynchronously from their initialization, so
you are forced to initialize them twice; the first time being
knowingly irrelevant.

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

Oct 11 '07 #238
Paul Hsieh wrote:
On Oct 10, 6:36 pm, "James Kuyper Jr." <jameskuy...@verizon.net>
wrote:
>Paul Hsieh wrote:
...
>>You know there are some people in this world who think C++ is a bit
too heavy, onerous, and just not quite right (did you know that STL's
vectors require that your class have an empty constructor and that
this empty constructor will be called on free spaces in the vector
By "empty constructor" do you mean a "default constructor"? That's only
required if you use one of the vector constructors that uses the default
constructor. If you create a vector that is empty, or initialized with N
copies of a specified object, or is initialized from an iterator range,
the default constructor is not required.

And what if you need to add entries to this vector over time? Looking
at the *implementations* of a few STLs, it seems to me that for
efficiency reasons, they pretty much need to default constructor to be
there. This is one of the problems with RAII -- you cannot prepare
space for your objects asynchronously from their initialization, so
you are forced to initialize them twice; the first time being
knowingly irrelevant.
What? You most certainly do not have to initialise objects twice.

What do vectors have to do with RAII anyway?

--
Ian Collins.
Oct 11 '07 #239
On Oct 10, 10:51 pm, jacob navia <ja...@nospam.orgwrote:
user923005 wrote:
On Oct 10, 12:26 pm, Szabolcs Nagy <nszabo...@gmail.comwrote:
jxh wrote:
For a different example, a template function based quick sort
implementation could allow the compiler to inline the call to the
comparison function.
/* generic_qsort.c */
PREFIX(qsort)(TYPE *arr, size_t len) {
...
if (LESS(*right, pivot)) {...}
...
}
/* int_qsort.c */
#define PREFIX(s) int_##s
typedef int TYPE;
static inline int LESS(int a, int b) {return a < b;}
#include "generic_qsort.c"
#undef PREFIX
not much longer than c++ templates imho
macros are ugly though
I used this very technique in the source code for chapter 13 of "C
Unleashed".
I called it PRELUDE instead of PREFIX.

Interesting , Mr "user923005". So you are Heathfield. It seemed to me
that both "you" and "he" are the same. I wonder how many of the
c.l.c "regulars" are just the same guy!
How on earth did you make that remarkable leap of logic?

Oct 11 '07 #240
"J. J. Farrell" <jj*@bcs.org.ukwrites:
On Oct 10, 10:51 pm, jacob navia <ja...@nospam.orgwrote:
>user923005 wrote:
[...]
I used this very technique in the source code for chapter 13 of "C
Unleashed".
I called it PRELUDE instead of PREFIX.

Interesting , Mr "user923005". So you are Heathfield. It seemed to me
that both "you" and "he" are the same. I wonder how many of the
c.l.c "regulars" are just the same guy!

How on earth did you make that remarkable leap of logic?
Presumably he knows that Richard Heathfield is an author of "C
Unleashed", but didn't realize that there are several other authors as
well (and didn't bother to check before making the accusation).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 11 '07 #241
"Ian Collins" <ia******@hotmail.comwrote in message
>
What? You most certainly do not have to initialise objects twice.

What do vectors have to do with RAII anyway?
If using STL you need to provide a default / empty constructor and a copy
constructor for every class. Otherwise the system will rapidly gum up.

It can be a problem because the whole point of the constructor / destructor
/ method paradigm is to make use that every class is valid. However it only
sometimes makes sense to have default objects. A default image, for
instance, might be 0 by 0 dimensions, but if you are not careful then you'll
introduce errors by having a null for the raster data, when in the context
of the program it doesn't make any sense to have zero-dimensioned images
anyway.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Oct 11 '07 #242
Malcolm McLean wrote:
"Ian Collins" <ia******@hotmail.comwrote in message
>>
What? You most certainly do not have to initialise objects twice.

What do vectors have to do with RAII anyway?
If using STL you need to provide a default / empty constructor and a
copy constructor for every class. Otherwise the system will rapidly gum up.
Or not compile. What do you expect from a container? The semantics of
a vector are pretty similar to an array, where objects also have to have
a public copy constructor. In practice, C++ standard containers tend to
be used to store PODs, small objects or pointers.
It can be a problem because the whole point of the constructor /
destructor / method paradigm is to make use that every class is valid.
So if you declare a vector of 10 objects, what would you rather have, 10
default initialised objects, or garbage? If an object can't be default
initialised, it probably has no place being in a container.
However it only sometimes makes sense to have default objects. A default
image, for instance, might be 0 by 0 dimensions, but if you are not
careful then you'll introduce errors by having a null for the raster
data, when in the context of the program it doesn't make any sense to
have zero-dimensioned images anyway.
No one would store images in a container you would store pointers, or
back on the sub-thread subject of RAII, a smart pointer to one.

--
Ian Collins.
Oct 11 '07 #243
Malcolm McLean wrote:
"Ian Collins" <ia******@hotmail.comwrote in message
>>
What? You most certainly do not have to initialise objects twice.

What do vectors have to do with RAII anyway?
If using STL you need to provide a default / empty constructor and a
copy constructor for every class.
This is emphatically not the case, and deliberately so. Many
people use the STL extensively with many classes which do not
permit default construction. It works where appropriate.
Otherwise the system will rapidly gum up.
Not really.
It can be a problem because the whole point of the constructor /
destructor / method paradigm is to make use that every class is valid.
However it only sometimes makes sense to have default objects. A default
image, for instance, might be 0 by 0 dimensions, but if you are not
careful then you'll introduce errors by having a null for the raster
data, when in the context of the program it doesn't make any sense to
have zero-dimensioned images anyway.
Which is why the STL works hard *not* to require default
constructors when it can be avoided.

-- James
Oct 11 '07 #244
Keith Thompson said:
"J. J. Farrell" <jj*@bcs.org.ukwrites:
>On Oct 10, 10:51 pm, jacob navia <ja...@nospam.orgwrote:
>>user923005 wrote:
[...]
>I used this very technique in the source code for chapter 13 of "C
Unleashed".
I called it PRELUDE instead of PREFIX.

Interesting , Mr "user923005". So you are Heathfield. It seemed to me
that both "you" and "he" are the same. I wonder how many of the
c.l.c "regulars" are just the same guy!

How on earth did you make that remarkable leap of logic?

Presumably he knows that Richard Heathfield is an author of "C
Unleashed", but didn't realize that there are several other authors as
well (and didn't bother to check before making the accusation).
This is a recurring problem with Jacob Navia's articles. He asserts
something that he (presumably) believes to be true, but without checking
his facts. Because he believes it to be true, he cannot understand why
people disagree with him, and assumes that it must surely be because they
are either ignorant or hostile, so he reacts with hostility. At least,
that is my best guess. I can think of no other rational explanation
(although perhaps others here /can/ do so).

His advertising of his compiler within this non-commercial newsgroup seems
to have diminished to the point where it is no longer a significant issue.
If we can educate him to do that, perhaps we can also educate him to check
his facts before making claims, at which point he might yet become a
valuable member of the group.

Well, you never know your luck, do you?

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Oct 11 '07 #245
J. J. Farrell said:
On Oct 10, 10:51 pm, jacob navia <ja...@nospam.orgwrote:
<snip>
>Interesting , Mr "user923005". So you are Heathfield. It seemed to me
that both "you" and "he" are the same. I wonder how many of the
c.l.c "regulars" are just the same guy!

How on earth did you make that remarkable leap of logic?
Rover is a dog. Fido is a dog. Therefore, Rover is Fido.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Oct 11 '07 #246
"Keith Thompson" <ks***@mib.orga écrit dans le message de news:
ln************@nuthaus.mib.org...
"Malcolm McLean" <re*******@btinternet.comwrites:
>"Keith Thompson" <ks***@mib.orgwrote in message
>>That's a matter of taste. Personally, given a language that supports
operator overloading, I have no problem with using "+" for string
concatenation. The fact that it's not commutative just doesn't bother
me. The intended meaning of
"hello" + " " + "world"
seems perfectly obvious. It's a convenient notation; mathematical
purity isn't required.
The problem is that this doesn't arise too often. The actual example
you posed would always be rendered "hello world", for example, so
really it is

char *audience;

"hello " + audience;

Most often you want to embed numbers in strings you create on the fly,
because usually the output will be numerical.

"hello " + audience ", you have " + ticks + "seconds on your account".

That "," should be "+", yes?
>Unfortuately then

"hello " + audience + "time left " + hours + minutes + "\n";

isn't clear.

So you provide a function that converts an integer to a string:

"hello " + audience + "time left " + image(hours) + image(minutes) + "\n";
If we want to travel this road, why not go the whole nine yards and support
a more readable syntax:

"hello $audience, time left $hours:$minutes\n";

--
Chqrlie.
Oct 11 '07 #247
"Malcolm McLean" <re*******@btinternet.coma écrit dans le message de news:
4t******************************@bt.com...
>
"Keith Thompson" <ks***@mib.orgwrote in message
>So you provide a function that converts an integer to a string:

"hello " + audience + "time left " + image(hours) + image(minutes) +
"\n";
Now you've introduced a requirement for automatic garbage collection.
It's the approach I used in MiniBasic, but then it was never sold as a
fast, low-level language.
A temporary string object is indeed constructed above, and could be disposed
of without full blown garbage collection. string management would be
needed, maybe with reference counting.

--
Chqrlie.
Oct 11 '07 #248
"jxh" <jx*@despammed.coma écrit dans le message de news:
11**********************@22g2000hsm.googlegroups.c om...
On Oct 8, 3:14 am, "Charlie Gordon" <n...@chqrlie.orgwrote:
>"jxh" <j...@despammed.coma écrit dans le message de news:
1191631033.397358.38...@o3g2000hsb.googlegroups.c om...
>template <typename T>
struct T_listnode {
T value;
struct T_listnode<T*next;
};
would work both in C++ and in the hypothetical C template
implementation.

Why use this horrible syntax ?

As stated in the quoted material, it is for compatibility with
C++, but also to lessen the burden of introducing the feature
into C compilers.
Compatibility with C++ may not be desirable at the syntax level, if the
semantics differ.
Syntactic choices have little impact on compiler complexity. Introducing
templates in C by transplanting a C++ implementation is hopeless.

--
Chqrlie.
Oct 11 '07 #249
Paul Hsieh wrote:
On Oct 10, 6:36 pm, "James Kuyper Jr." <jameskuy...@verizon.net>
wrote:
>Paul Hsieh wrote:
...
>>You know there are some people in this world who think C++ is a bit
too heavy, onerous, and just not quite right (did you know that STL's
vectors require that your class have an empty constructor and that
this empty constructor will be called on free spaces in the vector
By "empty constructor" do you mean a "default constructor"? That's only
required if you use one of the vector constructors that uses the default
constructor. If you create a vector that is empty, or initialized with N
copies of a specified object, or is initialized from an iterator range,
the default constructor is not required.

And what if you need to add entries to this vector over time? Looking
at the *implementations* of a few STLs, it seems to me that for
efficiency reasons, they pretty much need to default constructor to be
there.
To add entries to your vector:

1. If doing so would require more space than is currently allocated,
allocate more space.

2. If the extra entries are being added anywhere but at the end of the
vector, you need to copy existing entries to new locations to make room
for the new entries, and to destroy existing entries to make room for
the objects that will be copied. I'm not familiar with the new "move
semantics" that are being considered for the next version of the C++
standard, but I presume that the existing entries might be moved rather
than copied and destroyed, where appropriate.

3. The new entries are then copied into the space reserved for them.

I don't see any need for the default constructor here. Certainly not for
efficiency's sake: adding calls to a default constructor at any place in
the above sequence would merely require corresponding calls to the
destructor, wasting time both ways.

The exception, of course, is if you want to insert new
default-constructed entries. That's precisely the point. None of the
standard containers requires that the contained type be default
constructible, unless you use those members which construct/insert
default-constructed elements into the container. That seems a very
reasonable way of handling it, and I don't see how mentioning that fact
qualifies as a criticism of the C++ standard containers.
This is one of the problems with RAII -- you cannot prepare
space for your objects asynchronously from their initialization, so
you are forced to initialize them twice; the first time being
knowingly irrelevant.
I don't what you're talking about. Every standard C++ container class
has an allocator class associated with it, either by explicitly using
std::vector<T,Allocator>, or by letting Allocator default to
std::allocator<T>.

Creating space for your objects asynchronously from their initialization
is precisely what the Allocator class is for. The Allocator::allocate()
function creates the space, without calling the constructor. The
Allocator::construct() function calls the appropriate constructor,
without having to allocate the space. The Allocator::destroy() function
calls the appropriate destructor, without deallocating the space. The
Allocator::deallocate() function releases the space, without calling the
destructor.

Calling std::vector::reserve() is the way for the end user to trigger a
call to Allocator::allocate(), if needed to reserve space for the
requested number of elements. One of the few criticisms I have of
std::vector is that calling reserve() with a value smaller than the
current value of capacity() is not guaranteed to make the appropriate
call to Allocator::deallocate().
Oct 11 '07 #250

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

Similar topics

7
by: sbobrows | last post by:
{Whilst I think much of this is OT for this newsgroup, I think the issue of understanding diagnostics just about gets under the door. -mod} Hi, I'm a C++ newbie trying to use the Boost regex...
1
by: Hardy | last post by:
Hi, just come into the boost world. just the first.cpp in the program_options examples, with many link error... devc++4.9.9.2, gcc 3.4.2, can I get your opinions on this problem? thank you~ ...
1
by: å¼ æ²ˆé¹ | last post by:
How to compile the HelloWorld of boost.asio? Maybe this is a stupid problem , but I really don't konw how to find the right way. My compile environment is WinXP, Msys , MinGw , G++ 3.4.2,...
1
by: Max Wilson | last post by:
Hi, Has anyone here built Boost.Python modules under MinGW? I'm trying to build the Boost.Python tutorial under MinGW and getting an error that says it depends on MSVC, which puzzles me because...
11
by: Osiris | last post by:
I have these pieces of C-code (NOT C++ !!) I want to call from Python. I found Boost. I have MS Visual Studio 2005 with C++. is this the idea: I write the following C source file:...
1
by: =?UTF-8?B?SmVucyBNw7xsbGVy?= | last post by:
(I also posted this to boost-user) The BGL implementation of breadth-first search uses a dedicated color map. I had the following idea: Some algorithms don't need to distinguish black/gray,...
1
by: Noah Roberts | last post by:
Trying to use boost::function in a C++/CLI program. Here is code: pragma once #include <boost/function.hpp> #include <boost/shared_ptr.hpp> #include <vector> using namespace System;
4
by: Man4ish | last post by:
namespace ve/////////////////ve.h { struct VertexProperties { std::size_t index; boost::default_color_type color; }; }...
2
by: Man4ish | last post by:
I have created Graph object without vertex and edge property.It is working fine. #include <boost/config.hpp> #include <iostream> #include <vector> #include <string> #include...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
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...
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,...
0
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...
0
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...

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.