Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > CCL vs STL: a comparison

Reply
Thread Tools

CCL vs STL: a comparison

 
 
jacob navia
Guest
Posts: n/a
 
      05-11-2012
Here is a comparison of two source codes that use the C++ STL and the
C containers library (ccl).

First, the C++ example:

// Standard Template Library example

#include <iostream>
#include <list>
using namespace std;

// Simple example uses type int

int main(void)
{
list<int> L;
L.push_back(0); // Insert a new element at the end
L.push_front(0); // Insert a new element at the beginning
L.insert(++L.begin(),2);// Insert "2" before position of first argument
// (Place before second argument)
L.push_back(5);
L.push_back(6);

list<int>::iterator i;

for(i=L.begin(); i != L.end(); ++i) cout << *i << " ";
cout << endl;
return 0;
}

/* OUTPUT: 0 2 0 5 6 */
---------------------------------------------------------------------------
Here is the C example
#include "containers.h"
static int PrintInt(void *pdata,void *extraArgs)
{
int i = *(int *)pdata;
fprintf(extraArgs,"%d ",i);
}
int main(void)
{
List *L;
int data;

L = iList.Create(sizeof(int));

data = 0;
iList.Add(L,&data);
iList.PushFront(L,&data);
data = 2;
iList.InsertAt(L,1,&data);
data = 5;
iList.Add(L,&data);
data = 6;
iList.Add(L,&data);
iList.Apply(L,PrintInt,stdout);
iList.Finalize(L);
}
----------------------------------------------------------------------------

I could have used iterators to print the list but I think using "Apply"
looks better ...

What do you think?
 
Reply With Quote
 
 
 
 
Ike Naar
Guest
Posts: n/a
 
      05-11-2012
On 2012-05-11, jacob navia <(E-Mail Removed)> wrote:
> #include "containers.h"
> static int PrintInt(void *pdata,void *extraArgs)
> {
> int i = *(int *)pdata;
> fprintf(extraArgs,"%d ",i);
> }


PrintInt promises to return an int, but it doesn't.

> int main(void)
> {
> List *L;
> int data;
>
> L = iList.Create(sizeof(int));
>
> data = 0;
> iList.Add(L,&data);
> iList.PushFront(L,&data);
> data = 2;
> iList.InsertAt(L,1,&data);
> data = 5;
> iList.Add(L,&data);
> data = 6;
> iList.Add(L,&data);
> iList.Apply(L,PrintInt,stdout);


No terminating newline is printed?

> iList.Finalize(L);
> }


This

int data = 5;
iList.Add(L, &data);

looks clumsier than

L.push_back(5);

and it's more prone to errors,
for instance, the compiler will accept

long data = 5;
iList.Add(L, &data);

but this may lead to unexpected run-time behaviour on
a machine on which sizeof(int) != sizeof(long)..
 
Reply With Quote
 
 
 
 
jacob navia
Guest
Posts: n/a
 
      05-11-2012
Le 11/05/12 19:54, Ike Naar a écrit :
> On 2012-05-11, jacob navia<(E-Mail Removed)> wrote:
>> #include "containers.h"
>> static int PrintInt(void *pdata,void *extraArgs)
>> {
>> int i = *(int *)pdata;
>> fprintf(extraArgs,"%d ",i);
>> }

>
> PrintInt promises to return an int, but it doesn't.
>



Yes, this is an error. Should return one or other value.

>> int main(void)
>> {
>> List *L;
>> int data;
>>
>> L = iList.Create(sizeof(int));
>>
>> data = 0;
>> iList.Add(L,&data);
>> iList.PushFront(L,&data);
>> data = 2;
>> iList.InsertAt(L,1,&data);
>> data = 5;
>> iList.Add(L,&data);
>> data = 6;
>> iList.Add(L,&data);
>> iList.Apply(L,PrintInt,stdout);

>
> No terminating newline is printed?
>


No. But it is easy to add one.

>> iList.Finalize(L);
>> }

>
> This
>
> int data = 5;
> iList.Add(L,&data);
>
> looks clumsier than
>
> L.push_back(5);
>
> and it's more prone to errors,
> for instance, the compiler will accept
>
> long data = 5;
> iList.Add(L,&data);
>
> but this may lead to unexpected run-time behaviour on
> a machine on which sizeof(int) != sizeof(long)..


Yes, you can put any value, and you have to keep track of what you are
doing.

A specific interface for a specific data type can be obtained by using
the template file furnished with the sample implementation.

If you do that you obtain compiler checking at compile time.
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      05-11-2012
jacob navia <(E-Mail Removed)> writes:

> Here is a comparison of two source codes that use the C++ STL and the
> C containers library (ccl).


It's not clear what the point is. In terms of code clarity and
simplicity, C++ is likely to come out on top in simple comparisons like
this.

> First, the C++ example:
>
> // Standard Template Library example
>
> #include <iostream>
> #include <list>
> using namespace std;
>
> // Simple example uses type int
>
> int main(void)
> {
> list<int> L;


You can also just initialise the list:

list<int> L = {0, 2, 0, 5, 6};

C libraries will always have live withing C's core language limitations.

> L.push_back(0); // Insert a new element at the end
> L.push_front(0); // Insert a new element at the beginning
> L.insert(++L.begin(),2);// Insert "2" before position of first argument
> // (Place before second argument)
> L.push_back(5);
> L.push_back(6);
>
> list<int>::iterator i;
>
> for(i=L.begin(); i != L.end(); ++i) cout << *i << " ";


Modern C++ has this:

for (int i: L) cout << i << " ";

Nice, yes?

> cout << endl;
> return 0;
> }
>
> /* OUTPUT: 0 2 0 5 6 */
> ---------------------------------------------------------------------------
> Here is the C example
> #include "containers.h"
> static int PrintInt(void *pdata,void *extraArgs)
> {
> int i = *(int *)pdata;
> fprintf(extraArgs,"%d ",i);
> }
> int main(void)
> {
> List *L;
> int data;
>
> L = iList.Create(sizeof(int));
>
> data = 0;
> iList.Add(L,&data);
> iList.PushFront(L,&data);
> data = 2;
> iList.InsertAt(L,1,&data);
> data = 5;
> iList.Add(L,&data);
> data = 6;
> iList.Add(L,&data);
> iList.Apply(L,PrintInt,stdout);
> iList.Finalize(L);
> }
> ----------------------------------------------------------------------------
>
> I could have used iterators to print the list but I think using "Apply"
> looks better ...


C++ also has an "apply" and, what's more, it doesn't need a top-level
function definition:

for_each(L.begin(), L.end(), [](int i){ cout << i << " "; });

> What do you think?


I am not sure what you are really asking.

--
Ben.
 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      05-11-2012
Le 11/05/12 21:31, Ben Bacarisse a écrit :
>
> You can also just initialise the list:
>
> list<int> L = {0, 2, 0, 5, 6};



In the C library you can do the equivalent with


int tab[]={0, 2, 0, 5, 6};

iList.InitializeWith(sizeof(int),5,tab);
 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      05-11-2012
Le 11/05/12 21:31, Ben Bacarisse a écrit :
> for (int i: L) cout<< i<< " ";


I added this:

#if 0
for(i=L.begin(); i != L.end(); ++i) cout << *i << " ";
cout << endl;
#else
for (int i: L) cout << i << " ";
#endif

and obtained:

~ $ g++ list.cpp
list.cpp: In function ‘int main()’:
list.cpp:25: error: a function-definition is not allowed here before ‘:’
token
list.cpp:27: error: expected primary-expression before ‘return’
list.cpp:27: error: expected `;' before ‘return’
list.cpp:27: error: expected primary-expression before ‘return’
list.cpp:27: error: expected `)' before ‘return’
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      05-11-2012
jacob navia <(E-Mail Removed)> writes:

> Le 11/05/12 21:31, Ben Bacarisse a écrit :
>>
>> You can also just initialise the list:
>>
>> list<int> L = {0, 2, 0, 5, 6};

>
>
> In the C library you can do the equivalent with
>
>
> int tab[]={0, 2, 0, 5, 6};
>
> iList.InitializeWith(sizeof(int),5,tab);


The 5 is unfortunate. Did you consider

iList.InitializeWith(sizeof *tab, sizeof tab, tab);

?

--
Ben.
 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      05-11-2012
Le 11/05/12 21:31, Ben Bacarisse a écrit :
> for_each(L.begin(), L.end(), [](int i){ cout<< i<< " "; });


I added this:
for_each(L.begin(), L.end(), [](int i){ cout << i << " "; });

as you wrote and obtained:

list.cpp:26: error: expected primary-expression before ‘[’ token
list.cpp:26: error: expected primary-expression before ‘]’ token
list.cpp:26: error: expected primary-expression before ‘int’

~$ g++ -v
Using built-in specs.
Target: i686-apple-darwin11
Configured with:
/private/var/tmp/llvmgcc42/llvmgcc42-2336.9~22/src/configure
--disable-checking --enable-werror
--prefix=/Applications/Xcode.app/Contents/Developer/usr/llvm-gcc-4.2
--mandir=/share/man --enable-languages=c,objc,c++,obj-c++
--program-prefix=llvm- --program-transform-name=/^[cg][^.-]*$/s/$/-4.2/
--with-slibdir=/usr/lib --build=i686-apple-darwin11
--enable-llvm=/private/var/tmp/llvmgcc42/llvmgcc42-2336.9~22/dst-llvmCore/Developer/usr/local
--program-prefix=i686-apple-darwin11- --host=x86_64-apple-darwin11
--target=i686-apple-darwin11 --with-gxx-include-dir=/usr/include/c++/4.2.1
Thread model: posix
gcc version 4.2.1 (Based on Apple Inc. build 565 (LLVM build 2336.9.00)

 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      05-11-2012
Le 11/05/12 21:31, Ben Bacarisse a écrit :
> I am not sure what you are really asking.


Just trying to discuss the two approaches. I am sure
C++ has more "syntactic sugar" than C, but the
two approaches lead to similar code.

I was just asking from feedback about that, maybe improvements
proposals.
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      05-11-2012
jacob navia <(E-Mail Removed)> writes:

> Le 11/05/12 21:31, Ben Bacarisse a écrit :
>> for (int i: L) cout<< i<< " ";

>
> I added this:
>
> #if 0
> for(i=L.begin(); i != L.end(); ++i) cout << *i << " ";
> cout << endl;
> #else
> for (int i: L) cout << i << " ";
> #endif
>
> and obtained:
>
> ~ $ g++ list.cpp
> list.cpp: In function ‘int main()’:
> list.cpp:25: error: a function-definition is not allowed here before
> ‘:’ token
> list.cpp:27: error: expected primary-expression before ‘return’
> list.cpp:27: error: expected `;' before ‘return’
> list.cpp:27: error: expected primary-expression before ‘return’
> list.cpp:27: error: expected `)' before ‘return’


Surely that's a question (if it is a question) for comp.lang.c++? But
since I "opened the door" as they say in cheap TV legal dramas -- it's a
new feature, but recent g++ compilers have it (-std=c++0x).

--
Ben.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Updates to the ccl jacob navia C Programming 35 07-07-2012 03:53 PM
Re: ccl HENRY Eshbaugh C Programming 3 08-30-2011 04:10 AM
ccl and stl: design differences jacob navia C Programming 0 04-17-2011 06:55 PM
New release of the C Containers Library (CCL) jacob navia C Programming 117 11-15-2010 04:12 PM
Re: New release of the C Containers Library (CCL) Jon C++ 16 11-09-2010 09:38 AM



Advertisments