answersLogoWhite

0


Best Answer

The simplest way is to allocate raw memory of the required length using a pointer-to-pointer to the class of object. Once you have the memory, you can access the individual elements just as you would any other type of array, to either instantiate new objects or to point to existing objects.

The following example uses the class itself to allocate a dynamic array of a given size, via a static member function.

// Declare a simple class with a default constructor,

// one member variable and a static member function.

class MyClass

{

public:

MyClass():m_int(0){} // Default constructor initialises member variable.

private:

int m_int; // Member variable.

public:

static MyClass** CreateArray(unsigned int count); // Static member function.

};

// Implementation of the static member function.

MyClass** MyClass::CreateArray(unsigned int count)

{

MyClass** ppResult = NULL;

if( count )

{

// Calculate size of allocation.

size_t size = count * sizeof( MyClass* );

// Allocate memory and zero.

if( ppResult = ( MyClass** ) malloc( size ))

memset( ppResult, 0x00, size );

}

return( ppResult );

}

int main()

{

// Some variables.

int i = 0;

MyClass** ppArray;

// Instantiate objects in a fixed-size array (uses default constructor).

MyClass Array[10];

// Instantiate a dynamic array of objects (and check for NULL).

if( ppArray = MyClass::CreateArray( 10 ))

{

// Point array elements to the existing objects.

for( i=0; i<10; ++i )

ppArray[i] = &Array[i]; // Any existing object will do here.

// ...do stuff...

// Finished with dynamic array (does NOT destroy the existing objects).

delete( ppArray );

ppArray = NULL;

}

// Instantiate a new dynamic array (and check for NULL).

if( ppArray = MyClass::CreateArray( 5 ))

// Instantiate new objects via default constructor.

for( i=0; i<5; ++i )

ppArray[i] = new MyClass();

// Note: it's worth checking each element is not NULL before accessing it!

// ...do stuff...

// Destroy each object that was created.

for( int i=0; i<5; ++i )

{

delete( ppArray[i] );

ppArray[i] = NULL;

}

// Finished with dynamic array.

delete( ppArray );

ppArray = NULL;

}

return( 0 );

// Array[10] will now fall from scope...

}

User Avatar

Wiki User

12y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How would you create space for an array of object using pointer and give examples?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Passing an array name to a pointer assigns the first memory location of the array to the pointer variable?

Yes, passing an array name to a pointer assigns the first memory location of the array to the pointer variable. An array name is the same as a pointer to the first location of the array, with the exception that an array name is a r-value, while a pointer is an l-value.


Does mentioning the array name gives the base address in all the contexts?

Mentioning the array name in C or C++ gives the base address in all contexts except one. Syntactically, the compiler treats the array name as a pointer to the first element. You can reference elements using array syntax, a[n], or using pointer syntax, *(a+n), and you can even mix the usages within an expression. When you pass an array name as a function argument, you are passing the "value of the pointer", which means that you are implicitly passing the array by reference, even though all parameters in functions are "call by value". There is, however, one very important distinction. While an array name is referentially the same as a pointer, it is not a pointer in that it does not occupy program referential space in the process. This means that, while you can change the value of a pointer, and thus the address to which it points, you can not change the value of an array name. This distinction is what we call R-Value (array or pointer) as opposed to L-Value (pointer only), i.e. can the object appear on the left sign of an assignment operator.


Which two pointer does not increment or decrement in arithmetic array?

constant pointer and character pointer


How do you return an array from function?

By returning a pointer to the first element of the array.


What is an array of pointers to pointers?

A pointer is a variable that stores value of address of a variable. Since a pointer itself is a variable, it is allocated a memory location.Pointer to pointer means a pointer which points to the address of a pointer. In other words a pointer to a pointer has the address of the address of a variable.We can have pointers to int, and pointers to char, and pointers to any structures we've defined, and in fact pointers to any type in C, it shouldn't come as too much of a surprise that we can have pointers to other pointers. If we're used to thinking about simple pointers, and to keeping clear in our minds the distinction between the pointer itself and what it points to, we should be able to think about pointers to pointers, too, although we'll now have to distinguish between the pointer, what it points to, and what the pointer that it points to points.

Related questions

Passing an array name to a pointer assigns the first memory location of the array to the pointer variable?

Yes, passing an array name to a pointer assigns the first memory location of the array to the pointer variable. An array name is the same as a pointer to the first location of the array, with the exception that an array name is a r-value, while a pointer is an l-value.


How do you use character in c plus plus?

If you are referring to the character object 'char,' then here are a couple of uses:To create an object, use this:char object = 'a';To create an array of chars, use this:char array[10];To dynamically allocate an array of chars, use this:char array = new char[10];(Don't forget to delete the object with 'delete [] array')


What is a pointer in the array?

A pointer into an array of elements of type E is a pointer to a single element of type E:typedef ..... E;E array[123];E* const pointer = &array[18]; // points to the 19th element inside 'array'An array of pointers is an array whose elements are pointers:typedef .... E;E* array[123];E** const pointer = &array[18]; // points to the 19th pointer within 'array'Referencing the name of the array variable without use of the index operator itself is a constant pointer to its first element. Therefore, the following if-clause is always true:typedef .... E;E array[123];if (array &array[N]) { // ALWAYS true ...}


Why you use an array of pointer to pointer?

because u freakin can


Which two pointer does not increment or decrement in arithmetic array?

constant pointer and character pointer


Does mentioning the array name gives the base address in all the contexts?

Mentioning the array name in C or C++ gives the base address in all contexts except one. Syntactically, the compiler treats the array name as a pointer to the first element. You can reference elements using array syntax, a[n], or using pointer syntax, *(a+n), and you can even mix the usages within an expression. When you pass an array name as a function argument, you are passing the "value of the pointer", which means that you are implicitly passing the array by reference, even though all parameters in functions are "call by value". There is, however, one very important distinction. While an array name is referentially the same as a pointer, it is not a pointer in that it does not occupy program referential space in the process. This means that, while you can change the value of a pointer, and thus the address to which it points, you can not change the value of an array name. This distinction is what we call R-Value (array or pointer) as opposed to L-Value (pointer only), i.e. can the object appear on the left sign of an assignment operator.


Why can't we increment an array like a pointer?

once we initialize the array variable, the pointer points base address only &amp; it's fixed and constant pointer


What is pointer to a structure?

A pointer is a variable that holds address information. For example, in C++, say you have a Car class and another class that can access Car. Then, declaring Car *car1 =new Car() creates a pointer to a Car object.. The variable "car1" holds an address location.


How do you return an array from function?

By returning a pointer to the first element of the array.


What is an array of pointers to pointers?

A pointer is a variable that stores value of address of a variable. Since a pointer itself is a variable, it is allocated a memory location.Pointer to pointer means a pointer which points to the address of a pointer. In other words a pointer to a pointer has the address of the address of a variable.We can have pointers to int, and pointers to char, and pointers to any structures we've defined, and in fact pointers to any type in C, it shouldn't come as too much of a surprise that we can have pointers to other pointers. If we're used to thinking about simple pointers, and to keeping clear in our minds the distinction between the pointer itself and what it points to, we should be able to think about pointers to pointers, too, although we'll now have to distinguish between the pointer, what it points to, and what the pointer that it points to points.


How do you convert from array notation to pointer notation?

In the C and C++ languages the array notation arr[i] is completely equivalent to the pointer notation *(arr + i).


Pointer to 3 dimensons array?

If the array is static you can simply point at the first element. For dynamic arrays you can allocate a contiguous block to a single pointer which can then be subdivided using a one-dimensional array of pointer to pointers, each of which points to a one-dimensional array of pointers, each of which points to a separate object within the array. For extremely large arrays, however, it is better to split the elements into separate one-dimensional arrays, by creating a one-dimensional array of pointer to pointers first, then allocating each of those pointers to a separate one-dimensional array of pointers, each of which points to a separate one-dimensional array of objects. Either way, you must destroy all the individual arrays in the reverse order of creation.