Question

Computer Science Midterm Exam Review Questions 21. How do you dynamically allocate variables in C++? 22....

Computer Science Midterm Exam Review Questions

21. How do you dynamically allocate variables in C++?

22. How do you dynamically allocate an array in C++?

23. How do you deallocate variables in C++?

24. How do you deallocate arrays in C++?

25. How do you dynamically allocate memory in C?

26. How do you deallocate memory in C?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

In c++ , new and delete operators are used to allocate and de-allocate memory respectively. The new operator denotes a request for memory allocation on the heap. If sufficient memory is available, new operator initializes the memory and returns the address of the newly allocated memory to the pointer variable.

Here, data type could be any built-in data type including array or any user defined data types including class and structure.

21. Syntax to dynamically allocate variables in c++:

data_type pointer_variable = new data_type;

example : int *p = new int;

22. Syntax to dynamically allocate array in c++:

data_type   pointer_variable = new data_type[ size ] ; // size variable specifies the number of elements in an array

int *p = new int[ 5 ] ; // creating an array of 5 elements

23 . Syntax to de-allocate variable in c++:

delete pointer_variable ; // it releases memory pointed by the pointer_variable.

example :

int *p = new int;

delete p; // delete the memory pointed by the pointer p

24. Syntax to de-allocate arrays in c++:

delete[ ]   pointer_variable;   // it will free the entire array pointed by the pointer_variable.

Following   4 library functions are provided by C defined under <stdlib.h> header file to facilitate dynamic memory allocation in C programming :

1. malloc()

2. calloc()

3. free()

4. realloc()

25. malloc() function is used to dynamically allocate a single large block of memory with the specified size. It returns a pointer of type void which can be cast into a pointer of any form.

pointer = (cast_type*) malloc(size in bytes); 

/* dynamically allocating memory using malloc().

Here, sizeof() operator is used to get the size of int in bytes.   */

int *p = (int *) malloc(sizeof(int));

int *p = (int*) malloc( n* sizeof(int)); // dynamically creating an array ( n denotes the number of elements in the array )

26. free() function is used to dynamically de-allocate the memory .

Syntax:

free(pointer_variable);

int *p = (int*) malloc(sizeof(int));

free(p); // deallocating memory pointed by the pointer p .

Add a comment
Know the answer?
Add Answer to:
Computer Science Midterm Exam Review Questions 21. How do you dynamically allocate variables in C++? 22....
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Computer Science Midterm Exam Review Questions 5. What is a struct, and how do you declare...

    Computer Science Midterm Exam Review Questions 5. What is a struct, and how do you declare one? 6. Define a struct and use it to solve a simple problem. 7. What is the main difference between structs in C and C++

  • Computer Science Midterm Exam Review Questions 11. What is a pointer? 12. Are pointers and array...

    Computer Science Midterm Exam Review Questions 11. What is a pointer? 12. Are pointers and array names the same thing? How are they alike? How do they differ?

  • computer science

    CSCI 3000 Homework 4In this assignment, you will implement a simple version of Computer Lab administration system in C++. Your program will monitor computers in 4 computer labs and will allow users to log in and log out. Each computer lab has different number of computers.·      Lab 1 has 10 computers·      Lab 2 has 6 computers·      Lab 3 has 3 computers·      Lab 4 has 12 computersHere is a sample state of the system:Lab ArraySome of the computers are free (no...

  • Question 4] All variables and headers properly defined for the C functional program. char st 80...

    Question 4] All variables and headers properly defined for the C functional program. char st 80 1: unsigned *ptri-s .a) What is the size of the array 's' in bytes? .b) ptr1 has a hex-decimal value of 28FE60 referring to the first element of st 1 array. What is the decimal value? c) Following this functional software; a Ans size- b Ans-- for (i 0; sl i0:+i) printf( "in %c: It %dVt",toupper(s[i] . (i.2), toupper( st i l . (i.2)));...

  • Word Bank: a) python b) computer science c) algorithm d) program e) interpreter f) compiler g)...

    Word Bank: a) python b) computer science c) algorithm d) program e) interpreter f) compiler g) syntax h) semantics i) value J) variable k) operator l) operand m) expression n) statement o) input p)output q)call r) arguments s) library t) bug u) variable scope v) local variable w)global variable x) variable lifetime y) relational operators z) logical operators 1) Compares operands and results in a bool: 2) The duration of a variable's existence: 3) A list of instructions to solve...

  • Assembly Language Programming Assignment program must be in: MASM assembly language / x86 architecture / irvine...

    Assembly Language Programming Assignment program must be in: MASM assembly language / x86 architecture / irvine library procedures Objectives: 1. using register indirect addressing 2. passing parameters 3. generating “random” numbers 4. working with arrays Description: Write and test a MASM program to perform the following tasks: 1. Introduce the program. 2. Generate ARRAYSIZE random integers in the range [LO = 10 .. HI = 29], storing them in consecutive elements of an array. ARRAYSIZE should be set to 200....

  • A university instructor uses different teaching methods on three separate computer science classes. The instructor wants...

    A university instructor uses different teaching methods on three separate computer science classes. The instructor wants to evaluate the effectiveness of the methods by comparing the grades between the classes. The grades have been transformed and are in the table below. What can the instructor conclude with a = 0.05? Method A Method B Method с 21 19 21 24 25 20 27 19 23 25 25 20 24 28 35 31 29 26 19 17 20 29 20 17...

  • this is c code. please answer all questions on a piece of paper and show work....

    this is c code. please answer all questions on a piece of paper and show work. i need to prepare as i have a midterm i will have to be completing on paper 1) Bit Operators: This C program compiles and runs. What is its output? 1) #include <stdio.h> 2) void main (void) 3) unsigned char x =60; 4) 5) 6) 7) 8 ) 9) 10) 11) 12) 13) unsigned char a = x < 1; unsigned char b unsigned...

  • Part 3.  Write a program to dynamically allocate some char buffers, use memset to fill them, and...

    Part 3.  Write a program to dynamically allocate some char buffers, use memset to fill them, and use memcpy and memmove to move the data around. I suggest you do part 3a, test and verify it’s working as expected, then add part b, test & verify, then do part 3c.  You only need to turn in the complete program, but doing it in steps will help both with understanding and if you have bugs along the way. Part 3a. a.Allocate 2 char...

  • CHEM-C 105 Principles of Chemistry I Summer Semester Practice Midterm Exam. 7? questions Note that questions are...

    CHEM-C 105 Principles of Chemistry I Summer Semester Practice Midterm Exam. 7? questions Note that questions are graded by answer only and work doesn't count HOW do You Aind 1. In which of the following are the masses given in the correct order? A. eg < mg <g< kg the Correct order 2 B. eg < g < kg < mg C. kg <g<eg < mg D. mg< eg<g< kg 2. For each of the diagrams above, determine how accurate...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT