What is wrong in this program?

I am seeing memory access violation when I run program below. The error is "0xC0000005: Access violation writing location 0x00000000."

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "iostream"
using namespace std;

void set5(int *ptr)
{
	*ptr = 5;
}

void main()
{
	int *x = (int*) (0);
	set5(x);
	cout << *x << endl;
}


I think the error is on line *ptr=5. Any reason why it is happening?
The main function always has to be a int so change line nine to int main ()
and everything should work fine.

and i see another problem. i did fix it though
here is my code i changed the definition of the variable to a normal int, added a & to the input and changed cout to cout without the *.

it outputed 5 for me.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "iostream"
using namespace std;

void set5(int *ptr)
{
	*ptr = 5;
}

int main()
{
	int x = 0;
	set5(&x);
	cout << x << endl;
	return 0;
}
Last edited on
@supersammy7000: Thanks for fixing this issue. I have a question for you.

You changed the implementation to pass a reference to a pointer value. Does it not accept address of value. In other words, if I pass x, which is address of 0 value, to int *ptr variable is that not acceptable? Is it okay to pass reference as in input to int *ptr variable?
im not to good at this area but what i think your saying is that will x change in the main method. my answer is yes it will what is happening is. it creates a varable x with a address in ram. we tell the program to pass the address by putting a & in front of it so we are passing the address and not just a copy.

so in essence it we pass x address and it changes it in set5 to 5 and return nothing but changes the base value of x.
In your first post,
int *x = (int*) (0);
makes x be a pointer at 0x000(...)0. Your OS does not like that you are trying to use a reserved space in memory like that. (and *x would point to the value at 0x0000...think about what that would be?).
Last edited on
You get a segfault (or that violation error) when you access an invalid or protected memory address.
In your given code you set the pointer x to point to the address 0,this address has a special meaning , it can not be de-referenced (or used), so it gives an error.
This address which is also called NULL is used as a 'points to nothing' mark for pointers and if you try to use it,the program crashes.
funprogrammer wrote:
@supersammy7000: Thanks for fixing this issue. I have a question for you.

You changed the implementation to pass a reference to a pointer value. Does it not accept address of value. In other words, if I pass x, which is address of 0 value, to int *ptr variable is that not acceptable? Is it okay to pass reference as in input to int *ptr variable?

He is not passing a reference there, the ampersand symbol has different uses in different contexts.
1. Declaring a reference.
type &var_name;
2. Taking the address of a variable.This is what he used in that snippet.
1
2
3
int foo = 42;
int *fooptr = &foo;//fooptr is a pointer which stores a memory address of a variable,
//here we set fooptr to the address of foo. 

Read & as 'address of'
3. Bitwise and
1
2
3
int a = 3;//binary : 011
int b = 5;//binary : 101
int c = a & b;//c = 011 & 101 = 001 

Last edited on
Thanks @akn and @supersammy7000. That helped me understand this concept pretty well.
Topic archived. No new replies allowed.