Visual Studio 2015 CE vs. XCode 6.4 Debug

Hi everyone! I usually use both above mentioned compilers but I have noticed that this little code (below) in VS 2015 CE compiles and works, while in XCode it returns me an error:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include "std_lib_facilities.h"

char* findx(const char* s, const char* x)
{
    int count_s = 0;
    
    while (s[count_s] != 0)
    {
        int count_x = 0;
        while (x[count_x] != 0)
        {
            if (s[count_s] == x[count_x])
            {
                cout << "Found match " << x[count_x] << " at X[" << count_x
                << "] with " << s[count_s] << " at S[" << count_s
                << "]" << endl;
                return 0;
                break;
            }
            ++count_x;
        }
        ++count_s;
    }
    
    cout << "Match " << *x << " not found" << endl;
    return 0;
}

int main()
{
    char* p = new char[6]{ "Zorro" };
    char* p1 = new char[4]{ "Zio" };
    findx(p, p1);
    delete[] p;
    delete[] p1;
    keep_window_open();
    return 0;
}


The error I got in XCode are:
1
2
    char* p = new char[6]{ "Zorro" }; // Initializer-string for char array is too long
    char* p1 = new char[4]{ "Zio" }; // Initializer-string for char array is too long 


Thank you!

P.S. What do you feel to advice? I prefer XCode but I am not an experienced programmer so I trust you!
Compile your programs with both compilers with warnings set to high levels and and strict conformance with the standard demanded. This way, you have a much better chance of writing correct (conforming) C++ code.

Mostly, both will behave alike. But sometimes one compiler would be right (as the Microsoft compiler is in this case) and the other wrong; there would be other situations where the LLVM compiler is right and the Microsoft compiler wrong.

Interesting (no error on line 3, but line 5 is a different story altogether):
1
2
3
4
5
6
7
8
int main()
{
    char c[6]{ "Zorro" };
    
    char* p = new char[6]{ "Zorro" };
    
    return p[0] - c[0] ; 
}

clang++ 3.6: error: initializer-string for char array is too long
g++ 5.2: error: invalid conversion from 'const char*' to 'char' [-fpermissive]
http://coliru.stacked-crooked.com/a/b69ad2beed204148

msc++ 1800 (VS 2013): 'initializing' : cannot convert from 'initializer-list' to 'char [6]'
Element '1': no conversion from 'const char [6]' to 'char'

http://rextester.com/NEEQ54566

AFAIK, msc++ 1900 (VS 2015) which compiles this cleanly is the conforming C++ implementation wrt this.
Last edited on
That new char[6]{"Zorro"} was the subject of http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1490, fixed last november (along with a bunch of other issues) by the resolution of http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1467
As a core defect report, compilers are free to implement the fix even though it won't appear in a published standard until C++17. I see intel 15 accepts that program as intended.
Last edited on
Ok thank you very much Cubbi! You were very exhaustive!

Another question.. I know that is personal and depends on many things and preferences but which IDE do you advice me? I am a newbie and I am using VS or XCode among the dozen there are out there! :)
My advice would be, while a beginner, use as many as you can get your hands on, so that you won't be surprised when life puts you in front of one you didn't expect. Personally, I use the text editor vi most of the time.
I will then! Thank you very much for your advices! :)
Thank you JLBorges! :)
Topic archived. No new replies allowed.