
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
expand_more
expand_more
format_list_bulleted
Concept explainers
Question
![Part II: Understand Pointers/Dynamic Memory
To-do 2:
Write 3 different functions in C++ to create memory on the heap without causing a memory
leak.
Hint: You will need to assign the address to a pointer that was created outside the function. Remember,
you can return an address or change what a pointer points to (the contents of a pointer) in a function by
passing the pointer by reference or by passing the address of the pointer.
Additional questions:
What if you want to change the contents of what the pointer points to? Make a function that will set
the contents of the space on the heap. Do you still need to pass by reference or the address of the
pointer? Why or why not?
How will you delete the memory off the heap? Try doing it outside a function, and inside a function.
Make sure your delete function is setting your pointer back to NULL, since it is not supposed to be
pointing anywhere anymore.
You can check to see if you have any memory leaks using valgrind.
Svalgrind program_exe
Part IlI: Implementing 1-D arrays in assignment 4
To-do 3:
Create a C-style string that will hold a sentence/paragraph inputted by the user.
You can make a static array for this, but make sure your getline has the appropriate length.
Use cin.getline (array_name, num_chars_to_read) for C-style strings.
What do you think may happen if you enter more characters than specified by
num_chars_to_readin cin.getline ()? The code below may help this problem.
if (cin.fai1()) {
cin.ignore (256,'\n'); //get rid of everything leftover
cin.clear ();
//reset the failbit for the next cin
}
To-do 4:
Create an array of C++ strings for any N number of words that user wants to search for
and read those words from the user.
Ask the user how many words they want to search for.
Create an array of that many words
Read each word from the user. Remember, you would use getline for C++ strings.
getline (cin, word_array_name[i]);
You need to have a main function that asks for a sentence/paragraph and for the N words. After getting
inputs from the user, print out both arrays to check if they are working properly. Remember to check
memory leaks as well.](https://content.bartleby.com/qna-images/question/8759b09c-a99c-4616-917a-1eb84cf5071d/3bbe75a1-5da9-4bf0-95b0-a318cb060cc4/yado48_thumbnail.jpeg)
Transcribed Image Text:Part II: Understand Pointers/Dynamic Memory
To-do 2:
Write 3 different functions in C++ to create memory on the heap without causing a memory
leak.
Hint: You will need to assign the address to a pointer that was created outside the function. Remember,
you can return an address or change what a pointer points to (the contents of a pointer) in a function by
passing the pointer by reference or by passing the address of the pointer.
Additional questions:
What if you want to change the contents of what the pointer points to? Make a function that will set
the contents of the space on the heap. Do you still need to pass by reference or the address of the
pointer? Why or why not?
How will you delete the memory off the heap? Try doing it outside a function, and inside a function.
Make sure your delete function is setting your pointer back to NULL, since it is not supposed to be
pointing anywhere anymore.
You can check to see if you have any memory leaks using valgrind.
Svalgrind program_exe
Part IlI: Implementing 1-D arrays in assignment 4
To-do 3:
Create a C-style string that will hold a sentence/paragraph inputted by the user.
You can make a static array for this, but make sure your getline has the appropriate length.
Use cin.getline (array_name, num_chars_to_read) for C-style strings.
What do you think may happen if you enter more characters than specified by
num_chars_to_readin cin.getline ()? The code below may help this problem.
if (cin.fai1()) {
cin.ignore (256,'\n'); //get rid of everything leftover
cin.clear ();
//reset the failbit for the next cin
}
To-do 4:
Create an array of C++ strings for any N number of words that user wants to search for
and read those words from the user.
Ask the user how many words they want to search for.
Create an array of that many words
Read each word from the user. Remember, you would use getline for C++ strings.
getline (cin, word_array_name[i]);
You need to have a main function that asks for a sentence/paragraph and for the N words. After getting
inputs from the user, print out both arrays to check if they are working properly. Remember to check
memory leaks as well.
Expert Solution

This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution
Trending nowThis is a popular solution!
Step by stepSolved in 2 steps

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
- Write a paper that summarizes the chapter (or an online resource) that may help you understand the concept of pointers. Include the following topics in the paper. Include illustrations either from the book, or an online source or your own illustration. Come up with an introduction Pointer Variable Declarations and Initialization Pointer Operators Pass-by-Reference with Pointers Built-In Arrays Using const with Pointers sizeof Operator Pointer Expressions and Pointer Arithmetic Relationship Between Pointers and Built-In Arrays Pointer-Based String About Smart Pointersarrow_forwardInstructions: You are strictly not allowed to use anything other than pointers and dynamic memory. One function should perform one functionality only. Task 1 Write a program in C++ that reads data from a file. Create dynamic memory according to the data. Now your task is to perform the following task. Row wise Sum Column wise Sum Diagonal wise Sum Example data.txt 4 5 1.6 10.2 33.7 99 20.5 3 44 50 96.1 2 8 9 4 74 50 99 19.1 Output: Sum row wise: 165, 191, 17, 242.1 Sum col wise: 127.6, 120.1, 228.8, 118.1, 20.5 Sum diagonal wise: Not Possible Note: You are restricted to use pointers and your function should be generic. Avoid memory wastage, memory leakage, dangling pointer. Use regrow or shrink concepts if required.arrow_forwardWhat is a pointer? How do you dereference a pointer to get its stored value?arrow_forward
- How simple is it to transfer shared references into another array in C++? List approaches to tackle the issue. Do shared pointers copy the objects they control? Explainarrow_forwardHow can I quickly copy a group of shared references from one array into another in C++? Create a list of potential responses to the challenge at hand. Does copying shared pointers also copy the objects they manage? Explainarrow_forwardHow simple is it to transfer shared references into another array in C++? List approaches to tackle the issue. Do shared pointers transfer the objects they control? Explain?arrow_forward
- need help c++ Write the specified code: 1. Initialize an dynamic integer array of size 5 with the values 1,2,3,4,5. 2. Use pointer arithmetic to swap the 2nd and 4th values in the array. 3. Use a loop and pointer arithmetic to print all array values. PLEASE USE POINTER ARTHIMETIC IN YOUR CODEarrow_forwardCreate a c ++ program keeping in mind the concepts about Pointers (Pointers). Instructions: Write a program to find the maximum of a set of integers within an array. The program should ask the user to enter each of the values. At the end the program prints a pointer to point to the maximum value. The example is shown in the image Note create the program in c ++arrow_forwardWhat is a way to safely dereference a pointer?arrow_forward
arrow_back_ios
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education

Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education

Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON

Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON

C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON

Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning

Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education