The Artima Developer Community
Sponsored Link

Articles Forum
Safe Labels in C++

20 replies on 2 pages. Most recent reply: Dec 27, 2008 11:12 PM by M P

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 20 replies on 2 pages [ « | 1 2 ]
Nemanja Trifunovic

Posts: 172
Nickname: ntrif
Registered: Jun, 2004

Re: Safe Labels in C++ Posted: Oct 4, 2007 5:15 AM
Reply to this message Reply
Advertisement
> It does not say anything, really, just a vague reference
> to gcc being slower in C++ than in C. Which says nothing.
> And it does not talk about 'equivalent programs'.

"It turned out that compiling a piece of C code with g++ would give you worse code. It shouldn't have made a difference, but it did."

> All these are programs on top of the kernel, so they are
> not part of the O/S, they are part of the O/S
> distribution. There is a difference.

:)

O/S != kernel;
O/S = kernel + shell + various_other_user_space_services;
}

Roman Vorobiov

Posts: 1
Nickname: buyvicodin
Registered: Oct, 2007

Re: Safe Labels in C++ Posted: Oct 5, 2007 4:29 AM
Reply to this message Reply
0000

Achilleas Margaritis

Posts: 674
Nickname: achilleas
Registered: Feb, 2005

Re: Safe Labels in C++ Posted: Oct 5, 2007 6:13 AM
Reply to this message Reply
> > It does not say anything, really, just a vague
> reference
> > to gcc being slower in C++ than in C. Which says
> nothing.
> > And it does not talk about 'equivalent programs'.
>
> "It turned out that compiling a piece of C code with g++
> would give you worse code. It shouldn't have made a
> difference, but it did."

I never believed that. They never showed any code.

>
> > All these are programs on top of the kernel, so they
> are
> > not part of the O/S, they are part of the O/S
> > distribution. There is a difference.
>
> :)
>
> O/S != kernel;
> O/S = kernel + shell + various_other_user_space_services;
> }

In the context of programming languages, the only part of an O/S distribution that count as an O/S is the kernel.

You can't say, for example, that Internet Explorer is part of the O/S, because it is a normal application that could have been written in any language.

Max Lybbert

Posts: 314
Nickname: mlybbert
Registered: Apr, 2005

Why not C bitfields? Posted: Oct 10, 2007 2:56 PM
Reply to this message Reply
I had a hard time reading the article, because I felt that it solves an artificial problem. I can't see why a C bitfield ( http://msdn2.microsoft.com/en-us/library/yszfawxh.aspx for instance) wouldn't solve the problem:

struct Cat_state
{
unsigned int SLEEPING : 1;
unsigned int PURRING : 1;
unsigned int PLAYING : 1;
}

struct Dog_state
{
...
}

Cat_state c;
c.SLEEPING = 1;

Dog_state d;
d.CHEWING = 1;
d.BARKING = 1;

if (c.SLEEPING)
{
...
};

Miguel Tadeu

Posts: 1
Nickname: mtadeunet
Registered: Nov, 2007

Re: Why not C bitfields? Posted: Nov 12, 2007 10:36 AM
Reply to this message Reply
I read a book that noted just that(can remember which), but I picked the example described there and added a few features:
// from the book
template<unsigned long N>
struct binary
{
static const unsigned int digit = N % 10;
BOOST_STATIC_ASSERT(digit == 0 || digit == 1);

static unsigned long const value = binary<N/10>::value * 2 + digit;
};

template<>
struct binary<0>
{
static unsigned int const value = 0;
};

// from me...
template<unsigned long N, unsigned long S>
struct binary_shift_left : binary<N>
{
static unsigned long const value = (binary<N>::value << S);
};

template<unsigned long N, unsigned long S>
struct binary_shift_right : binary<N>
{
static unsigned long const value = (binary<N>::value >> S);
};

template<unsigned long N, unsigned long P = 0>
struct flag : binary_shift_left<N, P> { };

template<unsigned long P>
struct bit_flag : flag<1, P>{ };

// usage
int main(int argc, char *argv[])
{
unsigned long flags = 0x00;

flags = binary<1010101010>::value;

flags |= bit_flag< 1 >::value;
flags |= bit_flag< 2 >::value;
flags |= bit_flag< 3 >::value;
flags |= bit_flag< 4 >::value;
flags |= bit_flag< 5 >::value;
flags |= bit_flag< 6 >::value;
flags |= bit_flag< 7 >::value;
flags |= bit_flag< 8 >::value;

flags |= flag<11, 9>::value | flag<101, 5>::value;

cout << binary<1111>::value << endl;

return EXIT_SUCCESS;
}


It's to be noticed that recent compilers will resolve the templates and the code remains as optimized as in C.

M P

Posts: 1
Nickname: ma740988
Registered: Dec, 2008

Re: Safe Labels in C++ Posted: Dec 27, 2008 11:12 PM
Reply to this message Reply
Interesting to say the least. I'll need to read the article again but assume CAT_PURRING required 4 bits. To provide coverage across a range, I would end up with:

BIT_CONST( Cat_state, CAT_SLEEPING, 1 );
BIT_CONST( Cat_state, CAT_PURRING, 2 );
BIT_CONST( Cat_state, CAT_PLAYING, 6 );

i.e CAT_PLAYING will start at location 6?

Flat View: This topic has 20 replies on 2 pages [ « | 1  2 ]
Topic: Your C++ Wish List Previous Topic   Next Topic Topic: JavaScript and PHP Support in NetBeans 6.1

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use