STARTING OUT WITH C++FROM CONTROL STRU
18th Edition
ISBN: 9781323815458
Author: GADDIS
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Concept explainers
Question
Chapter 9.9, Problem 9.14CP
Program Plan Intro
Purpose of “new” operator:
The main purpose of the “new” operator is to dynamically allocate memory. Dynamic memory allocation is process where an individual chunk of memory space is allocated by the compiler that is sufficient for the storage of variables.
Purpose of “delete” operator:
The purpose of “delete” operator is to clear or free the memory that was allocated using a “new” operator. The allocated memory gets de-allocated when “delete” operator is used.
Example:
//null pointer declaration
int *myptr = nullptr;
//dynamic allocation of memory
myptr = new int;
//free the memory allocated
delete myptr;
//assign the value to be null
myptr= nullptr;
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
please give implementation of
void write(u_int32_t address, u_int32_t data){///// REPLACE THIS /////read_fifo(address);return;}
What is the correct way to assign the pointer p to the address of x?. Single choice.
p = %x;
p = &x;
&p = *x;
*p = &x;
What is the address of (int) m[i][j] in terms of the base address and COLS?
What are potential problems with <code>strcpy()</code> and <code>strcat</code>
Why does <code>scanf()</code> take a pointer?
Chapter 9 Solutions
STARTING OUT WITH C++FROM CONTROL STRU
Ch. 9.5 - Prob. 9.1CPCh. 9.5 - Write the definition statement for a variable...Ch. 9.5 - List three uses of the symbol in C++.Ch. 9.5 - What is the output of the following code? int x =...Ch. 9.5 - Rewrite the following loop so it uses pointer...Ch. 9.5 - Prob. 9.6CPCh. 9.5 - Prob. 9.7CPCh. 9.5 - Is each of the following definitions valid or...Ch. 9.9 - Prob. 9.9CPCh. 9.9 - Give an example of the proper way to call the...
Ch. 9.9 - Complete the following program skeleton. When...Ch. 9.9 - Look at the following array definition: const int...Ch. 9.9 - Assume ip is a pointer to an int. Write a...Ch. 9.9 - Prob. 9.14CPCh. 9.9 - Prob. 9.15CPCh. 9.9 - Prob. 9.16CPCh. 9.9 - Prob. 9.17CPCh. 9 - What does the indirection operator do?Ch. 9 - Look at the following code. int x = 7; int iptr =...Ch. 9 - So far you have learned three different uses for...Ch. 9 - Prob. 4RQECh. 9 - Prob. 5RQECh. 9 - Prob. 6RQECh. 9 - What is the purpose of the new operator?Ch. 9 - What happens when a program uses the new operator...Ch. 9 - Prob. 9RQECh. 9 - Prob. 10RQECh. 9 - Prob. 11RQECh. 9 - Prob. 12RQECh. 9 - Each byte in memory is assigned a unique...Ch. 9 - The _________ operator can be used to determine a...Ch. 9 - Prob. 15RQECh. 9 - The ________ operator can be used to work with the...Ch. 9 - Array names can be used as ________, and vice...Ch. 9 - Prob. 18RQECh. 9 - The ________ operator is used to dynamically...Ch. 9 - Under older compilers, if the new operator cannot...Ch. 9 - Prob. 21RQECh. 9 - When a program is finished with a chunk of...Ch. 9 - You should only use pointers with delete that were...Ch. 9 - Prob. 24RQECh. 9 - Look at the following array definition: int...Ch. 9 - Prob. 26RQECh. 9 - Assume tempNumbers is a pointer that points to a...Ch. 9 - Look at the following function definition: void...Ch. 9 - Prob. 29RQECh. 9 - Prob. 30RQECh. 9 - Prob. 31RQECh. 9 - T F The operator is used to get the address of a...Ch. 9 - T F Pointer variables are designed to hold...Ch. 9 - T F The symbol is called the indirection...Ch. 9 - T F The operator dereferences a pointer.Ch. 9 - T F When the indirection operator is used with a...Ch. 9 - T F Array names cannot be dereferenced with the...Ch. 9 - Prob. 38RQECh. 9 - T F The address operator is not needed to assign...Ch. 9 - T F You can change the address that an array name...Ch. 9 - T F Any mathematical operation, including...Ch. 9 - T F Pointers may be compared using the relational...Ch. 9 - T F When used as function parameters, reference...Ch. 9 - T F The new operator dynamically allocates memory.Ch. 9 - T F A pointer variable that has not been...Ch. 9 - Prob. 46RQECh. 9 - T F In using a pointer with the delete operator,...Ch. 9 - Prob. 48RQECh. 9 - Prob. 49RQECh. 9 - int x, ptr = nullptr; ptr = x;Ch. 9 - Prob. 51RQECh. 9 - Prob. 52RQECh. 9 - Prob. 53RQECh. 9 - float level; int fptr = level;Ch. 9 - Prob. 55RQECh. 9 - Prob. 56RQECh. 9 - Prob. 57RQECh. 9 - Prob. 58RQECh. 9 - int pint = nullptr; pint = new int[100]; //...Ch. 9 - Prob. 60RQECh. 9 - Prob. 61RQECh. 9 - Prob. 62RQECh. 9 - Array Allocator Write a function that dynamically...Ch. 9 - Test Scores #1 Write a program that dynamically...Ch. 9 - Drop Lowest Score Modify Problem 2 above so the...Ch. 9 - Test Scores #2 Modify the program of Programming...Ch. 9 - Prob. 5PCCh. 9 - Case Study Modification #1 Modify Program 9-19...Ch. 9 - Case Study Modification #2 Modify Program 9-19...Ch. 9 - Mode Function In statistics, the mode of a set of...Ch. 9 - Median Function In statistics, when a set of...Ch. 9 - Reverse Array Write a function that accepts an int...Ch. 9 - Array Expander Write a function that accepts an...Ch. 9 - Element Shifter Write a function that accepts an...Ch. 9 - Movie Statistics Write a program that can be used...
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Similar questions
- 1)Answer the question with the correct option A) Which of the following assigns to the pointer p to the address of value? options are given p = value; *p = &value; p = &value; p = &value; B) What is the output of the following code fragment? Options are given below the code: int v1=2, v2=-1, *p1, *p2; p1 = &v1; p2 = &v2; p2 = p1; cout << *p2 << endl; 2 -2 1 -1 C) Which of the following statements correctly prints out the value that is in the memory address that the pointer p is pointing to? cout << int* p; cout << p; cout << *p; cout << &p;arrow_forwardGiven the declaration below, if array aData[][] was associated to the memory location 1010 (in decimal), what is the value/address (in decimal) equivalent to aData[5] ? double aData[6][4]; Note that sizeof(double) results to 8, meaning 8 bytes.arrow_forwardWrite a C program that uses the following: a main() to read two integer values from the user, val1 and val2, and prints the returned value from swap().a swap() that uses call by reference (takes the addresses into pointers) to swap values, and prints their values after the swap "num1 = # and num2 = #". This function returns the largest of the two values. If these are equal, it returns their sum.arrow_forward
- Write a short C program that declares and initializes (to any value you like) adouble, an int, and a char. Next declare and initialize a pointer to each of thethree variables. Your program should then print the address of, and value storedin, and the memory size (in bytes) of each of the six variables.Use the “%p” formatting specifier to print addresses in hexadecimal. You shouldsee addresses that look something like this: "0xbfe55918". The initial characters"0x" tell you that hexadecimal notation is being used; the remainder of the digitsgive the address itself. Use proper formatting specifiers for char, int and double.Use the sizeof operator to determine the memory size allocated for each variable.Sample output from your program:The address of char ___ is 0x_______The address of int ___ is 0x_______The address of double ___ is 0x_______The address of char* ___ is 0x_______The address of int* ___ is 0x_______The address of double* ___ is 0x_______The value of char ___ is _______The…arrow_forwardWrite a short C program that declares and initializes (to any value you like) adouble, an int, and a char. Next declare and initialize a pointer to each ofthe three variables. Your program should then print the address of, and valuestored in, and the memory size (in bytes) of each of the six variables.Use the “0x%x” formatting specifier to print addresses in hexadecimal. Youshould see addresses that look something like this: "0xbfe55918". The initialcharacters "0x" tell you that hexadecimal notation is being used; the remainderof the digits give the address itself.Use the sizeof operator to determine the memory size allocated for eachvariable.*/arrow_forwardWrite a block of code that uses a dynamic allocated array named Array to (1) Load ten integers from the user into the memory, (2) Find the number of negative integers in the array, and (3) Print a message with the number of negatives. Whenever no numbers are negative print an additional message "No negatives!" (Assume all libraries are included)arrow_forward
- Assume that pointer ptr is a pointer to a double, and has initial value of 0xfc0f. What is the value of ptr after the pointer has been decremented? (assume a 4 byte double) Group of answer choices 0xfb0e 0xfc13 0xfc0b 0xfc0earrow_forwardDeclare a character field, assign values to it (e.g. Next, declare a pointer to this field and write the actual address it points to on the screen (eg printf (“% p”, pointer);). Then change the address value (add a unit). Enter a value for this address and return the string to the screen. What has happened? Finally, change the address value to a very high value and try writing something again. What happens? The program should fall. Explain why. Explan all codes.thanksarrow_forward// Program 4.4.1#include <iostream>using namespace std;int main (){ int var = 20; // actual variable declaration. int *ip; // pointer variable ip = &var; // store address of var in pointer variable cout << "Value of var variable: "; // print the variable value cout << var << endl<<endl; cout << "Address stored in ip variable: "; // print the address stored in ip pointer variable cout << ip << endl<<endl; cout << "Value of *ip variable: "; // access the value at the address available in pointer cout << *ip << endl; return 0;} TASK 1. Explain in details what the program does? 2. What is the output.arrow_forward
- 4) Answer the question in C++ A) What is wrong with the following code segment? enum CarType {GM, FORD, BMW}; int main() { CartType = BMW; return 0; } B) Assume that the addresses of x and y is 0x61feb8 and 0x61feb0, respectively. What will be the values of x, y, p, and q, respectively, when the following code runs successfully? int main() { int x; double y; int* p = &x; double* q = &y; *p = 3; *q = 3.14; increaseByOne(p, q); cout << x << " " << y << endl; cout << p << " " << q << endl; return 0; }void increaseByOne(int*& p1, double* q1){ (*p1)++; (*q1)++; p1++; q1++;} The four value:arrow_forwardIn c++ you have a data in a text file, the data consists of a chatacter separated by a comma then an integer also separate md by a comma then another integer Example A,11,1 B,13,2 C,14,4 D,15,7 E,40,1 F,50,1 How will you read and store the data in a vector such that the character will be stored in vector char, and the two integers will be stored in vector x and y respectively.arrow_forwardConsider the following code: int main() { char *s = strdup("This is a test"); char *d = " "; while (s) { char *token = strsep(&s, d); puts(token); } free(s); return 0;} Which of the following causes an error or segmentation fault? A. free is called on a pointer that was not malloc'ed B. strsep must be called with s instead of &s C. The while loop is an infinite loop D. The string s points to is in read-only memory E. Something elsearrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning