-2

I'm trying to set an invalid value to -1.. But I don't like magic numbers.. Anyone know where to find a set of common constants. I'm working in VS6 (ish).

I'm trying to read a file from across a network, and I need a bad value for the total file size,so I know if I got valid info on it.. 0 is a valid size so I can't use that.

Harper Shelby HIT THE NAIL ON THE HEAD.. Just a little thumb. He mentioned the win32 constants.. which is exactly what I was thinking about.. Now to find a link :)

baash05
  • 4,394
  • 11
  • 59
  • 97
  • I'm not sure what are you asking for? For instance, why don't you define your own constant? – hasen Dec 09 '08 at 21:46
  • everyone does this.. I could do it.. But I'd rather use something standard.. Like when you SetLastError... It's always better IMHO to use the values in WINERROR.H – baash05 Dec 09 '08 at 21:52
  • Ironically, this question has INVALID_FILE_SIZE votes at the moment. Let's keep it hat way. ;-) – Jon 'links in bio' Ericson Dec 09 '08 at 22:11
  • you can't just ask "hey where do i get an invaid num huh?". it depends on the domain and the specific function you are calling. – Johannes Schaub - litb Dec 10 '08 at 01:28
  • I got an answer! I don't understand the neg vote on questions. It was clear, specific, and answerable. I wanted a general list of some of the consts available in VS. I used -1 as a sample, a use case. Perhaps my next question should be what the best lang to learn, or your favorite color coffee mug. – baash05 Dec 10 '08 at 04:31
  • Maybe because you changed the question half way through – Lodle Dec 10 '08 at 08:20
  • @baash05, you asked 2 different questions. Kind of hard to sneak something by with an edit history. – mmcdole Dec 10 '08 at 13:06

8 Answers8

2
#define BAD_VALUE -1

EDIT: the original question had no context. The revised question indicates you want an invalid file size and are thus looking for the win32 constants. Look at windows.h i think the constant you seek may be in windows.h or one of its sub-includes. grep your windows include directory ;-)

Steven A. Lowe
  • 60,273
  • 18
  • 132
  • 202
2

If -1 is an invalid value for a return value in your system, you should define it internally:

const int INVALID_FOO = -1

unless C compatibility is needed, in which case

#define INVALID_FOO -1

would be preferred. If it's a standard MFC or Windows resource, use INVALID_HANDLE or one of the other Win32-defined constants.

Harper Shelby
  • 16,475
  • 2
  • 44
  • 51
1

If you want to use constants used by WinApi, check out the WinError.h, WinUser.h and WinNT.h files.

Aoi Karasu
  • 3,730
  • 3
  • 37
  • 61
1

You want to use your own magic number -1 disguised as a Windows constant. This is very misleading.

Suppose I happen to know that INVALID_HANDLE is 0. Is it OK to initialize my pointers with INVALID_HANDLE?

char *myMessage = INVALID_HANDLE;

How does this strike you?

  • As readable.. What's in mymessage.. Oh it's an invalid handle.. I shouldn't use it. Hell who cares what it is.. INVALID_HANDLE could be 43264.. If I test. if(myMessage == INVALID_HANDEL) printf("the message has not been initilized"); works for me... – baash05 Dec 10 '08 at 04:38
1

In VS, Create a new windows console application project. Go into project settings and turn on browse support. Create a C++ file and add it to the project. Type:

#include <windows.h>
void main(void) {}

into the file. Compile it. Now type INVALID_FILE_SIZE into the file. Right click on it and goto definition of INVALID_FILE_SIZE. VS will open one of the many windows header files full of defined values. Enjoy.

jmucchiello
  • 18,754
  • 7
  • 41
  • 61
1

First thing is you should be using an unsigned int for file size as a file size is never negative. Now an invalid file size is normally the max int so in the case of using a 32 bit unsigned int it would be 0xFFFFFFFF

i.e.

const unsigned int INVALID_FILESIZE = 0xFFFFFFFF;

Also if this is on windows, windows.h defines invalid file size all ready (INVALID_FILE_SIZE)

Lodle
  • 31,277
  • 19
  • 64
  • 91
-1

It's generally accepted the 0 and 1 (positive & negative) are OK to use directly.

In fact, it'll probably make you code even more confusing to use a variable instead.

Update: OK, you updated your question after I wrote my answer. If you are using "-1" in an arithmetic way, then just "-1" is fine. If you are returning a error code (and the code just happens to be -1) then you should use a const.

 const int INVALID_VALUE = -1;
James Curran
  • 101,701
  • 37
  • 181
  • 258
  • It really wasn't a question of what to use, but thanks. I mean I was going to use a const/define variable.. I was kinda just looking for some of the standard ones that come with windows programming. like MB_OK or ID_YES, VK_ENTER... They're easy to read and available.. It was more of a curriosity. – baash05 Dec 10 '08 at 04:43
  • What is funny is that when ever I ask a real question I get down votes, No one tacks on a comment saying "can you elaborate", or anything. I'm not the most experienced programmer but hey, my questions lead somewhere. – baash05 Dec 10 '08 at 04:46
-1
If bytes_read < 0
    // error
EndIf
strager
  • 88,763
  • 26
  • 134
  • 176
  • A. I don't see a list in there, B. I don't see a constant in there. C. since the bytes_read is an unsigned variable it will never be less then 0 (assuming that had anything to do with the question) – baash05 Dec 10 '08 at 04:49
  • @baash05: "I'm trying to read a file from across a network, and I need a bad value for the total file size,so I know if I got valid info on it.. 0 is a valid size so I can't use that." Looks like some fread()-style function, which returns the number of bytes read, or -1 (which is < 0) on error. – strager Dec 10 '08 at 18:44