Re: Cracking DES with C++ is faster than Java?

From: Claudio Puviani (puviani_at_hotmail.com)
Date: 05/06/04


Date: Thu, 06 May 2004 05:28:19 GMT


<newstome@comcast.net> wrote
> In sci.crypt Douglas A. Gwyn <DAGwyn@null.net> wrote:
> > A. Lloyd Flanagan wrote:
> >> Perhaps, but the statement was that it wasn't "safe" to compile C code
> >> with a C++ compiler.
> >
> > And I gave examples of that.
>
> I just looked back through this thread, and couldn't find any posting
> from you giving such an example. I'm honestly curious: can you post
> an example of something that is valid C and C++, but works differently
> in the two languages? (I think that's what you're saying is possible.)

I know this wasn't addressed to me, but I'm sure you don't care who gives you the
examples.

First example: sizeof('x') == sizeof(int) in C but in C++ sizeof('x') ==
sizeof(char). Therefore, any code that uses the value of sizeof('x') will behave
differently under the two languages. This is because 'x' is of type int in C but
of type char in C++.

Here's another snippet, adapted from appendix C of the C++ Standard:

    char a[100];
    printf("%d\n", sizeof(0, a));

This will print 100 in C++ but it will print the value of sizeof(char *) in C.
The reason is that in C, the comma operator returns and r-value, but in C++, it
returns an l-value.

And another example adapted from that same appendix:

    int x[99];
    void f()
    {
        struct x { int a; };
        printf("%d\n", sizeof(x));
    }

This will print the size of the array in C, but in C++, it will print the size of
the struct. The reason for this difference is that C++ introduces the name of the
struct at the function scope thereby hiding the array. In C, the tag, 'x', has
no visibility past the declaration.

Granted, these are contrived examples, but the aim was to show differences, not
real-life code. I'm sure someone could find more examples.

Claudio Puviani



Relevant Pages

  • Re: Cracking DES with C++ is faster than Java?
    ... This is because 'x' is of type int in C but ... The reason is that in C, the comma operator returns and r-value, but in C++, it ... struct at the function scope thereby hiding the array. ...
    (comp.lang.java)
  • Re: Cracking DES with C++ is faster than Java?
    ... This is because 'x' is of type int in C but ... The reason is that in C, the comma operator returns and r-value, but in C++, it ... struct at the function scope thereby hiding the array. ...
    (comp.lang.cpp)
  • Re: array with a single element
    ... On Mon, 19 Jul 2004, Harti Brandt wrote: ... > l>typedef struct ... Or just because we needed an array of one element for some reason, ...
    (comp.lang.c)
  • Re: pack heterogeneous data types
    ... How do i pack different data types into a struct or an array. ... into a single array? ... The reason i need to do this is send a packet over a network. ...
    (comp.lang.python)
  • Re: Passing an array of structuresfrom a pointer?
    ... to only declare structs in headers and then define the ... the struct should be declared ... what if you have a simple array like this: ... In the header we would declare? ...
    (microsoft.public.vc.language)

Quantcast