문제

나는 내 시간과 동료의 시간을 낭비하는 불쾌한 오타가 있었는데, 그것은 다음과 같습니다.

for (i = 0; i < blah; i++); // <- I had a semi-colon here, that's the bug!
{
  // Some awesome logic here
}

우선, 그것은 매우 창피합니다. 두 번째로, 나는 이것을 반복해서는 안됩니다. 나는 C.에 비교적 새로운 것이며 Java에서는 사용할 수 있다고 생각합니다. FindBugs 이와 같은 오류를 잡으려면 C 코드에 어떤 도구를 사용해야합니까? 보풀?

도움이 되었습니까?

해결책

예, PC 린트 아마도 사용 가능한 최고의 도구 일 것입니다.

다른 팁

In addition to Lykathea's PC-Lint suggestion, you can also get better (or at least more) diagnostics if you bump up the warning level of the compiler. Something like /W4 or -Wall

Though I'm not sure if your particular problem would have been caught with this (MS VC doesn't seem to flag it even with all warnings enabled). I think that's because it's not an uncommon idiom for for loops to be empty when the work is done as side effects of the loop control expressions.

A few things that have saved me in the past, from the top of my head:

  • Use if (3 == bla) rather than (bla == 3), because if you misspell and type (3 = bla) the compiler will complain.

  • Use the all-warnings switch. Your compiler should warn you about empty statements like that.

  • Use assertions when you can and program defensively. Put good effort into making your program fail early, you will see the weaknesses that way.

  • Don't try to circumvent any safeguards the compiler or the OS have put in place. They are there for your ease of programming aswell.

Also look at clang static analysis

I would start by learning about splint and gdb. If you need more advanced, build on these two tools. But they are a good start.

GCC has most of the functionality that Lint has had built in via the warning flags.

Any good GUI programming environment ("IDE" - Integrated Development Environment) like Eclipse would generate a warning in a case like that.

A good syntax highlighter will make some cases like this more visible.

I would suggest seeing if you have the ability to enforce MISRA standards. They were written with great thought and many rules that are simple for a compiler to check. For example, A rule I use requires all NOP commands have their own line. This means when you put a ; on the end of a loop statement it will through an error saying that it is not on it's own line.

QA·C by Programming Research is another good static analysis tool for C.

In this (old) version of How to Shoot Yourself In the Foot, and in many other versions around the web, C is always the language that allows for the simplest procedure. When programming in C, you have to remember this and be careful. If you want protection, choose another language.

This saying is attributed to Bjarne Stroustrup (C++) himself. To (mis)quote:

"C makes it easy to shoot yourself in the foot"

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top