473,769 Members | 4,846 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

exit, atexit and scope

I would like to know if the use of the pointer ref in the function
cleanup() below is valid or if something in the norm prevents this
kind of cross-reference during exit(). I haven't seen anything in the
norm against this, I mean an as-if rule saying "atexit registered
functions are executed as-if they were called from main", making val
out of scope at this point.

a+, ld.

#include <stdio.h>
#include <stdlib.h>

int *ref;

void cleanup(void) {
if (ref) fprintf(stderr, "val = %d\n", *ref);
}

void test(void) {
int val = 12;
ref = &val;
exit(EXIT_FAILU RE);
}

int main(void) {
atexit(cleanup) ;
test();
return 0;
}
Nov 15 '07 #1
16 3612
Laurent Deniau <La************ @gmail.comwrite s:
I would like to know if the use of the pointer ref in the function
cleanup() below is valid or if something in the norm prevents this
kind of cross-reference during exit(). I haven't seen anything in the
norm against this, I mean an as-if rule saying "atexit registered
functions are executed as-if they were called from main", making val
out of scope at this point.
You don't mean scope. You are talking about the lifetime of the
variable 'val'. The name 'val' is out of scope during the whole time
exit is doing its job, but the variable, now pointed to by 'ref', still
exists. I think it is safe.
#include <stdio.h>
#include <stdlib.h>

int *ref;

void cleanup(void) {
if (ref) fprintf(stderr, "val = %d\n", *ref);
}

void test(void) {
int val = 12;
ref = &val;
exit(EXIT_FAILU RE);
}

int main(void) {
atexit(cleanup) ;
test();
return 0;
}
--
Ben.
Nov 15 '07 #2
Ben Bacarisse <be********@bsb .me.ukwrites:
Laurent Deniau <La************ @gmail.comwrite s:
>I would like to know if the use of the pointer ref in the function
cleanup() below is valid or if something in the norm prevents this
kind of cross-reference during exit(). I haven't seen anything in the
norm against this, I mean an as-if rule saying "atexit registered
functions are executed as-if they were called from main", making val
out of scope at this point.

You don't mean scope. You are talking about the lifetime of the
variable 'val'. The name 'val' is out of scope during the whole time
exit is doing its job, but the variable, now pointed to by 'ref', still
exists. I think it is safe.
>#include <stdio.h>
#include <stdlib.h>

int *ref;

void cleanup(void) {
if (ref) fprintf(stderr, "val = %d\n", *ref);
}

void test(void) {
int val = 12;
ref = &val;
exit(EXIT_FAILU RE);
}
I dont think it is safe.
>>
int main(void) {
atexit(cleanup) ;
test();
test() runs but "int val" is effectively "gone" when the function exits.
> return 0;
}
At this point cleanup is called.

Surely for this to be OK val would need to be a static?

But, I welcome correction.
Nov 15 '07 #3
Laurent Deniau wrote:
I would like to know if the use of the pointer ref in the function
cleanup() below is valid or if something in the norm prevents this
kind of cross-reference during exit(). I haven't seen anything in the
norm against this, I mean an as-if rule saying "atexit registered
functions are executed as-if they were called from main", making val
out of scope at this point.

a+, ld.

#include <stdio.h>
#include <stdlib.h>

int *ref;

void cleanup(void) {
if (ref) fprintf(stderr, "val = %d\n", *ref);
This test doesn't make sense if it is at all possible that ref is
uninitialised. In this program, it's fine, but consider initialising ref
to NULL at the start of main().
}

void test(void) {
int val = 12;
ref = &val;
exit(EXIT_FAILU RE);
}

int main(void) {
atexit(cleanup) ;
test();
return 0;
}

--
Philip Potter pgp <atdoc.ic.ac. uk
Nov 15 '07 #4
Richard <rg****@gmail.c omwrites:
Ben Bacarisse <be********@bsb .me.ukwrites:
>Laurent Deniau <La************ @gmail.comwrite s:
>>I would like to know if the use of the pointer ref in the function
cleanup() below is valid
<snip>
> I think it is safe.
>>#include <stdio.h>
#include <stdlib.h>

int *ref;

void cleanup(void) {
if (ref) fprintf(stderr, "val = %d\n", *ref);
}

void test(void) {
int val = 12;
ref = &val;
exit(EXIT_FAILU RE);
}

I dont think it is safe.
>>>
int main(void) {
atexit(cleanup) ;
test();

test() runs but "int val" is effectively "gone" when the function
exits.
but exit is called before test returns (in this context using the word
'exit' for a function return in going to be a problem!). From my
reading of the standard, the actions of exit -- i.e. the calling of
all registered atexit functions -- take place like a normal function
call. test() would return only after exit is done. I say "would"
because this can happen only when exit returns to its caller -- and
that does not happen (7.20.4.3 p6).
>
>> return 0;
}

At this point cleanup is called.
No. You missed the call of exit in test().

--
Ben.
Nov 15 '07 #5
Ben Bacarisse <be********@bsb .me.ukwrites:
No. You missed the call of exit in test().
Quite correct!

Is it "defined" that val is still there in this case?
Nov 15 '07 #6
Richard <rg****@gmail.c omwrites:
Ben Bacarisse <be********@bsb .me.ukwrites:
>No. You missed the call of exit in test().

Quite correct!

Is it "defined" that val is still there in this case?
I can't see why not. If exit() were my_func() and my_func called the
same functions that exit() does it certainly would be. I can't see any
special case text that makes what exit() does special enough to
invalidate this argument.

--
Ben.
Nov 15 '07 #7
Ben Bacarisse <be********@bsb .me.ukwrites:
Richard <rg****@gmail.c omwrites:
>Ben Bacarisse <be********@bsb .me.ukwrites:
>>No. You missed the call of exit in test().

Quite correct!

Is it "defined" that val is still there in this case?

I can't see why not. If exit() were my_func() and my_func called the
same functions that exit() does it certainly would be. I can't see any
special case text that makes what exit() does special enough to
invalidate this argument.
Putting it that way I agree. I kind of had it in my mind that calling
exit() was a bit more severe than my_function(). But thinking about it
atoexit would be pretty useless if everything at global scope at least
wasn't available, so why should the stack(!?) for test() have been
folded. Interesting question though.

Nov 15 '07 #8
Philip Potter wrote, On 15/11/07 15:06:
Laurent Deniau wrote:
>I would like to know if the use of the pointer ref in the function
cleanup() below is valid or if something in the norm prevents this
kind of cross-reference during exit(). I haven't seen anything in the
norm against this, I mean an as-if rule saying "atexit registered
functions are executed as-if they were called from main", making val
out of scope at this point.

a+, ld.

#include <stdio.h>
#include <stdlib.h>

int *ref;

void cleanup(void) {
if (ref) fprintf(stderr, "val = %d\n", *ref);

This test doesn't make sense if it is at all possible that ref is
uninitialised. In this program, it's fine, but consider initialising ref
to NULL at the start of main().
<snip>

Not required. Any variable defined at file scope or defined as static in
block scope is initialised to an appropriate zero if no initialisation
is provided, so ref is initialised to a null pointer.
--
Flash Gordon
Nov 15 '07 #9
Ben Bacarisse wrote On 11/15/07 11:36,:
Richard <rg****@gmail.c omwrites:

>>Ben Bacarisse <be********@bsb .me.ukwrites:

>>>No. You missed the call of exit in test().

Quite correct!

Is it "defined" that val is still there in this case?


I can't see why not. If exit() were my_func() and my_func called the
same functions that exit() does it certainly would be. I can't see any
special case text that makes what exit() does special enough to
invalidate this argument.
A possible implementation of exit():

#include <stdlib.h>
#include <setjmp.h>
#include "_implementatio n_magic.h"
void exit(int code) {
_exit_status = code;
longjmp (_pre_main_jmpb uf, 1);
}

.... and Boom! all the program's `auto' variables vanish.
Any atexit-registered functions had better not try to
use them ...

I don't know off-hand whether any implementations '
exit() functions work this way, but I don't see anything
in the Standard that would forbid it. So I, for one,
will not make use of possibly-deceased `auto' variables
in my atexit() functions, and I'd urge the same course
on others.

--
Er*********@sun .com
Nov 15 '07 #10

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

Similar topics

3
1888
by: Maxwell Hammer | last post by:
An application I am developing executes many threads and then has a "monitor" part that waits for certain events. One of these events causes the application to have to shutdown. On shutdown the monitor part notifies the threads of a shutdown, and the threads must cleanup and exit. When all threads have exited the monitor itself exits. The traceback below occured on a shutdown. At the time there was only one thread running, however the...
13
4881
by: Siemel Naran | last post by:
Hi. I posted this question to comp.lang.c++, but am rephrasing it a bit from what I learned and posting to comp.lang.c++.moderated for more insight. So how do I solve my problem? I want it so that when the user presses Ctrl-C or eds the task from task manager (or the kill command in UNIX) then the system should shutdown gracefully. This means it should call the destructors of all objects, which means freeing dynamic memory, closing...
8
2839
by: JKop | last post by:
Let's say that when your program ends (no matter how) that you want a certain block of code to be executed at the end. Here's the code: std::cout << "The program will now end.\n"; std::system("PAUSE"); I've looked up "exit", "atexit" and "abort". Up until this point I simply would've done:
17
8064
by: jwaixs | last post by:
Hello, I was wondering, what's the difference between exit and return in the main() function? For me they both look the same, or aren't they? And if they aren't, which should I use in which situation? Also I was wondering if it whould be wise to combine the standard status with return. exp:
20
3613
by: lovecreatesbeauty | last post by:
Hello experts, Is the following code snippet legal? If it is, how can exit() do the keyword return a favor and give a return value to the main function? Can a function call (or only this exit(n)) statement provide both function call and return features of the C programming language? /* headers omitted */ int main (void)
19
11500
by: ern | last post by:
Right now I'm using exit(0) to terminate my program. My program is a console .exe application. After the "exit(0)" line of code is encountered, the console application waits for an enter press, before terminating. I want it to terminate completely without having to press enter manually. Anybody know what I might be missing here ?
3
3328
by: carl.dhalluin | last post by:
Hi, I am playing with the atexit module but I don't find a way to see the difference between a script calling sys.exit(<returncode>) and the interpreting arriving at the end of the source code file. This has a semantic difference for my applications. Is there a way to determine in an exithandler (that is registered using atexit.register)
9
11579
by: titanandrews | last post by:
Hi All, Is there any way to catch the exit code from someone calling exit(status) and check the value before the program terminates? Just for an idea, the code I'm thinking of is something like this: void exithandler() { DWORD exitCode = 0;
11
2848
by: Rahul | last post by:
Hi Everyone, I have seen code in different styles like main(argc,argv) int argc; char **argv; { if(argc != 2) exit(1); else exit(0);
0
9589
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
9423
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
10047
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
9995
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
9863
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
7410
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
5304
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...
2
3563
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
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.