hello.
how can one implement gcc's cleanup attribute for msvc?
how destructors are implemented on top of msvc?
In addition I want the behavior to be identical to the cleanup attribute so atexit is not an option.
hello.
how can one implement gcc's cleanup attribute for msvc?
how destructors are implemented on top of msvc?
In addition I want the behavior to be identical to the cleanup attribute so atexit is not an option.
There are no destructors in C. You are on the wrong forum.
1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
3. Get rid of conio.h and other antiquated DOS crap headers.
4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.
hello.
I'm well aware of it. however in the extensions provided to c by gcc there is a cleanup attribute attribute which may be used to implement destructors on top of it as well as an attribute destructor.
my goal is to be able to implement the cleanup attribute functionality for msvc compiler.
thanks any way.
You would probably get a lot further if you just accepted that C was C and no amount of compiler specific hackery would make it taste like C++.cleanup (cleanup_function)
The cleanup attribute runs a function when the variable goes out of scope. This attribute can only be applied to auto function scope variables; it may not be applied to parameters or variables with static storage duration. The function must take one parameter, a pointer to a type compatible with the variable. The return value of the function (if any) is ignored.
If -fexceptions is enabled, then cleanup_function will be run during the stack unwinding that happens during the processing of the exception. Note that the cleanup attribute does not allow the exception to be caught, only to perform an action. It is undefined what happens if cleanup_function does not return normally.
If you want this behaviour, you're better off writing C++ (or learning to write C properly). Attributes like this only lead to "Oh no, my safety net has gone" when you switch from one compiler to another.
Either stick with GCC, or write portable code to begin with.
You're even more hamstrung with MS, since the Microsoft 'C' compiler will never be updated from C89 standard (we've had C99 and C11 since then).
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
alright. but again is there any mechanism availiable in any of msvc versions that can be used in order to implement the cleanup attribute in c.
I'm not intrested in portablity here, nor in using c++ or gcc.
I definitely agree with you here. I'm personally an advocate of writing portable code, and not using any compiler specific extensions or even pragmas at all, where possible.
It is also usually easier to write portable code, than it is to work out how to map particular compiler-specific attributes or extensions to other compilers. Generally, I only use compiler specific attributes or extensions (and look askance at pragmas) unless I am happy to be locked into one particular compiler for life. Which, generally, I am not.
Never say never, although I agree it probably won't happen for a while.
Herb Sutter, who is lead architect of the Visual Studio development team, happens to also be chair of ISO (or ANSI's) standardisation committee for C++. It's hardly surprising that he might encourage Microsoft compilers to lean towards C++ rather than C.
It's a matter of opinion whether that is a good thing or not (with the dividing line being whether one prefers C or C++). Certainly, Microsoft have gone ahead in leaps and bounds with improving the quality and standard compliance of their C++ compiler offering, although support of C has languished.
There are published suggestions of internal dissent with Microsoft over the lack of support of C. So, it remains to be seen if that will change.
Structured Exception Handling? The __try/__finally keywords can force clean-up before exiting a block of code
Structured Exception Handling Basics - General Programming - Articles - Articles - GameDev.net
I've read the link, however, it doesn't provide the answer for my problem.
for example:
Code:int main() { char *ptr; ptr = malloc(10); myFunc(&ptr); strcpy(ptr,"string"); printf("%s\n",ptr); return 0; }
Assume that myFunc(char **ptr) is a function you can implement as you wish.
But the main code is fixed and you can't change it at all.
here the only way to go is using the cleanup attribute.
the case I've presented is the case I'm trying to solve.
any suggestions?
you would have to do some stack hackery where myFunc saves the return address from the stack, and replaces it with a jump to your cleanup function, where myfunc does what you want then executes a return to the original address. will be tricky to implement in a general way.
The case you've presented here can't be solved.the case I've presented is the case I'm trying to solve
The GCC "cleanup" attribute certainly can't help. The `ptr' variable in `main' would have to be marked with the cleanup attribute; marking that `ptr' variable with the "cleanup" attribute would be changing the code and require a recompile. If you can change the code, you may as well make a proper portable fix.
If you are truly not allowed to change buggy code you'll have to live with a bug. You may be able to bandage over it, but that's probably the absolute best that you can do, and every situation will probably require you to use a different kind of bandage.
That said, let's face the reality here. You've constructed a situation where you believe you must have a feature to write correct code. The "cleanup" attribute is extremely useful, but it isn't required to write correct code.
If you want scoped destruction you are limited to: using C++, using vendor specific extensions, or writing an awful mess of code with `setjmp' and `longjmp'.
Soma
> But the main code is fixed and you can't change it at all.
Mmm - okay....
> here the only way to go is using the cleanup attribute.
But you're already changing the code if you plan to try something like
char __attribute__ ((cleanup (myFunc))) *ptr;
Not to mention actually implementing myFunc() as well.
So why can't you change it to just call free() at the end, like any normal person would?
TBH, it seems like you're searching for some overly elaborate solution that might take months to find, whereas just editing the code to fix the problem in whatever small number of places memory is being leaked (which would take a couple of days).
Remember, you have to FIND these variables which need cleanup to begin with (this will take all the time), and then decide what to do about them. Blindly adding "cleanup" to everything you find would add as many bugs as you remove (like cleaning up a pointer that was actually returned or copied elsewhere). Having found the source of a leak, fixing it is usually pretty trivial - if you need to free it in the SAME function that allocated it.
Leaks that occur because memory is allocated in one function, and (supposedly) freed in another function will not be fixed by this cleanup magic.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Also posted in devshed forum.
tank you for the quick replys.
some further clarifications:
1. when I've written I can't change the code, I only meant I can't change the main function.
2. this is a riddle\question I was asked ,so I must follow the rules.
3. what I seek is a direction for the answer. I already know that the cleanup attribute solves the problem, therefore, my task is to
implement an equivalent for msvc.
4. I can't see how setjmp and longjmp can help since the memory allocated is still being used after the call to myFunc.