C vs. C++ question about initialization differences...

Go To Last Post
8 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

A long time ago here at avrfreaks I almost remember a discussion about a difference between C and C++ in how they initialize variables, but I don't recall how it went.  Was there something that C did or didn't initiailze that C++ does differently?  I know they both initialize globals to 0 it seems, and things like variables in functions are thrown on the stack and are not initialized.  Was there something else?  In general, what else is different besides name mangling when you change from compiling a piece of source code from one type of compiler or another (C vs C++)?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Are you talking about initialiser lists and their point of execution during class construction?

 

class foo {
    foo(void) : bar(37) {
        DDRB = 0;
    }
private:
    int bar;
};

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The only thing that immediately comes to mind is designated initializers. cppreference has a nice comparison if you scroll down to the end of the "Designated initializers" section. Note that while designated initializers didn't become part of the standard until C++20, gcc has supported them as a nonstandard extension for quite a while.

github.com/apcountryman/build-avr-gcc: a script for building avr-gcc

github.com/apcountryman/toolchain-avr-gcc: a CMake toolchain for cross compiling for the Atmel AVR family of microcontrollers

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

To be honest guys I'm not sure, I just remember being surprised at the time that C went one way and C++ went the other.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

alank2 wrote:

A long time ago here at avrfreaks I almost remember a discussion about a difference between C and C++ in how they initialize variables, but I don't recall how it went.  Was there something that C did or didn't initiailze that C++ does differently?

 

Well, quite a few things come to mind:

 

1. The obvious one: C++ has types with non-trivial initialization, i.e. class types with user-defined constructors or implicit compiler-defined constructors. There's nothing like that in C. (To be precise, C has VLAs - types with non-trivial initialization, but that's it).

2. C++ supports dynamic initialization of objects with static storage duration. E.g. global variables can be initialized with non-trivial user code at program startup or later. There's nothing like that in C. In C static objects require initialization with compile-time constants. Speaking informally, in C static objects are always initialized "at compile time"

3. C++ does not permit jumps (i.e. `goto` and `switch`) over declarations of initialized automatic objects into the scope of such objects. In C there's no such restriction (albeit there is a similar restriction about jumps over VLA)

4. In C++ aggregate initialization is sequenced. In C it is unsequenced.

5. C has designated initializers. Designated initializers are now part of C++ as well, but their functionality is (slightly?) different.

6. C lets you drop a trailing `\0` when initializing a char array with a string literal. C++ doesn't.

...

999. There's definitely more

Last Edited: Sat. Apr 25, 2020 - 08:41 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

alank2 wrote:
I just remember being surprised at the time that C went one way and C++ went the other.

 

C and C++ are very different languages from day one, even in those parts that look superficially (syntactically) similar.

Last Edited: Sat. Apr 25, 2020 - 08:38 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Simplistically the only "initialization" that C does is copy .data into place and wipe .bss so everything else in C++ (c'tors etc) is additional /different.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I appreciate the details everyone.