Path: news.mathworks.com!not-for-mail
From: "James Tursa" <aclassyguywithaknotac@hotmail.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Sparse matrix product again
Date: Mon, 12 May 2008 23:05:57 +0000 (UTC)
Organization: Boeing
Lines: 49
Message-ID: <g0aigl$36t$1@fred.mathworks.com>
References: <67rg20F2pn19mU1@mid.dfncis.de> <fvfd1n$1dc$1@fred.mathworks.com> <688agnF2rcjvjU1@mid.dfncis.de> <fvmtmc$k80$1@fred.mathworks.com> <68dnitF2soa18U1@mid.dfncis.de> <fvsiih$m48$1@fred.mathworks.com>
Reply-To: "James Tursa" <aclassyguywithaknotac@hotmail.com>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1210633557 3293 172.30.248.35 (12 May 2008 23:05:57 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Mon, 12 May 2008 23:05:57 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 756104
Xref: news.mathworks.com comp.soft-sys.matlab:468029


"Tim Davis" <davis@cise.ufl.edu> wrote in message 
<fvsiih$m48$1@fred.mathworks.com>...
> Instead of "int" you should use mwSignedIndex.  It's 
safer,
> if you ever go to a 64-bit platform.  I also recommend 
not
> using mwIndex at all, but mwSignedIndex in its place.  
The
> problemis that mwIndex is unsigned, so codes like this:
> 
> mwIndex i, n ;
> ...
> 
> for (i = n-1 ; i >= 0 ; i--)  { do something }
> 
> fail in an infinite loop, since i is always >= 0.

Tim,

I also avoid unsigned integer types whenever possible for 
these types of concerns, but I think your advice is 
incomplete. For example, if a function returns an mwIndex 
and you stuff the result it into an mwSignedIndex variable 
without any overflow checking, you may get a negative 
number that will cause downstream problems.

I prefer to get the return value of a function into the 
exact same typed variable and *then* deal with it. If you 
want to use only signed types in your code after that then 
go ahead and convert it but check the result first. e.g., 
suppose you know that mwIndex is unsigned, then consider 
the following:

    mwIndex ui;
    mwSignedIndex si;
    ui = (some function that returns an mwIndex type);
    si = (mwSignedIndex) ui;
    if( si < 0 )
        overflow ... deal with the error;

In the above code you will detect the overflow when the 
return value is very large and you can deal with it before 
causing downstream problems. But even the above code has a 
flaw if mwIndex and mwSignedIndex are not the same size.  
So to be extra sure you might want to put in even more 
checks using sizeof, etc.

James Tursa