Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: question about errors when I change my C code to C++

Author: Dann Corbit

Date: 20:09:13 02/28/03

Go up one level in this thread


On February 28, 2003 at 10:48:19, Matt Taylor wrote:

>On February 27, 2003 at 09:55:35, Tom Likens wrote:
>
>>On February 26, 2003 at 17:07:45, Bo Persson wrote:
>>
>>[--snip--]
>>
>>
>>>>I see that I did not include data.h in data.c but it caused me no problem
>>>>with c files
>>>
>>>You have found an actual difference between the languages!
>>>
>>>In C++, const variables are considered local unless declared otherwise. In C,
>>>all global (file level) variables are extern by default.
>>>
>>>Bo Persson
>>>bop2@telia.com
>>
>>
>>No, no- C++ is just a superset of C.  There is *absolutely*
>>no differences between them :-) :-) :-) :-) :-) ;-)
>>
>>regards,
>>--tom
>
>Actually there are many subtle differences. Here's an example:
>
>C:
>int *array;
>array = malloc(size);
>
>C++:
>int *array;
>array = malloc(size); // error
>array = (int *) malloc(size);
>array = new int[size]; // preferred
>
>Also, I have a fair amount of code that looks like this:
>
>char *byteptr;
>
>// some stuff
>if (blah)
>    x = *((int *) byteptr)++;
>else
>    x = (int) *byteptr++;
>
>This does not work in C++. This example actually came from an x86 disassembler I
>wrote where it was extremely convenient to break type rules again & again. The
>only solution I have found in C++ is to use a union with pointers of varying
>type.
>
>You are right that most of C and C++ is the same, but some acceptable C programs
>will not compile as C++ for various reasons.

I think the smileys are a clue that he knew.

size_t s = sizeof('a'); // different answers for C and C++

const int n = 5; int a[n]; // Fails in C, works in C++

int new = 5; // fine in C, but keyword collision in C++

int  main()
{
    int i;
    i = 5;
    int j; /* Legal in C++, Illegal in C */
    return 0;
}

An example from Daniel Hudson:
#include <stdio.h>
#include <stdlib.h>

int *array1, array2[10];
const int age = 24;   /* static in C++, but extern in C */
struct a {
           struct b { int c; }
           int d;
         }

struct f { int a1; };
typedef double f;     /* legal C, illegal C++             */
int *array1;          /* duplicate defintion error in C++ */

int main(void)
{
  static struct array2 {int x;};  /* storage class specifier      *
                                   * illegal in C++, ignored in C */

  struct b B;     /* legal C, illegal it is local to type struct a *
                   * in C++.                                       */

  char str[5] = "abcdef"; /* valid C, not valid C++, array overflow */

  array1 = malloc(10);            /* implicit cast in C, illegal in C++ */

  printf("sizeof(array2) = %s\n" sizeof(array2));

  /* in C struct array2 will NOT hide previous definition of array2,
     in C++ it will. */

  return 0;
}

etc.






This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.