Question

how to allocate an array on the heap and stack? Create a constructor and a destructor...

how to allocate an array on the heap and stack? Create a constructor and a destructor if it's needed.

Please provide example code in C++.

Homework Answers

Answer #1

array allocation on stack is done when the array is declared with specified size in the program itself before compilation but not dynamically i.e if the compiler knows then it will allocate memory in stack. Otherwise the programmer can allocate dynamically or can force the memory allocation in heap by using "new" operator.

Example :-

#include <iostream>

using namespace std;
class example{
public:
int *arr_heap;
example(){

//allocation in heap memory
arr_heap = new int[10];
cout<<"heap allocation"<<endl;
}
~example(){

//de-allocation in heap memory
free(arr_heap);
cout<<"heap de-allocated"<<endl;
}
};

int main() {
int arr_stack[10];
//de-allocation in stack memory
example obj;
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Please create an array of Leg objects, one constructor that takes three parameters as constant C...
Please create an array of Leg objects, one constructor that takes three parameters as constant C string, and one number representing the distance in miles between the two cities Write a code block to create a static array (that is, not dynamic and not a vector) of 3 Leg objects using city names of your choosing. That's THREE objects, each created using THREE parameters. For example, the Leg class declaration looked like, class Leg { const char* const startCity; const...
suppose you have allocated an array of twenty doubles, called values, on the heap. Write the...
suppose you have allocated an array of twenty doubles, called values, on the heap. Write the line of code (including the semicolon) to free up the memory when the array is no longer needed. c code
C++ Itty Bitty Airfreight (IBA) Lab 3.1 Create your objects in the stack (not on the...
C++ Itty Bitty Airfreight (IBA) Lab 3.1 Create your objects in the stack (not on the heap). Add a friend function, kilotopound, which will convert kilograms to pounds. Change your weight mutator to ask whether weight is input in kilograms or pounds. If it is kilograms, call the friend function kilotopound to convert it to pounds and return pounds. There are 2.2 pounds in one kilogram. Create an object on the stack with the following information: uld – Container abbreviation...
10. Number Array Class Design a class that has an array of floating-point numbers. The constructor...
10. Number Array Class Design a class that has an array of floating-point numbers. The constructor should accept an integer argument and dynamically allocate the array to hold that many numbers. The private data members of the class should include the integer argument in a variable to hold the size of the array and a pointer to float type to hold the address of the first element in the array. The destructor should free the memory held by the array....
1.when we create a dynamic array the area of memory used is called a. runtime area...
1.when we create a dynamic array the area of memory used is called a. runtime area b. usable memory area c. heap d. stack
In C++ PART 1: Write a constructor for a class data type named Test that has...
In C++ PART 1: Write a constructor for a class data type named Test that has the following two member variables: double arr*; int size; The constructor should take one argument for the size and then dynamically allocate an array of that size. PART 2: Write a destructor for the Test class.
C Programming 1. Write a void function that takes in a 2D array of character and...
C Programming 1. Write a void function that takes in a 2D array of character and have it print out each array on a new numbered line on the console. 2. Illustrate the stack and the heap allocation. Specify what each variable value holds and where different references are pointing to. int main() { int myarray[3]; myfunction (myarray); } int myfunction(int* in) { int i; for (i = 0; i<3; i+= 1) { in[i] = i; } // illustrate the...
Unsure as to how I would be able to have an array behave as a stack...
Unsure as to how I would be able to have an array behave as a stack in java by removing the last (top) element of an array as well as pushing a new element into the top to the top of the stack. All without using any packages.
You are asked to implement a C++ class to model a sorted array of unsigned integers....
You are asked to implement a C++ class to model a sorted array of unsigned integers. The class is to be used in an embedded application that cannot assume the presence of the STL. The array has to be dynamically allocated in such a way that allows programmers using it to specify the required size. Your class should should: (1) provide the appropriate constructors and destructor; (2) provide methods for updating, and showing numbers in/to the array (e.g., to be...
Using this code (below) create a diagram that shows what the heap layout looks like and...
Using this code (below) create a diagram that shows what the heap layout looks like and explain how to exploit it (Include a diagram) Include the following items: Each chunk of memory allocated by malloc() and their metadata. Their sizes in bytes. The overflow direction in the heap. The size of the overflowing buffer to reach and overwrite the metadata. Overflow data that is meaningful for an exploit (this can be general). Code: #include <stdio.h> #include <string.h> #include <stdlib.h> int...