473,385 Members | 1,877 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

tricky question

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
11 2379
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
"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
"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
>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
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
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
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
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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: JZ | last post by:
Oracle 9iR2 I have a table: SQL> select * from test; A B C ------------------- ---------- ---------- 01/01/2004 10:00:00 1 1...
3
by: Martin | last post by:
Dear Group I wonder whether you can push me in a direction on how to design the following statement. I'm looking for a SELECT with some tricky ORDER BY. The database table looks like this: ...
1
by: Tim | last post by:
Hello, I'm extremely puzzled; I cannot figure out what I'm doing wrong. Here's the situation. I would really appreciate any suggestions. I'm modifying a shopping cart in the following way....
2
by: Kennedy_f | last post by:
Most questions of exam 70-228 have a selection of answers that all seem correct, but in reality, the right answer is the one that best solves the question.There a few trick questions like how to...
0
by: Piotr Szukalski | last post by:
Hi! I have a quite tricky question about .NET debugger: do I need to install the whole SDK to make SDK CLR debugger working? The situation is as follows: I have an application deployed to 130...
25
by: PyPK | last post by:
What possible tricky areas/questions could be asked in Python based Technical Interviews?
8
by: pras.vaidya | last post by:
Hi , below given question was asked to me during an interview and i figured it out little tricky . It would be a great help if anyone could solve it. Code : - main() { char...
2
by: Mark Sandfox | last post by:
I have a tricky control validation issue. I have probably designed this the wrong way but here is what I have. I have 6 TextBoxes; tbPN, tbA, tbC, tbS, tbZ, tbDOB and there are 20 of each with...
14
by: felixnielsen | last post by:
Consider this 3d vector: const SIZE = 'some_size'; std::vector<std::vector<std::vector<char> > >GRID(SIZE, std::vector<std::vector<char> >(SIZE, std::vector<char>(SIZE))); It can be viewed...
9
by: howachen | last post by:
Hi, I have one very simple tricky question which is quite interesting, I would like to share with all of you here... //======================================= <script...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.