Question

A.2 5 pts - VWhat is a dangling/stale pointer? And please give code example [Yes ]No Does delete delete a pointer? Please exp

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

`Hey,

Note: Brother in case of any queries, just comment in box I would be very happy to assist all your queries

A pointer pointing to a memory location that has been deleted (or freed) is called dangling pointer.

// Deallocating a memory pointed by ptr causes

// dangling pointer

#include <stdlib.h>

#include <stdio.h>

int main()

{

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

  

    // After below free call, ptr becomes a

    // dangling pointer

    free(ptr);

      

    // No more a dangling pointer

    ptr = NULL;

}

2)

Yes,

  • New operator is used for dynamic memory allocation which puts variables on heap memory.
  • Which means Delete operator deallocates memory from heap.
  • Pointer to object is not destroyed, value or memory block pointed by pointer is destroyed.

deleting memory dynamically allocated by malloc

#include <bits/stdc++.h>

using namespace std;

  

int main()

{

    // Dynamic memory allocated by using malloc

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

  

    delete ptr2;

  

    cout << "ptr2 deleted successfully";

  

    return 0;

}

Kindly revert for any queries

Thanks.

Add a comment
Know the answer?
Add Answer to:
A.2 5 pts - VWhat is a dangling/stale pointer? And please give code example [Yes ]No...
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
  • A.5 5 pts int cs 340 int *pointer &cs; Please code a constant pointer which points...

    A.5 5 pts int cs 340 int *pointer &cs; Please code a constant pointer which points to pointer Please code an Ivalue reference to reference *pointer Please code an rvalue reference to reference *pointer Please use what you created, if legal, to change the value of cs to 413. Provide your code or reasoning: A.5 5 pts int cs 340 int *pointer &cs; Please code a constant pointer which points to pointer Please code an Ivalue reference to reference *pointer...

  • in C++ 1. Answer the following questions: a) 2 pts] Explain why it is important to...

    in C++ 1. Answer the following questions: a) 2 pts] Explain why it is important to indent code. What happens if code is not indented? b) 2 pts) Explain how you can prevent the unintended use of garbage values? c) (2 pts] Give one reason why you should use the keyword const when dealing with constant values. Make sure to explain what could happen if you fail to use this keyword. d) 2 pts) Give an example where simply declaring...

  • Mathematics - Coding Theory Please give an example (or say why the example can not exist)...

    Mathematics - Coding Theory Please give an example (or say why the example can not exist) of a code in which contains a codeword of odd weight. Please explain why you say this. The trivial code and the repetition code does not count! C = (x + 1)(x)) R4

  • Please write below code in C++ using Visual Studio. Write program that uses a class template...

    Please write below code in C++ using Visual Studio. Write program that uses a class template to create a set of items. The program should: 1. add items to the set (there shouldn't be any duplicates) • Example: if your codes is adding three integers, 10, 5, 10, then your program will add only two values 10 and 5 • Hint: Use vectors and vector functions to store the set of items 2. Get the number of items in the...

  • 1. What is Critical thinking? Please give an example of Critical thinking. 2. What is Clinical...

    1. What is Critical thinking? Please give an example of Critical thinking. 2. What is Clinical reasoning? Please give an example of Clinical reasoning. 3. Differentiate Critical thinking and Clinical reasoning. 4. Why does a nurse must have both Critical thinking and Clinical reasoning? Explain 5. What are the techniques of critical thinking? What are the components of Clinical reasoning? 6. Why is Critical thinking and Clinical reasoning necessary in Nursing Process?

  • (5 pts each) Give example of an explicit function f in each of the following category...

    (5 pts each) Give example of an explicit function f in each of the following category with properly written domain D and range R such that (a) There exists a subset S of D with f-'[F(S)] + S (b) There exists a subset T of R with f[f-(T)] #T (11) (3+3+ 5 + 5 + 2) Define functional completeness. Show that x + y = (x + y) + (x + y), x · y = (x + x) +...

  • Your assignment is to:  (10 pts) Give an example of unethical business behavior (describe what...

    Your assignment is to:  (10 pts) Give an example of unethical business behavior (describe what is going on in one paragraph). You can choose from the list in Ghillyer, p.25, if you need ideas, or choose any example from the news, or invent your own hypothetical scenario of unethical business behavior.  (30 pts) LIST at least 3 stakeholders and explain how each is negatively impacted by the unethical behavior. See Ghillyer, p. 25, Figure 2.2, for an example...

  • (65 pts) Nuclear Fusiorn (a) What is nuclear fusion ? (b) Give an example (different than...

    (65 pts) Nuclear Fusiorn (a) What is nuclear fusion ? (b) Give an example (different than those in this problem below) of a nuclear fusion reaction (c) When a tritium nucleon and a deuterium nucleon under a fusion reaction they produce a helium aucleus and a neutron. If the masses of the species are the following tritium: 3.01605 amu deuterium: 20140 amu helium: 4.00260 amu neutron: 1.0089665 amu How much energy (in kJ) will be produced per mole of reaction...

  • 12 pts Please upload.cpp file only before the time up. The code must contain your name...

    12 pts Please upload.cpp file only before the time up. The code must contain your name and has proper format. Write a program that prompts a user to enter a line of text (including whitespace). Please use string object NOT e-string, a string library can be included. 1. Convert the input into uppercase and output the string to the screen. 2. Get the first and the last characters then appends the line with "I Love C++" and output the string...

  • Introduction: One of the most important uses of pointers is for dynamic allocation of memory. In...

    Introduction: One of the most important uses of pointers is for dynamic allocation of memory. In C++ there are commands that let the user request a chunk of memory from the operating system, and use this memory to store data. There are also commands to return memory back to the O/S when the program is finished using the data. In this lab, we will explore some of the things that can go wrong when using dynamic memory and discuss how...

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