1

is there a try-catch statement in C? or an external library someone made? would be very useful

if not, is there a way one can determine if a variable is an array?

tekknolagi
  • 10,663
  • 24
  • 75
  • 119
  • 6
    I shudder to imagine the programs you've written, not knowing how else to tell if a variable is an array other than wrapping every access in a try/catch... – drysdam Apr 25 '11 at 02:17
  • not every access - just checking array sizes is a bit of a pain. that's what it's form. – tekknolagi Apr 25 '11 at 02:25

6 Answers6

3

is there a try-catch statement in C? or an external library someone made? would be very useful

On Windows there is SEH, but you really shouldn't use that for general application usage. Without something like C++ destructors it's impossible to write truly exception safe code.

if not, is there a way one can determine if a variable is an array?

Yes. You keep track of it yourself.

Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
  • @tekknolagi: Not just given a pointer to a block that is an array, no. You have to pass the array length yourself. (I'm assuming you're talking about passing this to a function? ) – Billy ONeal Apr 25 '11 at 02:45
  • i meant using sizeof() as a way to determine if something as an array, assuming that the value returned by sizeof() would be greater than, say, sizeof(4). – tekknolagi Apr 25 '11 at 03:06
  • @tekknolagi: Not reliably, no. What happens if someone does `struct ABC { int isAnArray[42]; }`? The structure is not an array. If you're trying to determine the size of an array outside of the scope where it was declared, then it's going to suffer the pointer decay problem, at which point `sizeof()` will always return the size of a pointer. – Billy ONeal Apr 25 '11 at 03:14
1

There is no try-catch statement in C, but you can build an exception mechanism using jump buffers. However, that's probably a very bad idea as there is now way to automatically release resources when an exception is thrown.

If you are referring to a void * as a variable, then there is no way to determine if it is an array. However, you can build logic into your application to achieve runtime type information (RTTI) as well.

Judge Maygarden
  • 26,961
  • 9
  • 82
  • 99
  • 1
    You can build a *resource leaky* exception mechanism.... without destructors it's nearly impossible to write safe code there.... – Billy ONeal Apr 25 '11 at 01:54
  • Sure, but you could say the same thing about using malloc. – Judge Maygarden Apr 25 '11 at 02:10
  • 1
    I don't understand your point. It's impossible to correctly manage resources without either A. destructors or B. `finally` clauses. You can't get either of those in C. – Billy ONeal Apr 25 '11 at 02:11
  • @Billy I'd never emulate exceptions in C for that reason, but it doesn't mean some masochistic person couldn't pull it off. God help anyone that would have to maintain the code afterwards! – Judge Maygarden Apr 25 '11 at 04:40
1

I'm developing exceptions4c, an exception handling system in C (portable ANSI C) that currently supports: throw, try, catch, finally and a few more goodies. For example, it supports the Dispose pattern, so you can automatically release resources. You can also handle signals (such as SIGFPE and SIGSEGV) as if they were exceptions.

Guillermo Calvo
  • 777
  • 6
  • 9
0

There is no native support for exceptions in C, of course.

Take a look at the following link, basically, it makes use of setjmp() and longjmp():

http://www.on-time.com/ddj0011.htm

manojlds
  • 290,304
  • 63
  • 469
  • 417
0

If the compiler can determine if a variable is an array, so can you. Just look where it's declared (or malloc'd, in the case of a dynamically allocated array).

An̲̳̳drew
  • 13,375
  • 13
  • 47
  • 46
  • @Billy - Not necessarily. IIRC, the standard only mandates that `void *` is large enough to hold any (object) pointer type, not that pointer types necessarily be the same size. – Chris Lutz Apr 25 '11 at 02:38
  • @Chris: Not as far as I am aware. C (and C++) don't require function pointer types to be the same size as data pointer types, but for the most part a pointer is a pointer is a pointer. C allows for a difference between `int`, `float`, `struct`, and `union` types. However, `sizeof(struct A *)` and `sizeof(struct B *)` are required to be the same. See section 6.2.5.27 of the standard. (And the general requirement `pointers to qualified or unqualified versions of compatible types shall have the same representation and alignment requirements`) – Billy ONeal Apr 25 '11 at 02:53
  • @Billy - Your statement contradicts itself. You said, "sizeof(pointer) is always going to be the same...", then you said, "C allows for a difference between `int`, `float`, `struct`, and `union` types." – Chris Lutz Apr 25 '11 at 02:59
  • @Chris: Mine says `A pointer to void shall have the same representation and alignment requirements as a pointer to a character type. Similarly, pointers to qualified or unqualified versions of compatible types shall have the same representation and alignment requirements. All pointers to structure types shall have the same representation and alignment requirements as each other. All pointers to union types shall have the same representation and alignment requirements as each other. Pointers to other types need not have the same representation or alignment requirements.` – Billy ONeal Apr 25 '11 at 02:59
  • @Chris: Did you not see "for the most part" in my sentance? I don't throw those words in for no reason. :) – Billy ONeal Apr 25 '11 at 03:00
0

try-catch is a scheme for error handing in an object oriented environment like C++. ANSI C is not object oriented and it requires you declare your variable types. In C, error handling is performed primarily by checking returned values of called functions. You can see more in the messy thread here: ANSI C equivalent of try/catch?

Community
  • 1
  • 1
Perry Horwich
  • 2,798
  • 3
  • 23
  • 51