int *p=(int *) 1000




Comment hidden because of low score. Click to expand.
0
of 0 vote

The memory location numbered 1000 is likely not writeable, which would explain the segfault.

- eugene.yarovoi May 10, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

That address 1000(virtual address) is not part of your process.. When you dereference the pointer, the cpu issues 1000 as the address to be used.. As this is not there in your process's page table, it will result in a fault. !

- Pradeep Kashyap May 13, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Well I kinda understood you question here buddy. So lemme put my say as follow.

Question can be rephrased as

1> How do you ask a pointer to point to a specific memory address that you will provide it from your side and NOT system generated (Assuming your given address is valid address).
2> Now once you have your pointer pointed to that address, How do you put the content on that address.

Explanation is here :-

You are right, the way to do it is

int *p=(int *) )0X1000 ; (Assuming Ox1000 is valid System Address)

Now your pointer p is pointing to address location 0x1000 .

In this case if you do

cout<<p<<endl;

//I WILL GUARANTEE YOU YOUR PROGRAM NEVER CRASH and output will be the memory address that we assigned and that is 0X1000 in our case.

However here is the real question now .

Like system generated pointer variable ..

int *myPointer = new int();
* myPointer = 55 ; // This is how we set the content to the pointer .

but how do we set it in our case ?

Lets see how many people can answer this clearly enough .

- hprem991 May 13, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think you meant that you guarantee that it won't crash if they do cout<< p <<endl. If the pointer is dereferenced (as in cout<< *p <<endl), it might well crash.

To set the value, you'd set it in the same way you set any other pointer, e.g. *myPtr = 55. The only problem is that depending on what memory address you're pointing to, it might not be legal to write that address, in which case you'd get a segfault. That has nothing to do with the fact that the pointer was cast from int, and everything to do with the address in the pointer.

- eugene.yarovoi May 13, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Eugene :- Right Man!! I indeed it was meant for that but got a typo, Anyways quiet interesting that you caught that. After a long time Hope you are doing good.

Now the second part of which I haven't completed my above comment I guess you made it clear enough. Returned memory address is actually a valid address for reading and not writable ( until you are in jurisdiction of that memory being addressed ). Which means ,

int *myPointer = new int();
* myPointer = 55 ;

Always Passes &

int *myPointer = (int *)0x12345;
* myPointer = 55 ;

Fails !!!

- hprem991 May 14, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

When you do int* myPointer = new int(); you always get back a pointer to valid, writeable memory assuming the allocation itself doesn't fail. If you just cast some arbitrary address to a pointer, the memory address could be not allocated to the process or not writeable.

Another thing that you have to consider with an address like 0x12345 is that it's not aligned to a 4-byte boundary. On some architectures integer writes must be aligned to 4-byte boundaries, so this could be another reason it fails. When you do new int(), the returned memory is always correctly aligned.

- eugene.yarovoi May 14, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

In this int * means it point to integer address, and 1000 is address.
finally p =1000
and *p = content in memory location address of 1000

- Aniket October 05, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

AOL support +1-800-893-9752
When AOL user is unable to get the email or unable to log-in to the AOL account, then it is best to get the quick help by dialup toll-free # +1-800-893-9752
contact aol member services, aol support phone number usa, aol email support phone number, aol email tech support number, aol email customer service number, aol email customer support, aol customer support telephone number, aol technical support contact number, aol com customer service phone number, download aol mail to computer, Aol email support phone number, aol gold desktop download, aol mail tech support number, aol mail hotline number, aol mail toll free number, contact aol mail support number, aol customer service number, aol support phone number, aol support number, aol mail repair

- ap2ankur May 24, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I have another problem, there is a code:

typedef struct __st_callback{
	...
	...
	member n
	...
	...
}*P_CALLBACK;

#define ADDRESS 0x2000
#define  ((P_CALLBACK)*(unsigned int*)ADDRESS)

What is this?

- Jun October 18, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I got answer in stackoverfolw
title is ((PtrToStruct)*(unsigned int*)ADDRESS)

- jun.chen@dediprog.com October 18, 2018 | Flag Reply




Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More