473,796 Members | 2,460 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
11 2405
In article <ln************ @nuthaus.mib.or g>,
Keith Thompson <ks***@mib.orgw rote:
>"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******@caffei ne.csclub.uwate rloo.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
13779
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 01/01/2004 11:00:00 1 2
3
1625
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: MenuID TabText SubTabID TabOrderID ------- ----------- ----------- -----------
1
2000
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. I've just added new prices to several drop-downs on our javascript-based ordering page. These new prices must have shipping costs added to them at the bottom of the ordering system in a text box called "Shipping", while the existing prices do not...
2
3211
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 allow someone access to SQL Server using the Guest account over the Internet, and i solved such type of tricky questions before in ucertify. I feel i could have done better. Good luck to all who are preparing to take this exam.
0
1183
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 computers and everywhere it works just fine. Everywhere but two machines... _important_ machines... I see there is no other way to sit by the computers and investigate the issue. To do so I need a debugger - it's enought if it can tell me where an...
25
3412
by: PyPK | last post by:
What possible tricky areas/questions could be asked in Python based Technical Interviews?
8
1885
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 *s1="abcd",*s2=NULL; /* From here you call a function copy which has return type void .
2
1375
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 a corresponding number following the names. (Couldn't get ASP.NET to recognize a control array) The question is: How do I get the tbA, tbC, tbS, tbZ, & tbDOB to be required if (AND ONLY IF) tbPN has text? I have tried in the past to get...
14
2047
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 as coords for SIZE^3 different points in a 3d space: Now, instead of point consider Cubes. They each have 6 sides:
9
1725
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 type="text/javascript">
0
9673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10221
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10169
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10003
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6785
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4115
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.