By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,804 Members | 1,659 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,804 IT Pros & Developers. It's quick & easy.

tricky question

P: n/a
Program 1:

#include<stdio.h>
int main(void){
int *p;
p=(int *)malloc(sizeof(int));
*p=12;
printf("%d %p\n",*p,p);
return 0;
}

I run the program to get the following output :

12 0x80495b8

now

Program 2:

#include<stdio.h>
int main(void){
int *p;
p=0x80495b8;
printf("%d\n",*p);
return 0;
}

what should be the answer "if ,somehow the Process executing the
program 2 is allowed to access all the memory locations of Process
executing on behalf of Program 1 ".

I think it should be 12 ? correct me if I am wrong //

Thank you,
Onkar

Mar 7 '07 #1
Share this Question
Share on Google+
11 Replies


P: n/a
onkar wrote:
Program 1:

#include<stdio.h>
Where's stdlib.h for malloc. You're invoking undefined behaviour
already.
int main(void){
int *p;
p=(int *)malloc(sizeof(int));
No need to cast the return value of malloc.
*p=12;
printf("%d %p\n",*p,p);
It's better form to cast the argument corresponding to the %p
specifier to type void pointer.
return 0;
}

I run the program to get the following output :

12 0x80495b8

now

Program 2:

#include<stdio.h>
int main(void){
int *p;
p=0x80495b8;
Implementation defined behaviour.
printf("%d\n",*p);
return 0;
}

what should be the answer "if ,somehow the Process executing the
program 2 is allowed to access all the memory locations of Process
executing on behalf of Program 1 ".

I think it should be 12 ? correct me if I am wrong //
Well, you think wrong. Nothing about the second program is guaranteed.
It invokes undefined behaviour.

<OT>
In typical modern operating systems, each process is given it's own
address space, so the address 0x80495b8 of process two need not
correspond with the same address of process one. In fact, they won't
map to the same physical memory. If they did, then either the
operating system is broken or the system doesn't support virtual
memory, (or virtual memory has been disabled).
</OT>

Mar 7 '07 #2

P: n/a
"onkar" <on*******@gmail.comwrote in message
news:11**********************@n33g2000cwc.googlegr oups.com...
Program 1:

#include<stdio.h>
int main(void){
int *p;
p=(int *)malloc(sizeof(int));
*p=12;
printf("%d %p\n",*p,p);
return 0;
}

I run the program to get the following output :

12 0x80495b8

now

Program 2:

#include<stdio.h>
int main(void){
int *p;
p=0x80495b8;
printf("%d\n",*p);
return 0;
}

what should be the answer "if ,somehow the Process executing the
program 2 is allowed to access all the memory locations of Process
executing on behalf of Program 1 ".

I think it should be 12 ? correct me if I am wrong //
It's undefined behavior. On some implementations it may work as you expect,
but on the majority of modern systems the result will be either random,
zero, or a crash (e.g. segfault).

S

--
Stephen Sprunk "Those people who think they know everything
CCIE #3723 are a great annoyance to those of us who do."
K5SSS --Isaac Asimov
--
Posted via a free Usenet account from http://www.teranews.com

Mar 7 '07 #3

P: n/a
"santosh" <sa*********@gmail.comwrites:
onkar wrote:
[...]
>Program 2:

#include<stdio.h>
int main(void){
int *p;
p=0x80495b8;

Implementation defined behaviour.
No, it's a constraint violation.
> printf("%d\n",*p);
return 0;
}
There is no implicit conversion from integers to pointers (other than
the special case of a null pointer constant). (Some compilers may
allow the assignment as an extension, and will *probably* implement an
implicit conversion, but the standard allows other possibilities.)

If the assignment were changed from
p=0x80495b8;
to
p=(int*)0x80495b8;
then the behavior of the conversion would be implementation-defined.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 7 '07 #4

P: n/a
>Program 1:
>
#include<stdio.h>
int main(void){
int *p;
p=(int *)malloc(sizeof(int));
*p=12;
printf("%d %p\n",*p,p);
return 0;
}

I run the program to get the following output :

12 0x80495b8

now

Program 2:

#include<stdio.h>
int main(void){
int *p;
p=0x80495b8;
printf("%d\n",*p);
return 0;
}

what should be the answer "if ,somehow the Process executing the
program 2 is allowed to access all the memory locations of Process
executing on behalf of Program 1 ".
If, somehow, the process executing program 2 is allowed to access
all the memory locations of the process executing on behalf of
program 1, it still doesn't mean that the same byte has the same
address in both programs, even if somehow you get the two programs
running at the same time.

You can get a limited version of this with mmap() available on some
systems. You don't have to map the same segment to the same address
in two different processes.
>I think it should be 12 ? correct me if I am wrong //
You get no guarantees.
Mar 8 '07 #5

P: n/a
On 7 Mar 2007 04:28:21 -0800, "santosh" <sa*********@gmail.comwrote:
>onkar wrote:
>Program 1:

#include<stdio.h>

Where's stdlib.h for malloc. You're invoking undefined behaviour
already.
>int main(void){
int *p;
p=(int *)malloc(sizeof(int));

No need to cast the return value of malloc.
> *p=12;
printf("%d %p\n",*p,p);

It's better form to cast the argument corresponding to the %p
specifier to type void pointer.
Actually, it's mandatory.

Remove del for email
Mar 8 '07 #6

P: n/a
Barry Schwarz <sc******@doezl.netwrites:
On 7 Mar 2007 04:28:21 -0800, "santosh" <sa*********@gmail.comwrote:
>>onkar wrote:
>>Program 1:

#include<stdio.h>

Where's stdlib.h for malloc. You're invoking undefined behaviour
already.
>>int main(void){
int *p;
p=(int *)malloc(sizeof(int));

No need to cast the return value of malloc.
>> *p=12;
printf("%d %p\n",*p,p);

It's better form to cast the argument corresponding to the %p
specifier to type void pointer.

Actually, it's mandatory.
It's mandatory in the sense that failing to do so (unless the pointer
is already of type void* or of a pointer-to-character type) invokes
undefined behavior. But the compiler is under no obligation to tell
you about the error, and on many systems the call without the cast
happens to behave exactly like the proper call with the cast.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 8 '07 #7

P: n/a
On 7 Mar 2007 04:13:16 -0800, "onkar" <on*******@gmail.comwrote:
>Program 1:

#include<stdio.h>
int main(void){
int *p;
p=(int *)malloc(sizeof(int));
This cast prevented the compiler from informing you that you have done
something illegal. Your program invokes undefined behavior.
*p=12;
printf("%d %p\n",*p,p);
There is a mismatch between the %p and the type of the corresponding
argument. More undefined behavior.
return 0;
}

I run the program to get the following output :

12 0x80495b8

now

Program 2:

#include<stdio.h>
int main(void){
int *p;
p=0x80495b8;
Did not your compiler issue a diagnostic for this statement? Why did
you ignore it?
printf("%d\n",*p);
More undefined behavior.

First, you have no idea if 0x80495b8 is within the address
space of your program. Based on your first set of code, I would bet
that it is not..

Second, even if it is, you never initialized it with a value.
Therefore, the value is indeterminate. You are not allowed to
evaluate indeterminate values.
return 0;
}

what should be the answer "if ,somehow the Process executing the
program 2 is allowed to access all the memory locations of Process
executing on behalf of Program 1 ".
For undefined behavior, the answer should always be household voltage
applied to the seat of the programmers chair.

Even if your if could be true, most user systems today use virtual
memory and two processes referring to the same logical address
actually refer to different physical addresses.
>
I think it should be 12 ? correct me if I am wrong //
Undefined behavior is always wrong.
Remove del for email
Mar 8 '07 #8

P: n/a
onkar wrote:
Program 1:

#include<stdio.h>
int main(void){
int *p;
p=(int *)malloc(sizeof(int));
*p=12;
printf("%d %p\n",*p,p);
return 0;
}

I run the program to get the following output :

12 0x80495b8

now

Program 2:

#include<stdio.h>
int main(void){
int *p;
p=0x80495b8;
printf("%d\n",*p);
return 0;
}

what should be the answer "if ,somehow the Process executing the
program 2 is allowed to access all the memory locations of Process
executing on behalf of Program 1 ".

I think it should be 12 ? correct me if I am wrong //
besides the undefined behaviour of assigning an int to an int*
you also forgot to initialise *p to 12. There's no *way* the
printf() could print 12

(ok, it's 1 chance in sizeof(int))
--
Nick Keighley

"If, indeed the subatomic energy in the stars is being freely
used to maintain their great furnaces, it seems to bring a little
nearer to fulfillment our dreams of controlling this latent
power for the well-being of the human race - or for its suicide."
Aurthur S. Eddington "The Internal Constitution of the Stars" 1926

Mar 8 '07 #9

P: n/a
"Nick Keighley" <ni******************@hotmail.comwrites:
onkar wrote:
>Program 1:

#include<stdio.h>
int main(void){
int *p;
p=(int *)malloc(sizeof(int));
*p=12;
printf("%d %p\n",*p,p);
return 0;
}

I run the program to get the following output :

12 0x80495b8

now

Program 2:

#include<stdio.h>
int main(void){
int *p;
p=0x80495b8;
printf("%d\n",*p);
return 0;
}

what should be the answer "if ,somehow the Process executing the
program 2 is allowed to access all the memory locations of Process
executing on behalf of Program 1 ".

I think it should be 12 ? correct me if I am wrong //

besides the undefined behaviour of assigning an int to an int*
you also forgot to initialise *p to 12. There's no *way* the
printf() could print 12
I think the idea was that Program 2 would run immediately after
Program 1, and would occupy the same memory space. In reality, that's
unlikely.
(ok, it's 1 chance in sizeof(int))
I think you mean something like one chance in UINT_MAX+1.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 8 '07 #10

P: n/a
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.orgwrote:
>"Nick Keighley" <ni******************@hotmail.comwrites:
>besides the undefined behaviour of assigning an int to an int*
you also forgot to initialise *p to 12. There's no *way* the
printf() could print 12
>(ok, it's 1 chance in sizeof(int))

I think you mean something like one chance in UINT_MAX+1.
Not quite that, either.

There is a nonzero chance that the program won't be allowed to access
the memory that p is pointing at.

There's also a good chance that the bit pattern it's pointing at won't
have a uniform probability distribution; for example, many OSs zero-fill
memory before giving it to a program, and if the memory has been used
already in the program's run (f'rexample, by loading shared libraries
or initializing the runtime library) then the values left behind by that
use will probably follow fairly predictable patterns.

Given that the pointer can be dereferenced without causing a crash
and that the bit pattern follows a uniform probability distribution,
the probability that the value it points at will be 12 is (number of
representations of the value 12 for type int) chances in (two the power
of CHAR_BIT*sizeof(int)).

If there are no padding bits in int (so exactly one possible bit
pattern represents the value 12) or in unsigned int (so UINT_MAX+1 is
the number of possible bit patterns), then given accessibility of memory
and uniformly distributed patterns the likelihood of getting a 12 does
reduce to one chance in UINT_MAX+1, but it's not hard to construct a
case where it won't.
dave

--
Dave Vandervies dj******@csclub.uwaterloo.ca
Two glaring problems and one subtle error is a better-than-average
outcome in these parts.
--Eric Sosman in comp.lang.c
Mar 8 '07 #11

P: n/a
On Thu, 8 Mar 2007 18:52:54 +0000 (UTC),
dj******@caffeine.csclub.uwaterloo.ca (Dave Vandervies) wrote:
>If there are no padding bits in int (so exactly one possible bit
pattern represents the value 12) or in unsigned int (so UINT_MAX+1 is
the number of possible bit patterns), then given accessibility of memory
and uniformly distributed patterns the likelihood of getting a 12 does
reduce to one chance in UINT_MAX+1, but it's not hard to construct a
case where it won't.
I used to tell our programmers that a chance in a million means the
computer will do it once a second. That was back when computers were
slow :-)

--
Al Balmer
Sun City, AZ
Mar 8 '07 #12

This discussion thread is closed

Replies have been disabled for this discussion.