+ 2
Why the program is crushing !!??
#include<iostream> using namespace std; int main() { int *ptr; *ptr=5; cout<<*ptr; return 0; } The programming is crushing when ran in code::Blocks 16.01 it's an integer pointer which takes 4 bytes of memory. the value "5" should have got assigned in it and give output . Rather , the program is chushing.
4 Respostas
+ 4
int* ptr = new int;
ptr will point to a rubbish adress without this assignment. The new keyword is there to actually say: I want memory for a new variable. And it returns the new variables adress.
Note that you must
delete ptr;
by hand to return its underlying memory.
ptr itself is not deleted, it just points to a deleted variable.
int a;
is different. a doesn't need new and it's deleted when it goes out of {scope}.
Use scope deletion whereever possible. It's faster and safer.
+ 3
pointers should hold the address of other variables.
try this:
int main(){
int *ptr, a;
a = 5;
ptr = &a;
cout << *ptr;
return 0;
}
+ 2
How does a program crush? And what is it crushing?
0
By program crush , i mean runtime error