473,394 Members | 1,709 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Assertion before definitions


What's the canonical way to perform an assertion before the definition of any
objects within a function? I've got this at the moment:

void AddFiveEachElement(int *p,size_t const len)
{
int dummy = (assert(p),assert(len),0);

int const *const pover = p + len;

/* Let's pretend we initialise an array here:

int array[4] = {p[0],p[1],p[2],p[3]};
*/

do
{
*p++ = 5;
}while(pover != p);
}

--

Frederick Gotham
Aug 25 '06 #1
7 1275
Frederick Gotham <fg*******@SPAM.comwrote:
#
# What's the canonical way to perform an assertion before the definition of any
# objects within a function? I've got this at the moment:

With modern C compilers, you can intermingle statements and
declarations

# {
# assert(p); assert(len);
# int const *const pover = p + len;
# /* Let's pretend we initialise an array here:
# int array[4] = {p[0],p[1],p[2],p[3]};
# */
# do
# {
# *p++ = 5;
# }while(pover != p);
# }

On older C compilers, you can insert additional {...}

# {
# assert(p); assert(len); {
# int const *const pover = p + len;
# /* Let's pretend we initialise an array here:
# int array[4] = {p[0],p[1],p[2],p[3]};
# */
# do
# {
# *p++ = 5;
# }while(pover != p);
# }}

--
SM Ryan http://www.rawbw.com/~wyrmwif/
So....that would make Bethany part black?
Aug 26 '06 #2
Frederick Gotham wrote:
What's the canonical way to perform an assertion before the definition of any
objects within a function?
Use C99?

But there isn't one for C90 that I know of. If you want a genuine
debugging tool
you'll either use a serious debugger, or roll your own suite of more
useful tools.
I've got this at the moment:

void AddFiveEachElement(int *p,size_t const len)
{
int dummy = (assert(p),assert(len),0);
Note that assert() on C90 requires an int argument, so you should make
your assertions more explicit.
>
int const *const pover = p + len;

/* Let's pretend we initialise an array here:
int array[4] = {p[0],p[1],p[2],p[3]};
Just as well you're only pretending. ;-)
*/

do
{
*p++ = 5;
}while(pover != p);
}
One simple way is...

void foo(int *p,size_t len)
{
assert(p != NULL);
assert(len != 0);
{
/* ze code */
}
}

But performing exit checks in non-void functions makes it even uglier.

--
Peter

Aug 26 '06 #3
Frederick Gotham wrote:
What's the canonical way to perform an assertion before the definition of any
objects within a function? I've got this at the moment:

void AddFiveEachElement(int *p,size_t const len)
{
int dummy = (assert(p),assert(len),0);

int const *const pover = p + len;
{
int * const pover = p+len;
assert(p != NULL && len 0);
...
The only risk is if the arithmetic or the assignment
cause undefined behavior. (eg int *const pover = p[5]) would
be bad.) In this case, the addition is valid regardless of
the values of p and len, so there's no risk. Even if
you have a case where there is risk, I think it's acceptable
to write it this way, since the assertion is only being
used as a clue to the human reader. I suppose that if
the assignment invokes undefined
behavior that makes the assertions not abort
in the normal fashion, it might cause some trouble
tracking down the error.

Aug 26 '06 #4
Bill Pursell wrote:
Frederick Gotham wrote:
>What's the canonical way to perform an assertion before the definition of any
objects within a function? I've got this at the moment:

void AddFiveEachElement(int *p,size_t const len)
{
int dummy = (assert(p),assert(len),0);

int const *const pover = p + len;

{
int * const pover = p+len;
assert(p != NULL && len 0);
...
The only risk is if the arithmetic or the assignment
cause undefined behavior. (eg int *const pover = p[5]) would
be bad.) In this case, the addition is valid regardless of
the values of p and len, so there's no risk.
No it is not valid for all values of p and len. Pointer arithmetic is
only defined within an object and one past the end of the original
object. Since the null pointer does not point to an object (that is a
major point of it) *any* arithmetic with a null pointer is undefined
through the lack of a definition. If p points to a valid object and
p+len takes you more that one past the end of the object that again you
have undefined behaviour.
Even if
you have a case where there is risk, I think it's acceptable
to write it this way, since the assertion is only being
used as a clue to the human reader. I suppose that if
the assignment invokes undefined
behavior that makes the assertions not abort
in the normal fashion, it might cause some trouble
tracking down the error.
Or the assignment having invoked undefined behaviour could cause the
program to ignore the assert and continue to crash later. How the
implementation could achieve this I'm not sure, but a sufficiently
perverse implementation is certainly allowed to.

So why not avoid the undefined behaviour the assert is intended to catch
by doing the assert first to it is ensured for of the chance to do its job.
--
Flash Gordon
Aug 26 '06 #5

Flash Gordon wrote:
Bill Pursell wrote:
Frederick Gotham wrote:
What's the canonical way to perform an assertion before the definition of any
objects within a function?
<snip discussion of pointer arithmetic>
So why not avoid the undefined behaviour the assert is intended to catch
by doing the assert first to it is ensured for of the chance to do its job.
How do you do that
without using C90 syntax and allowing declarations to be
mixed with code? eg, you can't do:

int *const p;
assert( f != NULL);
p = f + l; <-- invalid write to read-only p

So you must instead do:
assert(f != NULL);
int *const p = f+l;

Or do as Frederick did in the original post, something like:
int a = (assert(f != NULL), 0);
int *const p = f + l;

Personally, I think that's pretty ugly. It's a little bit better to
group it with the assignment: eg

int *const p = (assert(f!=NULL), f+l);

--
Bill Pursell

Aug 26 '06 #6
Bill Pursell wrote:
Flash Gordon wrote:
>Bill Pursell wrote:
>>Frederick Gotham wrote:
What's the canonical way to perform an assertion before the definition of any
objects within a function?

<snip discussion of pointer arithmetic>
>So why not avoid the undefined behaviour the assert is intended to catch
by doing the assert first to it is ensured for of the chance to do its job.

How do you do that
without using C90 syntax and allowing declarations to be
mixed with code? eg, you can't do:

int *const p;
assert( f != NULL);
p = f + l; <-- invalid write to read-only p

So you must instead do:
assert(f != NULL);
int *const p = f+l;
Or if the function is short enough to check by inspection
int *p; /* Note to maintainer, this should never be modified after
initial assignment */
assert( f != NULL);
p = f + l;

Not ideal.
Or do as Frederick did in the original post, something like:
int a = (assert(f != NULL), 0);
int *const p = f + l;

Personally, I think that's pretty ugly. It's a little bit better to
group it with the assignment: eg

int *const p = (assert(f!=NULL), f+l);
Or as has also been suggested introducing another scope.

None of them are ideal, but they all have the advantage of doing the
check before invoking undefined behaviour so ensuring that it will work
properly rather than relying on the properties of the system.

Personally I will allow a little ugliness for the sake of portability.
However, once it is accepted that there is undefined behaviour it is up
to the people in question to decide on whether for them it is worth
loosing some potential portability for the sake of better looking code
or not. Not everything has to be portable to all possible systems after
all and this is a check to catch program errors during development
rather than in release code where with NDEBUG defined the assert won't
do anything anyway.
--
Flash Gordon
Aug 26 '06 #7
>>>>Frederick Gotham wrote:
>>>>>What's the canonical way to perform an assertion before the
>definition of any objects within a function?
>Flash Gordon wrote:
>>So why not avoid the undefined behaviour the assert is intended to catch
by doing the assert first to it is ensured for of the chance to do its job.
>Bill Pursell wrote:
>How do you do that
without using C90 syntax and allowing declarations to be
mixed with code? eg, you can't do:

int *const p;
assert( f != NULL);
p = f + l; <-- invalid write to read-only p

So you must instead do:
assert(f != NULL);
int *const p = f+l;
Right -- but this uses C99 features (specifically, "variable
declaration/definition almost anywhere", or at least "after
code and without intervening open-brace").

In article <pu************@news.flash-gordon.me.uk>
Flash Gordon <sp**@flash-gordon.me.ukwrote:
>Or if the function is short enough to check by inspection
int *p; /* Note to maintainer, this should never be modified after
initial assignment */
assert( f != NULL);
p = f + l;

Not ideal.
Maybe not ideal, but probably what I would do. Of course, I always
thought that "const" *should* have been defined as a storage-class
specifier (with appropriate modifications to the syntax and semantics
so that you can apply it as well as, e.g., "static"), rather than
a type-qualifier. So my opinion may be suspect. :-)
>Or do as Frederick did in the original post, something like:
int a = (assert(f != NULL), 0);
int *const p = f + l;

Personally, I think that's pretty ugly. It's a little bit better to
group it with the assignment: eg

int *const p = (assert(f!=NULL), f+l);
I can definitely go for this one.
>Or as has also been suggested introducing another scope.
Note that you can even do this by introducing an entire separate
function, e.g.:

void operate_unchecked(T *ptr, size_t len) {
... all the "real work" goes here ...
}

void operate(T *ptr, size_t len) {
/* this is not an assert() because it is delivered in the
final version of the product! */
if (ptr == NULL || len == 0)
panic("bad arguments to operate()");
operate_unchecked(ptr, len);
}

Depending on performance goals and profiling, "operate_unchecked"
can initially be static (and, in C99, explicitly "inline" if you
like), and later exposed (if performance testing proves that the
parameter-checking is a significant performance problem).
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Aug 27 '06 #8

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

Similar topics

3
by: Todd Miller | last post by:
Hi, I recently discovered an assertion failure in the Python garbage collection system when scripts using our C extension (numarray) exit. The assertion is activated for Pythons configured using...
0
by: benevilent | last post by:
Hey, I'm getting an assertion error as a result of embedding python. "Modules/gcmodule.c:231: visit_decref: Assertion `gc->gc.gc_refs != 0' failed." I only get this assertion error with...
4
by: Morgan Leppink | last post by:
Hey all - We are running SQL 2000 with ALL available service packs, etc. applied. We just built a brand new database server, which has dual 2Ghz XEONs, 2GB memory, and the following disk...
3
by: bill | last post by:
I firmly believe that it is always a bad idea to put code in a header file. Nothing pisses me off more than seeing function definitions in a ..h, and I recently was truly blessed :) to witness...
2
by: Craig Klementowski | last post by:
Pardon the cross post, but I'm not sure where exactly to post this question. We have MFC application using many MFC extention DLL's. We started using a new MFC extention DLL that is mixed mode so...
1
by: Timur Safin | last post by:
Hi All, Sorry if it is offtopic here, I wasn't able to find any more relevant group... I'm slowly approaching AMD64 build for our product (as my personal fun project). And after I ran that...
5
by: Ron Louzon | last post by:
I have some C++ code that uses the CSingleLock( CCriticalSection *) constructor. In visual C++ 6.0, this code compiles and runs fine in both Debug and release modes. However, in Visual Studio...
2
by: Penny Balkwill via .NET 247 | last post by:
(Type your message here) I am supporting a system which uses ORACLE Forms, and we are getting an intermittent error: Assertion failed! Program: D:\Dev6i\bin\ifrun60.exe File:...
4
by: Mullai | last post by:
Hi , My program gives an error message like this Debug Assertion Failed! program:................ File: wincore.cpp Line: 958 Please can anyone help me out in this issue. I have to solve...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...

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.