Exists try catch or similliar for C ?
I have a big problem with strings, and I need continue executing the program before a exception in string conversion, or memory allocation.
Exists try catch or similliar for C ?
I have a big problem with strings, and I need continue executing the program before a exception in string conversion, or memory allocation.
Not in standard C, there isn't.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
in this case, how the best method to get a memory error, or string error ?
only with function returns ?
What sort of error?
A "replacement" for try/catch can be made by having (a stack of) longjmp buffer(s), and using setjmp to define the "catch" point, and longjmp to return to the setjmp'd location. However, there's a lot of things to take into account: longjmp doesn't do any clean-up. C++ has the ability to clean up any objects created between the exception and teh catch.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
ok,
in this moment Im have a big problem with memory limit,
i will study more this
thank's
In addition, longjmp() can destroy the contents of local variables because of compiler optimizations (if a variable is held in a register, then upon a longjmp this variable will be erroneously "restored" to some previous value even though it has been updated in the interim between setjmp() and longjmp() ). On the whole it's not reliable at all.
Code://try //{ if (a) do { f( b); } while(1); else do { f(!b); } while(1); //}
So, assuming you can catch that you've run out of memory, what are you going to do about it?
Are you genuinely running out of memory, or is it caused by a leak of some sort?
If the latter, perhaps you want to track where you allocate and free, and what memory you have allocated and freed.
There are ways to "track" your memory allocations. For example:
Then implement the trackmalloc() and trackfree() functions:Code:// trackmalloc.h #ifndef INTERNAL #define malloc(x) trackmalloc(x, __LINE__, __FILE__) #define free(x) trackfree(x, __LINE__, __FILE__) #endif void *trackmalloc(size_t size, int line, const char *file); void trackfree(void *ptr, int line, const char *file);
I should mention I just typed that in - it may have compile and runtime errors in it.Code:#define INTERNAL #include "trackmalloc.h" #include <stdlib.h> struct memblock { long magic; struct memblock *next; struct memblock *prev; const char *file; size_t size; int line; }; #define MAGIC 0x12345678 #define MAGIC 0x87654321 // Note: Above may need padding on 64-bit machines. Should be OK on 32 and 16-bit machines. struct memblock *memblock_list = NULL; void *trackmalloc(size_t size, int line, const char *file) { struct memblock *mb = malloc(size + sizeof(*mb)); if (!mb) // May want to output some error message here! return NULL; mb->magic = MAGIC1; mb->file = file; mb->line = line; if (memblock_list) mb->prev = memblock_list; mb->next = memblock_list; memblock_list = mb; return (void *)&mb[1]; } void *trackfree(void *ptr, int line, const char *file) { if (!ptr) return; else { struct memblock *mb = ((struct memblock *)(ptr))[-1]; if (mb->magic != MAGIC1) { // Bad free. } mb->magic = MAGIC2; if (mb == memblock_list) { memblock_list = mb->next; } // Unlink it. if (mb->next) mb->next->prev = mb->prev; if (mb->prev) mb->prev = mb->prev->next; free(mb); } }
--
Mats
Last edited by matsp; 01-05-2009 at 01:45 PM.
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Well, memory allocation with malloc can be "handled" because malloc returns NULL on error. As for memory conversion, that depends on the function. For example a function might return lets say 0 when you convert an invalid sting to an int. Or something else.
In any case, try/catch is not the only way to handle "exceptions". You can just use the return value of functions to do your job.
Also, if you google there are try/catch methods for C that you can use, but of course the code won't be portable this way.
You can post your code and get suggestions on how to handle errors in C
i know exeptions from JAVA
there is OUT OF BOUND EXEPTION when you read a cell in an array which index is bigger then the size.
you handle the exception by saying
which is basicly tells you what to do in case of the exceptionCode:try { } catch{ }
but its java
thanks a lot for all
matsp, for you principally.
Im using a ICP-i7188EX
http://www.icpdas.com/products/PAC/i.../7188E_PAC.htm
512 K SRAM
And lets don't forget that in order to handle an exception an expression has to throw it. If you don't do bound checking you won't handle an exception no matter the language.
EDIT: You can use functions and create an elegant way of handling simple exceptions in C. You can use goto/break to leave a big chunk of code like try/catch. For example:
Code:void* ptr safe_malloc(void** ptr, int times) { *ptr = malloc(times * sizeof(**ptr)); if (*ptr == NULL) {mem_flag = 0; return 1;} return 0; } .... while(1){ //TRY if (safe_malloc(&arr1, 123)) break; ... if (safe_malloc(&arr2, 123)) break; ... break; } if (!mem_flag) //CATCH Memory exception { ... } if (!out_of_bounds_flag) //CATCH Out of bounds exception { ... } if (1) // FINALLY { ... }
Last edited by C_ntua; 01-05-2009 at 02:12 PM.
you could always use window's GetLastError() function, but of course, that would make it strictly OS-dependant.