473,830 Members | 2,085 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

stack

jt
hello everyone..

int fun()
{
/* anything */
}
int main(void)
{
fun();
}

in this program, when the control is transferred to function fun()..
the return address is pushed to the stack..
Is it possible to access this value..??
Aug 3 '08
62 2896
Chris Dollin wrote:
Nick Keighley wrote:
>On 4 Aug, 22:30, CBFalconer <cbfalco...@yah oo.comwrote:
>>jt wrote:
>int fun() {
/* anything */
}
int main(void) {
fun();
}

in this program, when the control is transferred to function
fun().. the return address is pushed to the stack..
Is it possible to access this value..??

In a machine that can execute the standard C language, there is no
need for a stack. This makes it hard to access a return address
(of unknown form) stored in a non-existant stack.

but the information must be stored *somewhere* otherwise
the implementation wouldn't be able to return from the function.
Admittedly the implementaion is under no obligation to make
it available to the user.

In the program above, if the compiler chose to inline `fun`, the
"return address" would be "stored" by the implicit fall-through of
one instruction to the next [1].

As we have discussed before, not all C programs need an explicit
run-time stack data-structure.
At least programs making use of recursion do, as far as I can see, and I
doubt that any sane implementation is going to use different strategies
for both these cases.
Of those that do, that structure
need not correspond to the local hardware's notion of "stack",
if it has one. Even if it does, the implementation is under no
requirement of the C standard to expose that stack to C programs.

And even if it /does/, mere ability should not elicit reckless
employment. One can, after all, clean one's teeth with a
screwdriver.

[1] On an implementation that uses "instructio ns", of course.
Aug 5 '08 #31
"Nick Keighley" <ni************ ******@hotmail. comwrote in message
news:57******** *************** ***********@a1g 2000hsb.googleg roups.com...
On 4 Aug, 23:25, Harald van D©¦k <true...@gmail. comwrote:
>On Mon, 04 Aug 2008 17:42:31 -0400, CBFalconer wrote:
Harold Aptroot wrote:
"MisterE" <Mist...@nimga. comwrote in message
>>>Is it possible to access [the return address] value..??
>>No.
>Of course it is. The OP did not request a standard compliant way to
do
it after all
On this newsgroup, there are no non-standard compliant ways to do
anything. Anything of that nature is off=topic,

Please don't use topicality as an excuse or defence for lies.

its not a lie. It's a fact. Your statement is incorrect. There
*is* no standard way to do this and since the topic of this NG
is standard C there is no way to do it that is topical to this
NG. Though I must admit I'd have suggested trying another NG.
So suppose someone asks here how to make coffee, we just tell him it's
completely impossible because the C standard has nothing to say on it?
>
>I don't know
if MisterE is aware of the existence of any non-portable methods of
accessing a function call's return address, but you are.
Aug 5 '08 #32

"jt" <ka**********@g mail.comwrote in message
news:38******** *************** ***********@v1g 2000pra.googleg roups.com...
hello everyone..

int fun()
{
/* anything */
}
int main(void)
{
fun();
}

in this program, when the control is transferred to function fun()..
the return address is pushed to the stack..
Is it possible to access this value..??
No problem, I thought, with a few lines of asm. But when I actually tried
it, it was quite difficult to pick up the return address.

On my machine, it depends on how many bytes of local variables there are in
a function. So this would require compiler help, or you have to tailor the
asm code to each function, and have it break as soon you change any
variables.

So whatever it is you're trying to do, best to think of a different way...

--
Bartc

Aug 5 '08 #33
Harold Aptroot wrote:
"Nick Keighley" <ni************ ******@hotmail. comwrote in message
news:57******** *************** ***********@a1g 2000hsb.googleg roups.com...
>On 4 Aug, 23:25, Harald van D?k <true...@gmail. comwrote:
>>On Mon, 04 Aug 2008 17:42:31 -0400, CBFalconer wrote:
Harold Aptroot wrote:
"MisterE" <Mist...@nimga. comwrote in message
>>>>Is it possible to access [the return address] value..??

No.

Of course it is. The OP did not request a standard compliant way
to do
it after all

On this newsgroup, there are no non-standard compliant ways to do
anything. Anything of that nature is off=topic,

Please don't use topicality as an excuse or defence for lies.

its not a lie. It's a fact. Your statement is incorrect. There
*is* no standard way to do this and since the topic of this NG
is standard C there is no way to do it that is topical to this
NG. Though I must admit I'd have suggested trying another NG.

So suppose someone asks here how to make coffee, we just tell him it's
completely impossible because the C standard has nothing to say on it?
No. We tell him that it's not possible to make coffee using *only*
Standard C, that system specific extensions will be necessary, and to
ask for more information in a group which deals with the system or
implementation the poster is using.

Aug 5 '08 #34
Harold Aptroot said:

<snip>
So suppose someone asks here how to make coffee, we just tell him it's
completely impossible because the C standard has nothing to say on it?
Forget better mousetraps. If you can demonstrate how to make coffee in ISO
C, the world will build a six-lane motorway to your door.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Aug 5 '08 #35
Bartc wrote:
>
"jt" <ka**********@g mail.comwrote in message
news:38******** *************** ***********@v1g 2000pra.googleg roups.com...
>hello everyone..

int fun()
{
/* anything */
}
int main(void)
{
fun();
}

in this program, when the control is transferred to function fun()..
the return address is pushed to the stack..
Is it possible to access this value..??

No problem, I thought, with a few lines of asm. But when I actually
tried it, it was quite difficult to pick up the return address.

On my machine, it depends on how many bytes of local variables there
are in a function. So this would require compiler help, or you have to
tailor the asm code to each function, and have it break as soon you
change any variables.

So whatever it is you're trying to do, best to think of a different
way...
I strongly suspect that all the compilers that the OP is likely to care
about provide a built-in for this purpose.

Aug 5 '08 #36
"santosh" <sa*********@gm ail.comwrote in message
news:g7******** **@registered.m otzarella.org.. .
Harold Aptroot wrote:
>"Nick Keighley" <ni************ ******@hotmail. comwrote in message
news:57******** *************** ***********@a1g 2000hsb.googleg roups.com...
>>On 4 Aug, 23:25, Harald van D?k <true...@gmail. comwrote:
On Mon, 04 Aug 2008 17:42:31 -0400, CBFalconer wrote:
Harold Aptroot wrote:
"MisterE" <Mist...@nimga. comwrote in message

>Is it possible to access [the return address] value..??

No.

Of course it is. The OP did not request a standard compliant way
to do
it after all

On this newsgroup, there are no non-standard compliant ways to do
anything. Anything of that nature is off=topic,

Please don't use topicality as an excuse or defence for lies.

its not a lie. It's a fact. Your statement is incorrect. There
*is* no standard way to do this and since the topic of this NG
is standard C there is no way to do it that is topical to this
NG. Though I must admit I'd have suggested trying another NG.

So suppose someone asks here how to make coffee, we just tell him it's
completely impossible because the C standard has nothing to say on it?

No. We tell him that it's not possible to make coffee using *only*
Standard C, that system specific extensions will be necessary, and to
ask for more information in a group which deals with the system or
implementation the poster is using.
See? Now that's different than MisterE's plain "no"

Aug 5 '08 #37
Harold Aptroot wrote:
"santosh" <sa*********@gm ail.comwrote in message
news:g7******** **@registered.m otzarella.org.. .
>Harold Aptroot wrote:
>>>>"MisterE" <Mist...@nimga. comwrote in message

>>Is it possible to access [the return address] value..??
>
>No.
>>So suppose someone asks here how to make coffee, we just tell him
it's completely impossible because the C standard has nothing to say
on it?

No. We tell him that it's not possible to make coffee using *only*
Standard C, that system specific extensions will be necessary, and to
ask for more information in a group which deals with the system or
implementati on the poster is using.

See? Now that's different than MisterE's plain "no"
If "MisterE" meant his "No" as specifically within the context of ISO C,
then he is factually correct, though a more verbose, explanatory
response would've perhaps been better, but many such responses were
already provided to the OP by the time MisterE's post showed up on my
newsserver.

Aug 5 '08 #38
santosh wrote:
Chris Dollin wrote:
>As we have discussed before, not all C programs need an explicit
run-time stack data-structure.

At least programs making use of recursion do, as far as I can see,
Not all programs that make use of recursion need an explicit run-time
stack, either. But yes, programs (or parts thereof) that do non-tail
recursion will need /something/ like a run-time stack.
and I
doubt that any sane implementation is going to use different strategies
for both these cases.
Whole-program compilation might well choose to, if that was more
efficient. Suppose one were making a serious attempt to compile
as-Standard-as-possible C programs for the 6502, where stack depth
/really/ matters. Or the PDP8, which has the added delightful
feature that the subroutine return address gets planted /at the
target of the call instruction/.

I don't know if today's embedded processors are equally jabberwock-laden,
(except that the ARM isn't), but I'd lay odds that on at least some of
them, if essential recursion is allowed at all, the compiler will
treat recursive (or re-entrant) functions differently from non-R
functions.

--
'It changed the future .. and it changed us.' /Babylon 5/

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

Aug 5 '08 #39
Chris Dollin wrote:
I don't know if today's embedded processors are equally jabberwock-laden,
(except that the ARM isn't), but I'd lay odds that on at least some of
them, if essential recursion is allowed at all, the compiler will
treat recursive (or re-entrant) functions differently from non-R
functions.
For "will treat" safer to read "will treat, or can be told to treat".

--
'It changed the future .. and it changed us.' /Babylon 5/

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

Aug 5 '08 #40

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

Similar topics

15
7750
by: Andrew | last post by:
Last night I was reading about implementing my own stack. The example given pushes items on and off the stack at the start and end of each procedure (ie. in a std module). What's not so clear is how this would work with class objects. In this case do you have to push the object on the stack at the start of every public procedure etc. in the class and pop it off at the end? I can't see how else you can know which object is active - or...
14
30107
by: Kevin Grigorenko | last post by:
Hello, I couldn't find an obvious answer to this in the FAQ. My basic question, is: Is there any difference in allocating on the heap versus the stack? If heap or stack implementation is not part of the standard, then just disregard this question. Here's some questions I'm confused about, and if you can add anything else, please do so! Is the stack limited for each program?
4
2626
by: Chris Mabee | last post by:
Hello all, and Merry Christmas, I'm having a problem understanding an example of an array based implementation of a stack in a textbook of mine. The code in question is written below. The syntax is directly as in the book, except for where I added the comments at the lines I wanted to refer to or to skip sections of code. template <class Element_Type> class Stack {
4
3637
by: anonymous | last post by:
Thanks your reply. The article I read is from www.hakin9.org/en/attachments/stackoverflow_en.pdf. And you're right. I don't know it very clearly. And that's why I want to understand it; for it's useful to help me to solve some basic problem which I may not perceive before. I appreciate your help, sincerely.
8
2101
by: LedZep | last post by:
What up everyone, I have to write a program that uses a stack to determine whether a string is a palindrome (a string that is spelled identically backward and forward). The program has to ignore spaces, case sensitivity and punctuation. I need three text boxes. The first will be for input of the string. The second will display the string when the "check" button is clicked and the result of the analysis (string is a palindrome or is...
4
2152
by: alisaee | last post by:
plz check what i have made wrong what is requierd her is to creat class queue and class stack and run the push,pop operation . #include<iostream.h> #include<conio.h> #include<stdio.h> class stack { public:
16
4454
by: sarathy | last post by:
Hi all, I need a few clarifications regarding memory allocaion in C++. I apologize for the lengthy explanation. 1. In C++, Objects are allocated in heap. What does heap refer to? Is it an area in RAM/Memory or does it refer to a data structure being used for storing objects. 2. In C++, functions and its local variables go in stack. If local variables that are primitives go in stack, it is OK. But what
24
6593
by: John | last post by:
I know this is a very fundamental question. I am still quite confused if the program call stack stack should always grows upwards from the bottom, or the opposite, or doesn't matter?? That means the stack pointer should go upwards when there are "push" operations, and stack pointer should go downards when there are "pop" operations?? If this is the case, the address should go upwards (increasing) or downards (decreasing) then? i.e....
1
4224
by: alfie27 | last post by:
I currently have a working program that is a stack that stores integers. Now i have to convert it to store strings instead of integers. I have been working on this for hours and just keep getting errors of all kinds. I have decided to start from scratch. Any suggestions someone can give me would be greatly appreciated!! Here is the current code: #include <iostream> using std::cout; using std::cin; #include <cstring> using std::strcpy;
11
1788
by: tom | last post by:
Hi! Im new to Python and doing exercise found from internet. It is supposed to evaluate expression given with postfix operator using Stack() class. class Stack: def __init__(self): self.items = def push(self, item): self.items.append(item)
0
9642
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
10777
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10493
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
10526
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
10206
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...
1
7747
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6951
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();...
2
3960
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3076
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.