What Does This Code Do?

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
X Abstract X
Chaos Rift Regular
Chaos Rift Regular
Posts: 173
Joined: Thu Feb 11, 2010 9:46 pm

What Does This Code Do?

Post by X Abstract X »

I came across this code that compiles and runs (XCode, iOS) but it definitely isn't initializing the contents of the array. I'm pretty certain it's got to be doing something nasty. Can anyone explain this?

Code: Select all

std::string* myStrings = new std::string[4] { "Apples", "Oranges", "Bananas", "Coconuts" };
User avatar
Nokurn
Chaos Rift Regular
Chaos Rift Regular
Posts: 164
Joined: Mon Jan 31, 2011 12:08 pm
Favorite Gaming Platforms: PC, SNES, Dreamcast, PS2, N64
Programming Language of Choice: Proper C++
Location: Southern California
Contact:

Re: What Does This Code Do?

Post by Nokurn »

I am not sure I see what you mean. This code produces an error in C++98/03, since it uses an initializer list, but compiles and executes correctly in C++11:

Code: Select all

Jeremiah@mercury:~/Desktop% cat myStrings.cpp 
#include <iostream>
#include <string>
int main()
{
    std::string* myStrings = new std::string[4] { "Apples", "Oranges", "Bananas", "Coconuts" };
    for (int i = 0; i < 4; ++i)
        std::cout << i << ": " << myStrings[i] << std::endl;
    return 0;
}
Jeremiah@mercury:~/Desktop% c++ myStrings.cpp -o myStrings 
myStrings.cpp:5:48: error: expected ';' at end of declaration
    std::string* myStrings = new std::string[4] { "Apples", "Oranges", "Bananas", "Coconuts" };
                                               ^
                                               ;
1 error generated.
Jeremiah@mercury:~/Desktop% c++ -std=c++11 -stdlib=libc++ myStrings.cpp -o myStrings     
Jeremiah@mercury:~/Desktop% ./myStrings 
0: Apples
1: Oranges
2: Bananas
3: Coconuts
This is with Clang 3.3 on Mac OS X, but the behavior is the same on GCC 4.8.1 on Arch Linux.

If your misunderstanding comes from the use of {} to initialize a dynamically allocated array by assignment: This is a new feature in C++11 known as initializer lists, which Wikipedia has a good overview of. They're one of my favorite parts of C++11.
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: What Does This Code Do?

Post by dandymcgee »

After being stuck in C# world for so long I honestly forgot that this ever wasn't valid code. Lol.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
X Abstract X
Chaos Rift Regular
Chaos Rift Regular
Posts: 173
Joined: Thu Feb 11, 2010 9:46 pm

Re: What Does This Code Do?

Post by X Abstract X »

I looked at it again today, same issue. I'm using XCode 4.2 which uses LLVM 3.0 btw. I realize it's a C++ 0x feature, I'm just surprised at the fact that it compiles without an issue but simply does not initialize the damn strings. I'm guessing it's just a bug in this version?
Post Reply