How to dynamically highlight a single int [10] object?

Obviously, this will not work, because it is interpreted as the distribution of the int array:

 int (*ptr)[10] = new int[10]; 

Surprisingly, this does not work either:

 typedef int arr10[10]; arr10 *ptr = new arr10; 

It works:

 ptr = new int[1][10]; 

but it uses new[] , not new .

For my own curiosity, is it possible to use new to select one object of type int[10] instead of using new[] to select an array of 10 objects or an array of one object int[10]

+7
c ++
source share
5 answers

It's impossible. Arrays in C ++ do not have first-class object semantics, for example, they cannot be passed to functions * or returned from functions.

You can use std :: array in C ++ 11. It has many advantages: object semantics, debug mode index checking, iterator and STL interface, size() method, implicit conversion to pointers. When you have access to the C ++ 11 compiler, there is almost no reason to use the old old C arrays.


* You can pass pointers to elements or pointers / links to an array.

+7
source share

You select an array of objects.

T[10] is 10 objects, not one. In fact, these are eleven objects - an array ( array of 10 T ) containing 10 T objects.

This requires a constructor call for each T and corresponding calls to the destructor when the array receives delete d, so new[] and delete[] necessary for the arrays, and if you try to work around this, your code will be broken.

So:

 int * ptr = new int[10]; // ... delete [] ptr; 

Or Enlarge it (because bare pointers are ugly and error prone):

 boost::shared_array< int > ptr( new int[10] ); // ... 

Or (because not everyone likes Boost, and the standard brought us the <array> class :

 std::array< int, 10 > a; // ... 

Or the old standby <vector> :

 std::vector< int > v( 10 ); // ... 
+2
source share

new int[10] selects one object of type int[10] , that is, an array of 10 int s. However, the return value of the expression is a pointer to the first element of this array. Citation C ++ 14, 5.3.4 / 5:

When the selected object is an array (i.e., the syntax is noptr-new-declarator or the identifier of a new type or type-id denotes the type of an array), the new expression gives a pointer to the original element (if any) of the array. [Note: both new int and new int[10] are of type int* , and the type of new int[i][10] is int (*)[10] -end note]

+2
source share

One of the methods that work 1 within the framework of system rules of imperfect type:

 auto main() -> int { using Ten_ints = int [10]; Ten_ints* p_ints = []() -> Ten_ints* { struct Wrapper{ Ten_ints a; }; return &(new Wrapper)->a; }(); (void) p_ints; } 

A more direct path, taking responsibility:

 auto main() -> int { using Ten_ints = int [10]; Ten_ints* p_ints = reinterpret_cast<Ten_ints*>( new Ten_ints ); (void) p_ints; } 

The first fragment does what the OP requested (cited at the end of this answer), using plain new instead of new[] to highlight the object. The second snippet shows the most natural IMO way to use new[] . OP's suggestion for new int[1][10] is the second way to do this (and thus is the third way to get a pointer to an array) and has the advantage of working in a type system, but the disadvantage is that it is not very obvious.

In conclusion, the answer to

" you can use [non-array] new to select a single object of type int[10]

of course.


1 The standard guarantees that there is no padding at the beginning of the POD struct . In practice, this means that release is also not a problem. But for the formal one needs to use the same type for release as for allocation.

+2
source share

The new operator allocates memory for one type element. This is how it works. If you want to select a sequence from more than one element, you need to use the array operator and specify the number of the element you want new int[5] . If you really want to do something like this, you can define a structure and define an array on it. So after that, when you call new, it will allocate the array itself.

 typedef struct TEST{ int testInt[5]; }; TEST* myVarTest = new TEST(); 
+1
source share

All Articles