How to create an Array of Objects in the Stack memory?
Last Updated :
06 Apr, 2023
What is an Array of Objects?
An array of objects is a data structure that stores a collection of objects of the same type. The objects in the array are stored in contiguous memory locations, and the array provides indexed access to the objects. This means that you can access an individual object in the array using its index, just like you would with an array of primitive data types (e.g., int, float, etc.).
How to create an Array of Objects in the Stack memory?
To create an array of objects on the stack in C++, you can use the following syntax:
Type name[size];
Here, ‘Type‘ is the type of the objects in the array (e.g., int, float, MyClass, etc.); ‘name‘ is the name of the array, and size is the number of elements in the array.
For example:
int array[10]; // Creates an array of 10 integers
MyClass objects[20]; // Creates an array of 20 objects of type MyClass
Issues while creating an Array of Objects in Stack memory:
Keep in mind that creating an array of objects on the stack has some limitations.
- The size of the array must be a compile-time constant,
- The array is stored in the stack, which has limited space. If you need to store a large number of objects or if the size of the array is not known at compile time, you may want to consider using a dynamic array or creating the objects on the heap using new.
Algorithm:
Here is a simple algorithm that creates an array of n objects of type MyClass on the stack and initializes each object with a value val:
- Define a variable array of type MyClass.
- Iterate through the elements of the array.
- For each element i, do the following:
- Call the default constructor of MyClass to create a new object in array[i].
- Set the value of array[i] to val.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
class MyClass {
public :
MyClass() {}
void setValue( int val) { value = val; }
int getValue() const { return value; }
private :
int value;
};
int main()
{
const int n = 5;
const int val = 10;
MyClass array[n];
for ( int i = 0; i < n; i++) {
array[i].setValue(val);
}
for ( int i = 0; i < n; i++) {
cout << "Object " << i << ": "
<< array[i].getValue() << endl;
}
return 0;
}
|
Python3
class MyClass:
def __init__( self ):
self .value = None
def setValue( self , val):
self .value = val
def getValue( self ):
return self .value
if __name__ = = '__main__' :
n = 5
val = 10
array = [MyClass() for _ in range (n)]
for i in range (n):
array[i].setValue(val)
for i in range (n):
print ( "Object" , i, ":" , array[i].getValue())
|
Java
import java.util.Arrays;
class MyClass {
private int value;
public MyClass() {}
public void setValue( int val) { value = val; }
public int getValue() { return value; }
}
public class Main {
public static void main(String[] args) {
final int n = 5 ;
final int val = 10 ;
MyClass[] array = new MyClass[n];
for ( int i = 0 ; i < n; i++) {
array[i] = new MyClass();
array[i].setValue(val);
}
for ( int i = 0 ; i < n; i++) {
System.out.println( "Object " + i + ": " + array[i].getValue());
}
}
}
|
C#
using System;
class MyClass
{
private int value;
public MyClass()
{
}
public void SetValue( int val)
{
value = val;
}
public int GetValue()
{
return value;
}
}
class Program
{
static void Main( string [] args)
{
const int n = 5;
const int val = 10;
MyClass[] array = new MyClass[n];
for ( int i = 0; i < n; i++)
{
array[i] = new MyClass();
array[i].SetValue(val);
}
for ( int i = 0; i < n; i++)
{
Console.WriteLine( "Object " + i + ": " + array[i].GetValue());
}
Console.ReadLine();
}
}
|
Javascript
class MyClass {
constructor() {
this .value = 0;
}
setValue(val) {
this .value = val;
}
getValue() {
return this .value;
}
}
const n = 5;
const val = 10;
const array = new Array(n).fill( null ).map(() => new MyClass());
for (let i = 0; i < n; i++) {
array[i].setValue(val);
}
for (let i = 0; i < n; i++) {
console.log(`Object ${i}: ${array[i].getValue()}`);
}
|
Output
Object 0: 10
Object 1: 10
Object 2: 10
Object 3: 10
Object 4: 10
Output:
Output of the previous code
Auxiliary space: O(n)
time complexity: O(n)
Share your thoughts in the comments
Please Login to comment...