9

Does the C++ compiler treat the arrays same way as in C?

E.g

In C,

  • An array access using subscript operator is always interpreted as a pointer.
  • In function argument, array declarations are treated as pointer to start of element.
user744621
  • 93
  • 4
  • "In function argument, array declarations are treated as pointer to start of element" - ambiguous: true that `int a[2]; f(a)` can call `void f(int*);` BUT you can also write `void f(int[]);` and in C++ even `template void f(int (&a)[N]) { ...hey, can use N in here... }`, point being `a` doesn't have to be allowed to decay to a pointer. – Tony Delroy May 11 '11 at 07:08
  • @Tony: `void f(int[]);` is equivalent to `void f(int*);` in both C and C++. You may freely pass pointer to both functions according to the standard (i.e. not as extension or something). – Serge Dundich May 11 '11 at 11:19
  • Some of the differences are mentioned [here](http://stackoverflow.com/questions/4810664/). – fredoverflow May 11 '11 at 11:35

5 Answers5

14

Yes and no. Arrays work the same in both languages for the most part (C99 supports variable-length arrays, while C++ doesn't, and there may be a few other subtle differnces as well).

However, what you're saying isn't exactly true either. The compiler doesn't treat an array access as a pointer, not even in C. An array access can be more efficient in some cases, because the compiler has better information on aliasing available in the array case. In both C and C++, a plain pointer access means that the compiler has to assume that it may alias any other compatible type. If the compiler simply treated it as a pointer dereference, then this optimization opportunity would be lost.

Edit
As pointed out in a comment, the language standard does define array subscripting in terms of pointer arithmetics/dereferencing. Of course, actual compilers make use of the additional information that a pointer is really an array, so they're not treated exactly like pointers, but that could be considered an optimization beyond what the standard mandates.

jalf
  • 243,077
  • 51
  • 345
  • 550
  • +1: The issue with VLA and why they will not be supported in C++ is an important (easy to ignore on the other hand) decision: The size of the array is part of the type of the array in C++, and must be known at compile time. The stricter type checking in C++ makes a difference here. – David Rodríguez - dribeas May 11 '11 at 07:46
  • 5
    Semantically, array access with subscripts *is* defined in terms of an expression in which the array decays to a pointer to its first element (8.3.4/6 in C++). If the compiler can track where some pointers came from, in order to deduce the absence of aliasing, grand. This is a particularly easy case of it, but I think it's still correct to say that it's "interpreted as a pointer". Certainly it is *in the language*, even if typical implementations special-case optimizing it. – Steve Jessop May 11 '11 at 08:49
  • @Steve: Hmm, true. I wasn't aware that arrays were specifically defined in this way. I stand corrected then. :) – jalf May 11 '11 at 09:09
7

Not exactly same as in C99. C99 supports Variable Length Array (VLA), but C++ doesn't.

void f(int n)
{
   int array[n]; //valid C99, but invalid C++
}

That means, C++ compilers do not treat the arrays same way as do C (i.e C99) compilers.

However, other version of C (i.e C89) doesn't support VLA. So C89 arrays would be [almost] same as C++ arrays.

Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • 1
    (Just for thought: in practice, some compilers like GCC do allow this and probably always will, so depending on your portability/Standardisation needs you might decide to do it anyway) – Tony Delroy May 11 '11 at 07:03
  • 1
    @Tony: There are a few quirks on gcc approach exactly because the treatment of arrays are different in both languages. If you use VLA in g++ it will compile, and it will create something that is very close to an array, but not the same. In particular, in C++ the type of an array is statically known at compile time and it contains the size information, but with VLA size is only known at runtime. That means that you cannot use an VLA in some contexts: `template void foo( T(&)[N] ) {}` will gladly accept C++ arrays but not VLAs. – David Rodríguez - dribeas May 11 '11 at 07:40
4

Yes, they are treated in the same way. However, in C++ you probably should not be using them - investigate the std::vector class!

3

Yes. Arrays are treated in the same way in C and C++. However, C++ now has the STL, which is a collection of data structures and operations on them, such as string, vector, deque etc.

Sriram
  • 10,298
  • 21
  • 83
  • 136
  • 1
    If by "now" you mean "for the last 13 years", you are correct :) – fredoverflow May 11 '11 at 11:36
  • @FredOverflow: I used to program C++ in high school on Borland & Turbo C++ compilers and "IDEs" till about 8 years ago. Coming from there, "now" seemed like a good choice of word. :D – Sriram May 11 '11 at 11:42
-1

Yes, C++ is an extended version of C language apart from its interesting and appealing OOP features. Strostrupp and other designed it with sole intention of creating a Object Oriented Language with C like syntax. Fundamentally both are same in most cases (excluding C++'s OOP features) and arrays are not an exception.

"An array is basically a pointer to a sequential memory block. where the name of the array represents the first location of that block." This statement is true for both C and C++.

Array Implementation is same though there are some restrictions in how C++ compilers allow you to use them.

Amit
  • 13,134
  • 17
  • 77
  • 148
  • 1
    "C++ is just an extended version of C language." No. It is not. A kind of "backward-compatibility" with C has been intentionally preserved in C++ but this is it. C and C++ are totally different languages (C++ is object-oriented as you said and C is not and never will be) with different philosophy and different evolution directions. – Serge Dundich May 11 '11 at 07:50
  • 1
    <<"An array is basically a pointer to a sequential memory block. where the name of the array represents the first location." This statement is true ... for most other languages.>> What makes you think so? This kind of logic is partially true (actually array implies also storage to contain the elements - not just pointer) only for C-derived languages (like C++, Objective C, D) but for most others it is not the case. In lots of popular languages there is no such thing as pointer (like in Java, PHP). – Serge Dundich May 11 '11 at 07:57
  • 1
    agreed for your second comment..... but what makes you think that C++ is not the extended version of C ? even the first name of C++ was "extended C".... only difference is the introduction of Object Oriented Concepts.... ... – Amit May 11 '11 at 08:12
  • 1
    @TheAmitKumar: The fist name of C++ predecessor was "C with classes". – Serge Dundich May 11 '11 at 08:33
  • 1
    @TheAmitKumar: "only difference is the introduction of Object Oriented Concepts...." It is not "only difference". It is the whole language. It is the whole point of C++ existence in the first place. Programming in C++ is totally different compared to programming in C. Totally different techniques, totally different thinking and totally different code. Usually programmers that think of C++ as some sort of extension of C produce C-like crappy code full of memory leaks, exception-safety problems and so on - usually rewriting such code is the only way to fix it. – Serge Dundich May 11 '11 at 08:40
  • @Serge : ofcourse I know that OOP is the biggest feature of C++ language as a whole.... and I also know its pros and cons.... every good programmer knows it... you are misunderstanding me. I am a C++ programmer by profession. I know its hell if you don't follow OOP concepts in larger projects. And regarding the first name of the language. You are right but just read it once again. – Amit May 11 '11 at 08:52
  • <<"An array is basically a pointer to a sequential memory block. where the name of the array represents the first location of that block." This statement is true for both C and C++, and for most other languages which have pointers in their kitty.>> Like I said before it is true only for C-derived languages. It is NOT true for the most other languages INCLUDING those that have pointers. There are lots of languages (like Pascal/Object Pascal, Ada, etc.) that has pointers but array type is just like any other object type (and don't use that C-style array-is-pointer logic). – Serge Dundich May 11 '11 at 13:20