473,785 Members | 2,879 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 #1
11 2404
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*******@gmai l.comwrote in message
news:11******** **************@ n33g2000cwc.goo glegroups.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*********@gm ail.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*)0x80495 b8;
then the behavior of the conversion would be implementation-defined.

--
Keith Thompson (The_Other_Keit h) 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*********@gm ail.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*********@gm ail.comwrote:
>>onkar wrote:
>>Program 1:

#include<stdi o.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_Keit h) 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*******@gmai l.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_Keit h) 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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
13778
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
1998
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
3409
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
2045
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
1723
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
9645
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
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10152
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...
0
9950
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
6740
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
5381
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...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
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
3650
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.