From james at colannino.org Wed Jun 2 21:34:59 2004 From: james at colannino.org (James Colannino) Date: Wed, 02 Jun 2004 21:34:59 -0700 Subject: [OCLUG-devel] sizeof () works differently on char's, int's, etc. Message-ID: <40BEAA73.8040306@colannino.org> Hey everyone. I used the following to test how the sizeof() function measures arrays of various variable types: #include char line[10]; int integer[10]; float floaty[10]; int main() { printf ("\nCharacter: %d\n", sizeof(line)); printf ("Integer: %d\n", sizeof(integer)); printf ("Floating: %d\n\n", sizeof(floaty)); return 0; } My output came out as the following: Character: 10 Integer: 40 Floating: 40 So my question is this: why does the sizeof() function work with character arrays but not with numerical ones? Is the sizeof() function only intended to be used for characters and not for numbers? This has me very confused. James -- My blog: http://www.crazydrclaw.com/ My homepage: http://james.colannino.org/ "There are no uninteresting things; only uninterested people." --G.K. Chesterton From tthelin at sbcglobal.net Wed Jun 2 21:43:53 2004 From: tthelin at sbcglobal.net (Tim Thelin) Date: Wed, 02 Jun 2004 21:43:53 -0700 Subject: [OCLUG-devel] sizeof () works differently on char's, int's, etc. In-Reply-To: <40BEAA73.8040306@colannino.org> References: <40BEAA73.8040306@colannino.org> Message-ID: <40BEAC89.7070506@sbcglobal.net> James Colannino wrote: > Hey everyone. I used the following to test how the sizeof() function > measures arrays of various variable types: > > #include > > char line[10]; > int integer[10]; > float floaty[10]; > > int main() > > { > printf ("\nCharacter: %d\n", sizeof(line)); > printf ("Integer: %d\n", sizeof(integer)); > printf ("Floating: %d\n\n", sizeof(floaty)); > > return 0; > } > > My output came out as the following: > > Character: 10 > Integer: 40 > Floating: 40 > > So my question is this: why does the sizeof() function work with > character arrays but not with numerical ones? Is the sizeof() > function only intended to be used for characters and not for numbers? > This has me very confused. > > James James, sizeof gives back the number of bytes of storage a particular entity uses, not the number of elements in that entity. So a typical use would be determining the size of a struct in bytes. Along those lines, your results make sense, as your 10 ints would consume 40 bytes of storage. A way to get what you want, the number of elements of an array, could be something more like: #define GetNumberOfElements( array ) ( sizeof( array ) / sizeof( array[0] ) ) That essentially gets the total bytes the array takes up, and divides it by the size in bytes of a single element. The resuilt is the number of elements. Thanks, Tim Thelin From johnhscs at scsoftware.sc-software.com Wed Jun 2 21:49:09 2004 From: johnhscs at scsoftware.sc-software.com (John Heil) Date: Wed, 2 Jun 2004 21:49:09 -0700 (PDT) Subject: [OCLUG-devel] sizeof () works differently on char's, int's, etc. In-Reply-To: <40BEAA73.8040306@colannino.org> References: <40BEAA73.8040306@colannino.org> Message-ID: On Wed, 2 Jun 2004, James Colannino wrote: > Date: Wed, 02 Jun 2004 21:34:59 -0700 > From: James Colannino > To: oclug-devel at oclug.org > Subject: [OCLUG-devel] sizeof () works differently on char's, int's, etc. > > Hey everyone. I used the following to test how the sizeof() function > measures arrays of various variable types: > > #include > > char line[10]; > int integer[10]; > float floaty[10]; > > int main() > > { > printf ("\nCharacter: %d\n", sizeof(line)); > printf ("Integer: %d\n", sizeof(integer)); > printf ("Floating: %d\n\n", sizeof(floaty)); > > return 0; > } > > My output came out as the following: > > Character: 10 > Integer: 40 > Floating: 40 > > So my question is this: why does the sizeof() function work with > character arrays but not with numerical ones? Is the sizeof() function > only intended to be used for characters and not for numbers? This has > me very confused. > > James This is due to the intrinsic size of the data type. The array count counts the number of occurances of the data type which means that the total_size == data_type_size * count data type size count total array size ========= ==== ===== ================ char 1 10 10 int 4 10 40 float 4 10 40 Note that the size of int is platform dependent and is _not_ guaranteed to be 4 everywhere. On Intel/AMD 32 chips, it is 4. Other chips, maybe not. IIRC ditto for float. cheers johnh > -- > My blog: http://www.crazydrclaw.com/ > My homepage: http://james.colannino.org/ > > "There are no uninteresting things; only uninterested people." --G.K. > Chesterton > > _______________________________________________ > OCLUG-devel mailing list -- OCLUG-devel at oclug.org > http://www.oclug.org/mailman/listinfo/oclug-devel > - ----------------------------------------------------------------- John Heil South Coast Software Custom systems software for UNIX and IBM MVS mainframes 1-714-774-6952 johnhscs at sc-software.com http://www.sc-software.com ----------------------------------------------------------------- From james at colannino.org Wed Jun 2 21:58:21 2004 From: james at colannino.org (James Colannino) Date: Wed, 02 Jun 2004 21:58:21 -0700 Subject: [OCLUG-devel] sizeof () works differently on char's, int's, etc. In-Reply-To: <40BEAC89.7070506@sbcglobal.net> References: <40BEAA73.8040306@colannino.org> <40BEAC89.7070506@sbcglobal.net> Message-ID: <40BEAFED.9020609@colannino.org> Tim Thelin wrote: > A way to get what you want, the number of elements of an array, could be > something more like: > #define GetNumberOfElements( array ) ( sizeof( array ) / sizeof( > array[0] ) ) > That essentially gets the total bytes the array takes up, and divides it > by the size in bytes of a single element. The resuilt is the number of > elements. That is really cool :) That definately solves my problem. Thank you very much for your solution, and thank you John for explaining in such depth the way the sizeof() function works. As long as nobody minds, I'll probably be creating quite a bit of noise on this mailing list as I go through my beginning C book :-P James -- My blog: http://www.crazydrclaw.com/ My homepage: http://james.colannino.org/ "There are no uninteresting things; only uninterested people." --G.K. Chesterton From james at colannino.org Wed Jun 2 22:27:11 2004 From: james at colannino.org (James Colannino) Date: Wed, 02 Jun 2004 22:27:11 -0700 Subject: [OCLUG-devel] ArrayMaxValue() - how could I improve it? Message-ID: <40BEB6AF.7090102@colannino.org> The following is a function I whipped up to answer one of the programming exercises in my book which asked for a function capable of returning the highest value in an array. Since I'm new to programming and haven't been exposed to a lot of the other functions that are out there lurking in the standard libs, and since other people have different ways of doing things, I was just wondering if anybody sees where I could improve upon this. I know that one limitation I'd like to do away with is the fact that instead of just parsing through the array until the user's input is finished, it's going to go all the way up to element 20, even if the user only enters two or three numbers. Also, how does my coding style look? Are my comments too verbose? Does anything about the way I make use of whitespace make things more confusing? If I'm starting off my stay on OCLUG-devel by asking too many questions feel free to let me know :-P C is just really exciting to me right now and I have so many things I want to learn and so many questions I want to ask... James /******************************************************\ * This function parses through the elements in an * * array and returns the largest value. the variable * * 'number' is assigned each element in the array and * * is compared to the variable 'MaxValue' which * * contains the highest value found in each pass of * * the loop. The loop breaks when array[position] is * * greater than the number of elements in the array. * * The result is then returned to main(). * \******************************************************/ ArrayMaxValue(int number, int array[20]) /* array[ ] can hold a maximum of 20 elements */ { int position = 0; /* our current position in the array */ int MaxValue = 0; /* stores the highest value found */ while (array[position] <= ( sizeof(array) / sizeof( array[0] )){ number = array[position]; /* store the current element in 'number' */ if (number > MaxValue) /* if the current element is greater... */ MaxValue = number; /* then assign 'number' to 'MaxValue' */ } return (MaxValue); } -- My blog: http://www.crazydrclaw.com/ My homepage: http://james.colannino.org/ "There are no uninteresting things; only uninterested people." --G.K. Chesterton From tthelin at sbcglobal.net Wed Jun 2 23:36:18 2004 From: tthelin at sbcglobal.net (Tim Thelin) Date: Wed, 02 Jun 2004 23:36:18 -0700 Subject: [OCLUG-devel] ArrayMaxValue() - how could I improve it? In-Reply-To: <40BEB6AF.7090102@colannino.org> References: <40BEB6AF.7090102@colannino.org> Message-ID: <40BEC6E2.3020203@sbcglobal.net> James Colannino wrote: > I know that one limitation I'd like to do away with is the fact that > instead of just parsing through the array until the user's input is > finished, it's going to go all the way up to element 20, even if the > user only enters two or three numbers. There are two ways to deal with this, either have a value in the array which means "i'm an endpoint so don't go past me" or you store the number of valid entries somewhere. For instance a C string is the first case, you know there are no more elements when you hit null '\0' An example of the second case would be having a data structure that contained your array and had a size field.... so something like struct mydata { int array[ 20 ]; int numValidEntries; } > Also, how does my coding style look? Are my comments too verbose? > Does anything about the way I make use of whitespace make things more > confusing? Style is really subjective, just make sure you and anyone you work with agree with the style chosen. One of the biggest issues with software is maintaining it once its written, so comments are a Good Thing (tm). However don't comment the obvious or its harder to follow whats going on (visual clutter). So comment what people might not understand immediatly. This is learned with practice. Also, make use of the C++ "//" style comments as (at least for me) its less visual clutter than "/* ... */" everywhere. Whitespace usage is also subjective, so use what makes the code more readble to you. I prefer to use 4 spaces for my indents to make them stand out and I make moderate use of blank lines so that I can make groups of statements that act together. All this is really preference though. Too many blank lines, etc, and the code becomes harder to read because your eyes must have to move so much. Btw a "for" loop would probably be better than the "while" (did the book cover "for" loops yet?) so something like: int position = 0; int maxValue = 0 for( position = 0; position < 20; position++ ) { number = array[ position ]; if( number > maxValue ) maxValue = number } return maxValue; Btw, potential issues in the code that can cause bugs: * I don't see "position" incremented anywhere in your function... so is it really looping through the array correctly? * MaxValue starts at 0, so if your array was all negative numbers ( i.e. { -1, -100, -60 } ), it would return 0 instead of the largest negative (i.e. -1) Thanks, Tim Thelin From x at xman.org Thu Jun 3 00:01:42 2004 From: x at xman.org (Christopher Smith) Date: Thu, 03 Jun 2004 00:01:42 -0700 Subject: [OCLUG-devel] ArrayMaxValue() - how could I improve it? In-Reply-To: <40BEB6AF.7090102@colannino.org> References: <40BEB6AF.7090102@colannino.org> Message-ID: <1086246101.6583.77.camel@localhost> Okay, a couple of things: 1) Your book is probably based on the C89 standard, rather than C99, so it probably doesn't mention C++ style comments, but I'd encourage their use in a number of places 2) There is no need for the number parameter. Consider this pseudo code: int array[20] = { /* initialize the array */ }; int answer1 = ArrayMaxValue(0, array); int answer2 = ArrayMaxValue(1, array); int answer3 = ArrayMaxValue(123124, array); Would you expect answer3 be any different than answer1 or 2? No. This is a good indicator that it should not be a function parameter. Technically the variable isn't needed at all, but it does help with code clarity, so I wouldn't discard it out of hand. 3) Your while loop test condition is broken. "array[position]" returns the value stored at offset "position" in the array "array". What you want is just "position". Also, because array indexes are zero based, you want just "<" instead of "<=". 4) Currently, your "position" never changes, so your loop will never exit, and your function will never return. Sometimes it is easier to use a "for" loop to avoid this kinds of errors when iterating through an array. 5) Always declare the return type of your functions. While C89 will default to the return type of an int, C99 will not. More importantly it is just good syntactic hygiene. 6) I'm not a big fan of the "no braces block", particularly when you have comments which kind of make it look like there is more than one line there. That being said, there are those who would say I was flat out wrong, so consider this just a matter of style. 7) You don't need parenthesis around MaxValue in your return statement. 8) I find it helpful to follow consistent caps rules for my variables. So, either "position" and "MaxValue" should both be capitalized, or they both should start with a lower case letter, but not a mix. 9) Most Unix C hackers use underscore characters for word boundaries in names, rather than changes in capitalization. So, it'd be "array_max_value" instead of "ArrayMaxValue". This is also very much a stylistic thing, but it is worth noting that ArrayMaxValue is the Windows style. ;-) 10) I'd encourage you to use tools like doxygen for your code documentation. 11) Your current design passes in the array by value, rather than by reference. I assume you haven't reached the section on pointers in your book yet, so I won't go in to how you would make it pass by reference, but suffice it to say, you'll be discovering better ways to do this shortly. 12) Your function could tack on another parameter which indicates the number of elements in the array to examine. 13) array[] doesn't hold a *maximum* of 20 elements, it holds exactly 20 elements. There is no magic in C that indicates you are at the end of an array. If you called it with an array of 19 elements it would corrupt the stack. 14) Since your array is made up of *signed* integers, it would be a bad assumption that your maximum value will be greater than or equal to zero (what if the array is all negative values?). Why not just initialize it with the first element of your array? Attached is a sample implementation of your function that I came up with. I tried not to use concepts you might not understand or have familiarity with. See if it makes some of what I'm saying any clearer. -- Christopher Smith -------------- next part -------------- A non-text attachment was scrubbed... Name: maxValue.c Type: text/x-csrc Size: 623 bytes Desc: not available Url : http://localhost.localdomain/pipermail/oclug-devel/attachments/20040603/50219fb9/attachment.bin From james at colannino.org Thu Jun 3 00:05:07 2004 From: james at colannino.org (James Colannino) Date: Thu, 03 Jun 2004 00:05:07 -0700 Subject: [OCLUG-devel] ArrayMaxValue() - how could I improve it? In-Reply-To: <40BEC6E2.3020203@sbcglobal.net> References: <40BEB6AF.7090102@colannino.org> <40BEC6E2.3020203@sbcglobal.net> Message-ID: <40BECDA3.8010103@colannino.org> Tim Thelin wrote: > James Colannino wrote: > >> I know that one limitation I'd like to do away with is the fact that >> instead of just parsing through the array until the user's input is >> finished, it's going to go all the way up to element 20, even if the >> user only enters two or three numbers. > > > There are two ways to deal with this, either have a value in the array > which means "i'm an endpoint so don't go past me" or you store the > number of valid entries somewhere. >[...] > An example of the second case would be having a data structure that > contained your array and had a size field.... so something like > struct mydata > { > int array[ 20 ]; > int numValidEntries; > } That looks interesting. I haven't covered structures yet so I'm not quite sure how that works, but I'm sure I'll understand it better once I get to that chapter :) > Also, make use of the C++ "//" style comments as (at least for me) its > less visual clutter than "/* ... */" everywhere. I know that all modern compilers will accept the C++ style comments in C code, but would an older compiler accept it as well? I doubt that anything I'd ever be doing would involve an old compiler, but still, I'd like to know that my code is portable. I actually do prefer '//' to '/*... */'. > Btw a "for" loop would probably be better than the "while" (did the book > cover "for" loops yet?) Yup, I've been introduced to for loops. I actually just didn't think about the fact that I could implement it here :-P Probably would be best... > Btw, potential issues in the code that can cause bugs: > * I don't see "position" incremented anywhere in your function... so is > it really looping through the array correctly? Yup, you're right. I forgot to place my position++; statement in there. I haven't debugged the function yet, so that'll save me some embarrasing frustration later ;) > * MaxValue starts at 0, so if your array was all negative numbers ( i.e. > { -1, -100, -60 } ), it would return 0 instead of the largest negative > (i.e. -1) True. Perhaps I could initialize the variable by instead assigning the first value of the array to MaxValue (MaxValue = array[0];) and then let it go on from there to find the largest number. James -- My blog: http://www.crazydrclaw.com/ My homepage: http://james.colannino.org/ "There are no uninteresting things; only uninterested people." --G.K. Chesterton From scrane at cornerkitchenpeddler.com Fri Jun 4 15:57:55 2004 From: scrane at cornerkitchenpeddler.com (Stephen Crane) Date: Fri, 4 Jun 2004 15:57:55 -0700 Subject: [OCLUG-devel] C++ suffixes Message-ID: <200406041557.55784.scrane@cornerkitchenpeddler.com> Hi Glad to see a programming list! I had a quick question on header files. What is the "standard" suffix in linux to use for C++ header files. Is it .h or .hpp? And what about for source files? .C or .cpp? Thanks, Stephen From james at colannino.org Fri Jun 4 15:55:30 2004 From: james at colannino.org (James Colannino) Date: Fri, 04 Jun 2004 15:55:30 -0700 Subject: [OCLUG-devel] C++ suffixes In-Reply-To: <200406041557.55784.scrane@cornerkitchenpeddler.com> References: <200406041557.55784.scrane@cornerkitchenpeddler.com> Message-ID: <40C0FDE2.3050508@colannino.org> Stephen Crane wrote: >Hi > >Glad to see a programming list! >I had a quick question on header files. What is the "standard" suffix in linux >to use for C++ header files. Is it .h or .hpp? And what about for source >files? .C or .cpp? > > I don't know about the headers, but I know that for C++ I usually see .cpp. James -- My blog: http://www.crazydrclaw.com/ My homepage: http://james.colannino.org/ "There are no uninteresting things; only uninterested people." --G.K. Chesterton From nick at socklabs.com Fri Jun 4 16:04:17 2004 From: nick at socklabs.com (Nick Gerakines) Date: Fri, 04 Jun 2004 16:04:17 -0700 Subject: [OCLUG-devel] C++ suffixes In-Reply-To: <200406041557.55784.scrane@cornerkitchenpeddler.com> References: <200406041557.55784.scrane@cornerkitchenpeddler.com> Message-ID: <40C0FFF1.3090008@socklabs.com> These seem to be pretty standard on most *nix projects. Along with make and configure. Headers: .h Sources: .c, .cpp Stephen Crane wrote: > Hi > > Glad to see a programming list! > I had a quick question on header files. What is the "standard" suffix in linux > to use for C++ header files. Is it .h or .hpp? And what about for source > files? .C or .cpp? > > Thanks, > Stephen > > _______________________________________________ > OCLUG-devel mailing list -- OCLUG-devel at oclug.org > http://www.oclug.org/mailman/listinfo/oclug-devel > > > -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 256 bytes Desc: OpenPGP digital signature Url : http://localhost.localdomain/pipermail/oclug-devel/attachments/20040604/f109a0d8/attachment.bin From x at xman.org Fri Jun 4 16:08:54 2004 From: x at xman.org (Christopher Smith) Date: Fri, 04 Jun 2004 16:08:54 -0700 Subject: [OCLUG-devel] C++ suffixes In-Reply-To: <200406041557.55784.scrane@cornerkitchenpeddler.com> References: <200406041557.55784.scrane@cornerkitchenpeddler.com> Message-ID: <1086390534.20210.1.camel@localhost> On Fri, 2004-06-04 at 15:57, Stephen Crane wrote: > Hi > > Glad to see a programming list! > I had a quick question on header files. What is the "standard" suffix in linux > to use for C++ header files. Is it .h or .hpp? And what about for source > files? .C or .cpp? There really is no standard. I've seen: .h .H .hpp .hcc .C .cc .cpp Oh, and one crazy person did: .h++ .c++ (you just know that screwed up a few shell scripts). --Chris From scrane at cornerkitchenpeddler.com Fri Jun 4 20:36:01 2004 From: scrane at cornerkitchenpeddler.com (Stephen Crane) Date: Fri, 4 Jun 2004 20:36:01 -0700 Subject: [OCLUG-devel] Where to put enum Message-ID: <200406042036.01588.scrane@cornerkitchenpeddler.com> Hi all, I have a slight difficulty. I am putting my class declarations in classes.h (is this correct?) I have an enum that I use in one of my classes: Position GetAdjCell (dir direction) const; //where dir is an enum Now when I #include the classes.h file gcc throws a fit since it doesn't know what enum means (#include is processed by the preprocessor). Would it be correct for me to put the enum at the top of my classes.h? What should go in my .h file? Thanks, Stephen From james at colannino.org Sat Jun 5 10:33:05 2004 From: james at colannino.org (James Colannino) Date: Sat, 05 Jun 2004 10:33:05 -0700 Subject: [OCLUG-devel] question about bitwise ~ Message-ID: <40C203D1.1040100@colannino.org> Hey everyone. I just have a simple question. I learned that the ~ operator inverts all the bits in an operand. So, for example, say that a byte (a character) contains 10000110. It would become 01111001. I've tried this on characters and integers. Both work fine. However, I tried this on a float, and I got the following error from the compiler: error: wrong type argument to bit-complement Does anyone know why this won't work on a floating point variable? It seems to me that since all data types are constructed of bits, this should work, although the book has been very basic so far, and I also haven't finished the chapter, so I may be missing something. James -- My blog: http://www.crazydrclaw.com/ My homepage: http://james.colannino.org/ "There are no uninteresting things; only uninterested people." --G.K. Chesterton From james at colannino.org Sun Jun 6 08:13:37 2004 From: james at colannino.org (James Colannino) Date: Sun, 06 Jun 2004 08:13:37 -0700 Subject: [OCLUG-devel] question about bitwise ~ In-Reply-To: <1086457413.6945.1.camel@localhost> References: <40C203D1.1040100@colannino.org> <1086457413.6945.1.camel@localhost> Message-ID: <40C334A1.5090709@colannino.org> Christopher Smith wrote: > On Sat, 2004-06-05 at 10:33, James Colannino wrote: > >>Hey everyone. I just have a simple question. I learned that the ~ >>operator inverts all the bits in an operand. So, for example, say that >>a byte (a character) contains 10000110. It would become 01111001. I've >>tried this on characters and integers. Both work fine. However, I >>tried this on a float, and I got the following error from the compiler: >> >>error: wrong type argument to bit-complement >> >>Does anyone know why this won't work on a floating point variable? It >>seems to me that since all data types are constructed of bits, this >>should work, although the book has been very basic so far, and I also >>haven't finished the chapter, so I may be missing something. > > > bitwise operators only work on integer types (characters are actually > integer types in C). Floating point numbers can have different > representations on different architectures, and performing the ~ on them > would likely leave you with nothing of value. So, the language is > designed to catch what is likely an error. Oh, ok. That makes sense. I finished the chapter today and the book didn't mention anything about that, so I'm glad you answered my question Otherwise I wouldn'tve known. Playing with individual bits is actually a lot of fun. It's a bit complicated for me, but as I re-read certain parts and practice it starts to get easier. There's just something about being able to manipulate data at the sub-byte level (if that's an acceptable term which I really find interesting. It really helps me to understand how things work. BTW, the first time I sent this message (yesterday), it didn't go through. My mail was held in my ISP's queue for over 4 hrs. and then was spit back to me because the oclug mail server timed out. James -- My blog: http://www.crazydrclaw.com/ My homepage: http://james.colannino.org/ "There are no uninteresting things; only uninterested people." --G.K. Chesterton From james at colannino.org Sat Jun 5 22:54:17 2004 From: james at colannino.org (James Colannino) Date: Sat, 05 Jun 2004 22:54:17 -0700 Subject: [OCLUG-devel] question about bitwise ~ In-Reply-To: <1086457413.6945.1.camel@localhost> References: <40C203D1.1040100@colannino.org> <1086457413.6945.1.camel@localhost> Message-ID: <40C2B189.7090204@colannino.org> Christopher Smith wrote: > On Sat, 2004-06-05 at 10:33, James Colannino wrote: > >>Hey everyone. I just have a simple question. I learned that the ~ >>operator inverts all the bits in an operand. So, for example, say that >>a byte (a character) contains 10000110. It would become 01111001. I've >>tried this on characters and integers. Both work fine. However, I >>tried this on a float, and I got the following error from the compiler: >> >>error: wrong type argument to bit-complement >> >>Does anyone know why this won't work on a floating point variable? It >>seems to me that since all data types are constructed of bits, this >>should work, although the book has been very basic so far, and I also >>haven't finished the chapter, so I may be missing something. > > > bitwise operators only work on integer types (characters are actually > integer types in C). Floating point numbers can have different > representations on different architectures, and performing the ~ on them > would likely leave you with nothing of value. So, the language is > designed to catch what is likely an error. Oh, ok. That makes sense. I finished the chapter today and the book didn't mention anything about that, so I'm glad you answered my question :) Otherwise I wouldn'tve known. Playing with individual bits is actually a lot of fun. It's a bit complicated for me, but as I re-read certain parts and practice it starts to get easier. There's just something about being able to manipulate data at the sub-byte level (if that's an acceptable term ;) which I really find interesting. It really helps me to understand how things work. James -- My blog: http://www.crazydrclaw.com/ My homepage: http://james.colannino.org/ "There are no uninteresting things; only uninterested people." --G.K. Chesterton From msimpson at braysimpson.com Sat Jun 5 20:01:13 2004 From: msimpson at braysimpson.com (Morgan Simpson) Date: Sat, 5 Jun 2004 20:01:13 -0700 Subject: [OCLUG-devel] question about bitwise ~ In-Reply-To: <40C203D1.1040100@colannino.org> References: <40C203D1.1040100@colannino.org> Message-ID: On Jun 5, 2004, at 10:33 AM, James Colannino wrote: > However, I tried this on a float, and I got the following error from > the compiler: > > error: wrong type argument to bit-complement > > Does anyone know why this won't work on a floating point variable? Bitwise operators require integer data types. A character is a kind of small integer. Regards, Morgan From x at xman.org Sat Jun 5 10:43:34 2004 From: x at xman.org (Christopher Smith) Date: Sat, 05 Jun 2004 10:43:34 -0700 Subject: [OCLUG-devel] question about bitwise ~ In-Reply-To: <40C203D1.1040100@colannino.org> References: <40C203D1.1040100@colannino.org> Message-ID: <1086457413.6945.1.camel@localhost> On Sat, 2004-06-05 at 10:33, James Colannino wrote: > Hey everyone. I just have a simple question. I learned that the ~ > operator inverts all the bits in an operand. So, for example, say that > a byte (a character) contains 10000110. It would become 01111001. I've > tried this on characters and integers. Both work fine. However, I > tried this on a float, and I got the following error from the compiler: > > error: wrong type argument to bit-complement > > Does anyone know why this won't work on a floating point variable? It > seems to me that since all data types are constructed of bits, this > should work, although the book has been very basic so far, and I also > haven't finished the chapter, so I may be missing something. bitwise operators only work on integer types (characters are actually integer types in C). Floating point numbers can have different representations on different architectures, and performing the ~ on them would likely leave you with nothing of value. So, the language is designed to catch what is likely an error. --Chris From x at xman.org Sat Jun 5 11:15:32 2004 From: x at xman.org (Christopher Smith) Date: Sat, 05 Jun 2004 11:15:32 -0700 Subject: [OCLUG-devel] Where to put enum In-Reply-To: <200406042036.01588.scrane@cornerkitchenpeddler.com> References: <200406042036.01588.scrane@cornerkitchenpeddler.com> Message-ID: <1086459332.6957.33.camel@localhost> On Fri, 2004-06-04 at 20:36, Stephen Crane wrote: > Hi all, > > I have a slight difficulty. I am putting my class declarations in classes.h > (is this correct?) I have an enum that I use in one of my classes: > Position GetAdjCell (dir direction) const; //where dir is an enum > > Now when I #include the classes.h file gcc throws a fit since it doesn't know > what enum means (#include is processed by the preprocessor). Would it be > correct for me to put the enum at the top of my classes.h? What should go in > my .h file? It sounds to me like your problem isn't where the enum is, but rather some kind of syntactical error in the enum line. FYI, if the enum is tied to the class, I encourage declaring it inside the class itself. --Chris From scrane at cornerkitchenpeddler.com Mon Jun 7 10:14:14 2004 From: scrane at cornerkitchenpeddler.com (Stephen Crane) Date: Mon, 7 Jun 2004 10:14:14 -0700 Subject: [OCLUG-devel] ICFP Contest 2004 Message-ID: <200406071014.14244.scrane@cornerkitchenpeddler.com> Hi all - A while back there was a message on the main oclug list about the ICFP contest (http://www.cis.upenn.edu/group/proj/plclub/contest/) Well I attempted to enter (ran out of time, though). Attached is my simulator. It reads in a world file and two ant finite-state-machine files, runs them, and dumps the board for each round to STDOUT (I would suggest using a > pipe). Wanted to know what you thought of my code. This is the first big project I've ever done in C++. I just started learning C++ exactly one week ago (last monday), so my code probably isn't the best. Thanks, Stephen -------------- next part -------------- A non-text attachment was scrubbed... Name: ant_simulator.tar.gz Type: application/x-tgz Size: 28155 bytes Desc: not available Url : http://localhost.localdomain/pipermail/oclug-devel/attachments/20040607/ec31721e/attachment.bin From x at xman.org Mon Jun 7 10:31:17 2004 From: x at xman.org (Christopher Smith) Date: Mon, 07 Jun 2004 10:31:17 -0700 Subject: [OCLUG-devel] ICFP Contest 2004 In-Reply-To: <200406071014.14244.scrane@cornerkitchenpeddler.com> References: <200406071014.14244.scrane@cornerkitchenpeddler.com> Message-ID: <1086629477.6336.22.camel@localhost> On Mon, 2004-06-07 at 10:14, Stephen Crane wrote: > Hi all - > > A while back there was a message on the main oclug list about the ICFP contest > (http://www.cis.upenn.edu/group/proj/plclub/contest/) > Well I attempted to enter (ran out of time, though). Attached is my simulator. > It reads in a world file and two ant finite-state-machine files, runs them, > and dumps the board for each round to STDOUT (I would suggest using a > > pipe). > > Wanted to know what you thought of my code. This is the first big project I've > ever done in C++. I just started learning C++ exactly one week ago (last > monday), so my code probably isn't the best. Yeah, I'll give you credit just for getting things to compile given the time frame you've been learning in. I didn't have a chance to look at the code in detail, but having skimmed it, I'll point out a few different areas for improvement: 1) Be suspicious of switch statements. Most of the time that one is using a switch statement, one could instead use polymorphism or overloading to address the problem, creating better separation between the different bits of behavior. 2) Be suspicious of long methods, particularly constructors. Constructors should have only one job: constructing an object. Other behavior should either be done after construction or at least in separate methods. 3) Think about using the containers and algorithms in the STL. I'm sure you haven't had time to learn about how to use these suckers, but it is definitely a good thing for you to do so. 4) Why didn't you stick with an ENUM for your directions? 5) I think in a number of cases their might have been benefits to making some of your enums actually real objects/classes, and putting some of the behavior in your switch statements inside those objects. -- Christopher Smith From scrane at cornerkitchenpeddler.com Mon Jun 7 10:56:21 2004 From: scrane at cornerkitchenpeddler.com (Stephen Crane) Date: Mon, 7 Jun 2004 10:56:21 -0700 Subject: [OCLUG-devel] ICFP Contest 2004 In-Reply-To: <1086629477.6336.22.camel@localhost> References: <200406071014.14244.scrane@cornerkitchenpeddler.com> <1086629477.6336.22.camel@localhost> Message-ID: <200406071056.21209.scrane@cornerkitchenpeddler.com> On Monday, June 07, 2004 10:31 am, you wrote: > On Mon, 2004-06-07 at 10:14, Stephen Crane wrote: > > Hi all - > > > > A while back there was a message on the main oclug list about the ICFP > > contest (http://www.cis.upenn.edu/group/proj/plclub/contest/) > > Well I attempted to enter (ran out of time, though). Attached is my > > simulator. It reads in a world file and two ant finite-state-machine > > files, runs them, and dumps the board for each round to STDOUT (I would > > suggest using a > pipe). > > > > Wanted to know what you thought of my code. This is the first big project > > I've ever done in C++. I just started learning C++ exactly one week ago > > (last monday), so my code probably isn't the best. > > Yeah, I'll give you credit just for getting things to compile given the > time frame you've been learning in. > > I didn't have a chance to look at the code in detail, but having skimmed > it, I'll point out a few different areas for improvement: > > 1) Be suspicious of switch statements. Most of the time that one is > using a switch statement, one could instead use polymorphism or > overloading to address the problem, creating better separation between > the different bits of behavior. Couldn't figure out how to do that. Plus the description, in the task, of the simulator was in a pseudo-language and used switch, so to make things wuick and easy I used the same for the most part. > 2) Be suspicious of long methods, particularly constructors. > Constructors should have only one job: constructing an object. Other > behavior should either be done after construction or at least in > separate methods. Yeah, they were getting pretty long.... I'll have to look at that again. > 3) Think about using the containers and algorithms in the STL. I'm sure > you haven't had time to learn about how to use these suckers, but it is > definitely a good thing for you to do so. Well, I started Teach Yourself c++ in 24 hours on monday, but only got to chapter 15 out of 24 ;-) I'll learn about those in the near future. > 4) Why didn't you stick with an ENUM for your directions? Simple answer: the function Ant::Turn I needed to manipulate the direction mathematically and the c++ strict typing wouldn't let me. > 5) I think in a number of cases their might have been benefits to making > some of your enums actually real objects/classes, and putting some of > the behavior in your switch statements inside those objects. I'll look into that. I couldn't think that hard after about 15 hours of work ;-) Thanks for the input. Stephen From scrane at cornerkitchenpeddler.com Wed Jun 9 14:02:27 2004 From: scrane at cornerkitchenpeddler.com (Stephen Crane) Date: Wed, 9 Jun 2004 14:02:27 -0700 Subject: [OCLUG-devel] ICFP Contest 2004 In-Reply-To: <200406071014.14244.scrane@cornerkitchenpeddler.com> References: <200406071014.14244.scrane@cornerkitchenpeddler.com> Message-ID: <200406091402.27071.scrane@cornerkitchenpeddler.com> On Monday, June 07, 2004 10:14 am, Stephen Crane wrote: > Hi all - > > A while back there was a message on the main oclug list about the ICFP > contest (http://www.cis.upenn.edu/group/proj/plclub/contest/) > Well I attempted to enter (ran out of time, though). Attached is my > simulator. It reads in a world file and two ant finite-state-machine files, > runs them, and dumps the board for each round to STDOUT (I would suggest > using a > pipe). > > Wanted to know what you thought of my code. This is the first big project > I've ever done in C++. I just started learning C++ exactly one week ago > (last monday), so my code probably isn't the best. > > Thanks, > Stephen I'm trying to understand something in the g++ compiler. If I turn on the optimization (-O1) I get a SEGFAULT almost immediatly! Any clue why this would happen? (I'm using the code attached to my original post) Thanks, Stephen From x at xman.org Wed Jun 9 14:01:36 2004 From: x at xman.org (Christopher Smith) Date: Wed, 09 Jun 2004 14:01:36 -0700 Subject: [OCLUG-devel] ICFP Contest 2004 In-Reply-To: <200406091402.27071.scrane@cornerkitchenpeddler.com> References: <200406071014.14244.scrane@cornerkitchenpeddler.com> <200406091402.27071.scrane@cornerkitchenpeddler.com> Message-ID: <1086814896.6969.0.camel@smithch-ws.research.p4pnet.net> On Wed, 2004-06-09 at 14:02, Stephen Crane wrote: > I'm trying to understand something in the g++ compiler. If I turn on the > optimization (-O1) I get a SEGFAULT almost immediatly! Any clue why this > would happen? (I'm using the code attached to my original post) Is it your code that is SEGFAULTing, or the compiler? -- Christopher Smith From scrane at cornerkitchenpeddler.com Wed Jun 9 15:17:08 2004 From: scrane at cornerkitchenpeddler.com (Stephen Crane) Date: Wed, 9 Jun 2004 15:17:08 -0700 Subject: [OCLUG-devel] ICFP Contest 2004 In-Reply-To: <1086814896.6969.0.camel@smithch-ws.research.p4pnet.net> References: <200406071014.14244.scrane@cornerkitchenpeddler.com> <200406091402.27071.scrane@cornerkitchenpeddler.com> <1086814896.6969.0.camel@smithch-ws.research.p4pnet.net> Message-ID: <200406091517.08502.scrane@cornerkitchenpeddler.com> On Wednesday, June 09, 2004 02:01 pm, you wrote: > On Wed, 2004-06-09 at 14:02, Stephen Crane wrote: > > I'm trying to understand something in the g++ compiler. If I turn on the > > optimization (-O1) I get a SEGFAULT almost immediatly! Any clue why this > > would happen? (I'm using the code attached to my original post) > > Is it your code that is SEGFAULTing, or the compiler? Well, I tried it again and surprise, it worked. My code was segfaulting. For some reason when I used the compiler in eclipse/CDT which used: g++ -O3 -Wall -c -o simulator.o ../simulator.cpp g++ -o ant_simulator simulator.o Or: g++ -O0 -Wall -c -o simulator.o ../simulator.cpp g++ -o ant_simulator simulator.o it wouldn't work, but if I used: g++ -Wall -o ant_simulator simulator It would work. Both commands apparently work now, don't know why. Any reason why? Thanks, Stephen From x at xman.org Wed Jun 9 15:23:55 2004 From: x at xman.org (Christopher Smith) Date: Wed, 09 Jun 2004 15:23:55 -0700 Subject: [OCLUG-devel] ICFP Contest 2004 In-Reply-To: <200406091517.08502.scrane@cornerkitchenpeddler.com> References: <200406071014.14244.scrane@cornerkitchenpeddler.com> <200406091402.27071.scrane@cornerkitchenpeddler.com> <1086814896.6969.0.camel@smithch-ws.research.p4pnet.net> <200406091517.08502.scrane@cornerkitchenpeddler.com> Message-ID: <1086819835.7267.1.camel@smithch-ws.research.p4pnet.net> On Wed, 2004-06-09 at 15:17, Stephen Crane wrote: > On Wednesday, June 09, 2004 02:01 pm, you wrote: > > On Wed, 2004-06-09 at 14:02, Stephen Crane wrote: > > > I'm trying to understand something in the g++ compiler. If I turn on the > > > optimization (-O1) I get a SEGFAULT almost immediatly! Any clue why this > > > would happen? (I'm using the code attached to my original post) > > > > Is it your code that is SEGFAULTing, or the compiler? > > Well, I tried it again and surprise, it worked. My code was segfaulting. For > some reason when I used the compiler in eclipse/CDT which used: > g++ -O3 -Wall -c -o simulator.o ../simulator.cpp > g++ -o ant_simulator simulator.o > Or: > g++ -O0 -Wall -c -o simulator.o ../simulator.cpp > g++ -o ant_simulator simulator.o > it wouldn't work, but if I used: > g++ -Wall -o ant_simulator simulator > It would work. Both commands apparently work now, don't know why. Any reason > why? I usually start blaming the hardware if it's the compiler segfaulting, but if it's your code, I'd use ulimit to make sure you get core dumps and start looking at what's inside them. It's not unusual for optimizations to expose bugs that were previously hidden. -- Christopher Smith From scrane at cornerkitchenpeddler.com Wed Jun 9 15:40:37 2004 From: scrane at cornerkitchenpeddler.com (Stephen Crane) Date: Wed, 9 Jun 2004 15:40:37 -0700 Subject: [OCLUG-devel] ICFP Contest 2004 In-Reply-To: <1086819835.7267.1.camel@smithch-ws.research.p4pnet.net> References: <200406071014.14244.scrane@cornerkitchenpeddler.com> <200406091517.08502.scrane@cornerkitchenpeddler.com> <1086819835.7267.1.camel@smithch-ws.research.p4pnet.net> Message-ID: <200406091540.37872.scrane@cornerkitchenpeddler.com> On Wednesday, June 09, 2004 03:23 pm, you wrote: > On Wed, 2004-06-09 at 15:17, Stephen Crane wrote: > > On Wednesday, June 09, 2004 02:01 pm, you wrote: > > > On Wed, 2004-06-09 at 14:02, Stephen Crane wrote: > > > > I'm trying to understand something in the g++ compiler. If I turn on > > > > the optimization (-O1) I get a SEGFAULT almost immediatly! Any clue > > > > why this would happen? (I'm using the code attached to my original > > > > post) > > > > > > Is it your code that is SEGFAULTing, or the compiler? > > > > Well, I tried it again and surprise, it worked. My code was segfaulting. > > For some reason when I used the compiler in eclipse/CDT which used: > > g++ -O3 -Wall -c -o simulator.o ../simulator.cpp > > g++ -o ant_simulator simulator.o > > Or: > > g++ -O0 -Wall -c -o simulator.o ../simulator.cpp > > g++ -o ant_simulator simulator.o > > it wouldn't work, but if I used: > > g++ -Wall -o ant_simulator simulator > > It would work. Both commands apparently work now, don't know why. Any > > reason why? > > I usually start blaming the hardware if it's the compiler segfaulting, > but if it's your code, I'd use ulimit to make sure you get core dumps > and start looking at what's inside them. I'm not familiar with ulimit. What is it? I'm kinda new to *nix programming, so sorry if this is a dumb question. > It's not unusual for optimizations to expose bugs that were previously > hidden. From x at xman.org Wed Jun 9 15:53:34 2004 From: x at xman.org (Christopher Smith) Date: Wed, 09 Jun 2004 15:53:34 -0700 Subject: [OCLUG-devel] ICFP Contest 2004 In-Reply-To: <200406091540.37872.scrane@cornerkitchenpeddler.com> References: <200406071014.14244.scrane@cornerkitchenpeddler.com> <200406091517.08502.scrane@cornerkitchenpeddler.com> <1086819835.7267.1.camel@smithch-ws.research.p4pnet.net> <200406091540.37872.scrane@cornerkitchenpeddler.com> Message-ID: <1086821614.7267.8.camel@smithch-ws.research.p4pnet.net> On Wed, 2004-06-09 at 15:40, Stephen Crane wrote: > I'm not familiar with ulimit. What is it? I'm kinda new to *nix programming, > so sorry if this is a dumb question. Ah, then you probably are not familiar with core files (you are going to love those!). First of all, when in doubt, check the man page. Of course in this case the ulimit man page will only be somewhat enlightening. What you really want is the "bash" man page (search for ulimit). ulimit allows you to set limits on processes/users. If you do "ulimit -a" it displays all the settings. One of them is "maximum core files". Since core files are normally only a concern to developers, most distros these days set the limit on core file size to 0, effectively removing them. You'll want to change this. In the Unix world, when a process dies for an odd reason, it dumps the contents of it's memory to the filesystem, so that a developer can basically look at a snapshot of the process at the time it crashed. This comes in real handy for post-mortem debugging. To load up a core file in gdb, you typicall invoke it with the following syntax: gdb -- Christopher Smith From scrane at cornerkitchenpeddler.com Wed Jun 9 16:11:12 2004 From: scrane at cornerkitchenpeddler.com (Stephen Crane) Date: Wed, 9 Jun 2004 16:11:12 -0700 Subject: [OCLUG-devel] ICFP Contest 2004 In-Reply-To: <1086821614.7267.8.camel@smithch-ws.research.p4pnet.net> References: <200406071014.14244.scrane@cornerkitchenpeddler.com> <200406091540.37872.scrane@cornerkitchenpeddler.com> <1086821614.7267.8.camel@smithch-ws.research.p4pnet.net> Message-ID: <200406091611.12962.scrane@cornerkitchenpeddler.com> On Wednesday, June 09, 2004 03:53 pm, you wrote: > On Wed, 2004-06-09 at 15:40, Stephen Crane wrote: > > I'm not familiar with ulimit. What is it? I'm kinda new to *nix > > programming, so sorry if this is a dumb question. > > Ah, then you probably are not familiar with core files (you are going to > love those!). First of all, when in doubt, check the man page. Of course > in this case the ulimit man page will only be somewhat enlightening. > What you really want is the "bash" man page (search for ulimit). > > ulimit allows you to set limits on processes/users. If you do "ulimit > -a" it displays all the settings. One of them is "maximum core files". > Since core files are normally only a concern to developers, most distros > these days set the limit on core file size to 0, effectively removing > them. You'll want to change this. > > In the Unix world, when a process dies for an odd reason, it dumps the > contents of it's memory to the filesystem, so that a developer can > basically look at a snapshot of the process at the time it crashed. This > comes in real handy for post-mortem debugging. > > To load up a core file in gdb, you typicall invoke it with the following > syntax: > > gdb Very cool. What would you suggest for my max core file size? Is unlimited the best for debugging? And where do I find these dumps? I looked at the bash man page and found the section on ulimit, I just can't find much on core dumps in there ;-) Thanks for the tip, Stephen From x at xman.org Wed Jun 9 16:06:34 2004 From: x at xman.org (Christopher Smith) Date: Wed, 09 Jun 2004 16:06:34 -0700 Subject: [OCLUG-devel] ICFP Contest 2004 In-Reply-To: <200406091611.12962.scrane@cornerkitchenpeddler.com> References: <200406071014.14244.scrane@cornerkitchenpeddler.com> <200406091540.37872.scrane@cornerkitchenpeddler.com> <1086821614.7267.8.camel@smithch-ws.research.p4pnet.net> <200406091611.12962.scrane@cornerkitchenpeddler.com> Message-ID: <1086822394.7267.11.camel@smithch-ws.research.p4pnet.net> On Wed, 2004-06-09 at 16:11, Stephen Crane wrote: > Very cool. What would you suggest for my max core file size? Is unlimited the > best for debugging? And where do I find these dumps? I looked at the bash man > page and found the section on ulimit, I just can't find much on core dumps in > there ;-) Well, the main thing you want to avoid is a) a core dump which takes forever, or b) a core dump which consumes all disk space. You won't have too much problem with b) these days, but a) is definitely something to consider. I usually set it to like 100MB for debugging simple programs, since they never get near that large unless something has gone horribly wrong. -- Christopher Smith From james at colannino.org Sat Jun 19 17:33:48 2004 From: james at colannino.org (James Colannino) Date: Sat, 19 Jun 2004 17:33:48 -0700 Subject: [OCLUG-devel] determining the size of data types Message-ID: <40D4DB6C.2090201@colannino.org> Hey everyone. I was wondering what I could do to determine the size of certain data types on my architecture. For example, I want to be able to determine how many bytes are assigned to unsigned short integers as well as what the numerical range is. Is there some simple code that I can write to test this? I actually don't really need to know at this point how many bytes are assigned to each, but I do need to be able to know the numerical range of any given type. Thanks in advance. James -- My blog: http://www.crazydrclaw.com/ My homepage: http://james.colannino.org/ "There are no uninteresting things; only uninterested people." --G.K. Chesterton From james at colannino.org Sat Jun 19 18:19:47 2004 From: james at colannino.org (James Colannino) Date: Sat, 19 Jun 2004 18:19:47 -0700 Subject: [OCLUG-devel] declaring structures outside of main() Message-ID: <40D4E633.1020306@colannino.org> This is really weird and I was hoping that someone could shed some light on this. The following code will be condensed from its original version for clarity. Anyway, I have the following: #include const int MAX_COUNT = 6; struct type { [...variables...] }; struct type variable_name[MAX_COUNT]; int main() { [...] return 0; } When I do this, I get the following error from gcc: error: variable-size type declared outside of any function However, when I try struct type variable[x] (x is any number) instead of struct type variable[MAX_COUNT], it compiles successfully. I want this to be a global structure. I have no idea what's going on :( Anyone know? I know that it has to be something I'm doing, but it seems weird that I can declare it with an explicit number outside of any function but not when I'm using a constant. James -- My blog: http://www.crazydrclaw.com/ My homepage: http://james.colannino.org/ "There are no uninteresting things; only uninterested people." --G.K. Chesterton From james at colannino.org Sat Jun 19 18:32:10 2004 From: james at colannino.org (James Colannino) Date: Sat, 19 Jun 2004 18:32:10 -0700 Subject: [OCLUG-devel] one more question about structures... Message-ID: <40D4E91A.5050302@colannino.org> Ok, I have one more question about structures... I hope that these three messages I've sent to the group in the past 30 minutes or so haven't gotten annoying. I haven't seen any activity on the oclug-devel list lately, so I'm hoping that at least people will be happy that there's some traffic :-P This isn't about anything going wrong. I'm just curious about a benign compiler warning that I'm getting. Consider the following code: #include int main() { struct time { unsigned short int hour; unsigned short int min; }; struct date_time { unsigned short int month; /* 1-12 */ unsigned short int day; /* 1-31 */ unsigned short int year; struct time time_of_day; } date; date.time_of_day.hour = 1; return 0; } Whenever I assign a value to a nested structure, I get the following warning from the compiler: warning: declaration does not declare anything However, when I run my program (I also have a printf() in there to test that the value has been assigned), it shows me that it indeed was declared. I'm confused... By the way, I know that I could just do away with the nested structure and have the hour and min variables be a part of the first structure, thereby eliminating unnecessary confusion, but I just did this to serve as an example. James -- My blog: http://www.crazydrclaw.com/ My homepage: http://james.colannino.org/ "There are no uninteresting things; only uninterested people." --G.K. Chesterton From x at xman.org Sat Jun 19 18:45:10 2004 From: x at xman.org (Christopher Smith) Date: Sat, 19 Jun 2004 18:45:10 -0700 Subject: [OCLUG-devel] determining the size of data types In-Reply-To: <40D4DB6C.2090201@colannino.org> References: <40D4DB6C.2090201@colannino.org> Message-ID: <1087695909.6285.0.camel@localhost> On Sat, 2004-06-19 at 17:33, James Colannino wrote: > Hey everyone. I was wondering what I could do to determine the size of > certain data types on my architecture. For example, I want to be able > to determine how many bytes are assigned to unsigned short integers as > well as what the numerical range is. Is there some simple code that I > can write to test this? I actually don't really need to know at this > point how many bytes are assigned to each, but I do need to be able to > know the numerical range of any given type. Thanks in advance. I assume this is still C/C++ programming, so then you'd want to look at sizeof(). -- Christopher Smith From x at xman.org Sat Jun 19 18:49:09 2004 From: x at xman.org (Christopher Smith) Date: Sat, 19 Jun 2004 18:49:09 -0700 Subject: [OCLUG-devel] one more question about structures... In-Reply-To: <40D4E91A.5050302@colannino.org> References: <40D4E91A.5050302@colannino.org> Message-ID: <1087696149.6285.2.camel@localhost> On Sat, 2004-06-19 at 18:32, James Colannino wrote: > Ok, I have one more question about structures... I hope that these > three messages I've sent to the group in the past 30 minutes or so > haven't gotten annoying. I haven't seen any activity on the oclug-devel > list lately, so I'm hoping that at least people will be happy that > there's some traffic :-P > > This isn't about anything going wrong. I'm just curious about a benign > compiler warning that I'm getting. Consider the following code: > > #include > > int main() > > { > struct time { > unsigned short int hour; > unsigned short int min; > }; > > struct date_time { > unsigned short int month; /* 1-12 */ > unsigned short int day; /* 1-31 */ > unsigned short int year; > > struct time time_of_day; > } date; > > date.time_of_day.hour = 1; > > return 0; > } > > Whenever I assign a value to a nested structure, I get the following > warning from the compiler: > > warning: declaration does not declare anything Hmm... shouldn't it be: typedef struct date_time { /* stuff */ } date; Does that make the compiler warning go away? -- Christopher Smith From x at xman.org Sat Jun 19 19:00:39 2004 From: x at xman.org (Christopher Smith) Date: Sat, 19 Jun 2004 19:00:39 -0700 Subject: [OCLUG-devel] declaring structures outside of main() In-Reply-To: <40D4E633.1020306@colannino.org> References: <40D4E633.1020306@colannino.org> Message-ID: <1087696838.6285.6.camel@localhost> On Sat, 2004-06-19 at 18:19, James Colannino wrote: > This is really weird and I was hoping that someone could shed some light > on this. In general, variable sized arrays are funky business in C. C99 addresses this, but it's not well implemented in a lot of compilers. In gcc's case, I know it can use alloca to make variable sized arrays when you are inside a block (function or otherwise), but when you have a global, that won't work. My advice: allocate the array with malloc and/or use #define for your constant. -- Christopher Smith From james at colannino.org Sat Jun 19 19:12:39 2004 From: james at colannino.org (James Colannino) Date: Sat, 19 Jun 2004 19:12:39 -0700 Subject: [OCLUG-devel] one more question about structures... In-Reply-To: <1087696149.6285.2.camel@localhost> References: <40D4E91A.5050302@colannino.org> <1087696149.6285.2.camel@localhost> Message-ID: <40D4F297.7020805@colannino.org> Christopher Smith wrote: > On Sat, 2004-06-19 at 18:32, James Colannino wrote: > >>Ok, I have one more question about structures... I hope that these >>three messages I've sent to the group in the past 30 minutes or so >>haven't gotten annoying. I haven't seen any activity on the oclug-devel >>list lately, so I'm hoping that at least people will be happy that >>there's some traffic :-P >> >>This isn't about anything going wrong. I'm just curious about a benign >>compiler warning that I'm getting. Consider the following code: >> >>#include >> >>int main() >> >>{ >> struct time { >> unsigned short int hour; >> unsigned short int min; >> }; >> >> struct date_time { >> unsigned short int month; /* 1-12 */ >> unsigned short int day; /* 1-31 */ >> unsigned short int year; >> >> struct time time_of_day; >> } date; >> >> date.time_of_day.hour = 1; >> >> return 0; >>} >> >>Whenever I assign a value to a nested structure, I get the following >>warning from the compiler: >> >>warning: declaration does not declare anything > > > Hmm... shouldn't it be: > > typedef struct date_time { > /* stuff */ > } date; > > Does that make the compiler warning go away? Nope, that makes it compile with an error instead :-P What's weird is that the code I have above compiles with a warning on my laptop (but it still works), but not on my desktop machine. Weird... James -- My blog: http://www.crazydrclaw.com/ My homepage: http://james.colannino.org/ "There are no uninteresting things; only uninterested people." --G.K. Chesterton From james at colannino.org Sat Jun 19 19:17:30 2004 From: james at colannino.org (James Colannino) Date: Sat, 19 Jun 2004 19:17:30 -0700 Subject: [OCLUG-devel] determining the size of data types In-Reply-To: <1087695909.6285.0.camel@localhost> References: <40D4DB6C.2090201@colannino.org> <1087695909.6285.0.camel@localhost> Message-ID: <40D4F3BA.7010008@colannino.org> Christopher Smith wrote: > I assume this is still C/C++ programming, so then you'd want to look at > sizeof(). Yes, sorry, I should have specified the language... As for telling me about sizeof(), thank you. Now I can see how many bytes are used for each data type. Is there also code that I can write that will also determine the numerical range that I can get with each type? I.E. with a signed integer I can get 0 to whatever the maximum value is. Thanks again :) James -- My blog: http://www.crazydrclaw.com/ My homepage: http://james.colannino.org/ "There are no uninteresting things; only uninterested people." --G.K. Chesterton From james at colannino.org Sat Jun 19 19:20:41 2004 From: james at colannino.org (James Colannino) Date: Sat, 19 Jun 2004 19:20:41 -0700 Subject: [OCLUG-devel] declaring structures outside of main() In-Reply-To: <1087696838.6285.6.camel@localhost> References: <40D4E633.1020306@colannino.org> <1087696838.6285.6.camel@localhost> Message-ID: <40D4F479.5060304@colannino.org> Christopher Smith wrote: > On Sat, 2004-06-19 at 18:19, James Colannino wrote: > >>This is really weird and I was hoping that someone could shed some light >>on this. > > > In general, variable sized arrays are funky business in C. C99 addresses > this, but it's not well implemented in a lot of compilers. In gcc's > case, I know it can use alloca to make variable sized arrays when you > are inside a block (function or otherwise), but when you have a global, > that won't work. > > My advice: allocate the array with malloc and/or use #define for your > constant. Ah, I see. I was wondering if there was just some type of bug in my programming that I couldn't find or if it was actually an issue with the compiler itself. I'm glad it wasn't me :-P I'll probably just go ahead and use #define for this particular circumstance. James -- My blog: http://www.crazydrclaw.com/ My homepage: http://james.colannino.org/ "There are no uninteresting things; only uninterested people." --G.K. Chesterton From james at colannino.org Sat Jun 19 19:22:11 2004 From: james at colannino.org (James Colannino) Date: Sat, 19 Jun 2004 19:22:11 -0700 Subject: [OCLUG-devel] determining the size of data types In-Reply-To: <40D4F3BA.7010008@colannino.org> References: <40D4DB6C.2090201@colannino.org> <1087695909.6285.0.camel@localhost> <40D4F3BA.7010008@colannino.org> Message-ID: <40D4F4D3.7090507@colannino.org> James Colannino wrote: > I.E. with a signed integer I can get 0 to whatever the maximum value is[...] Oops...I meant unsigned int... James -- My blog: http://www.crazydrclaw.com/ My homepage: http://james.colannino.org/ "There are no uninteresting things; only uninterested people." --G.K. Chesterton From x at xman.org Sat Jun 19 19:53:53 2004 From: x at xman.org (Christopher Smith) Date: Sat, 19 Jun 2004 19:53:53 -0700 Subject: [OCLUG-devel] determining the size of data types In-Reply-To: <40D4F4D3.7090507@colannino.org> References: <40D4DB6C.2090201@colannino.org> <1087695909.6285.0.camel@localhost> <40D4F3BA.7010008@colannino.org> <40D4F4D3.7090507@colannino.org> Message-ID: <1087700033.6285.8.camel@localhost> On Sat, 2004-06-19 at 19:22, James Colannino wrote: > James Colannino wrote: > > > I.E. with a signed integer I can get 0 to whatever the maximum value is[...] > > Oops...I meant unsigned int... #include That'll give you what you want. -- Christopher Smith From tthelin at sbcglobal.net Sun Jun 20 21:32:04 2004 From: tthelin at sbcglobal.net (Tim Thelin) Date: Sun, 20 Jun 2004 21:32:04 -0700 Subject: [OCLUG-devel] one more question about structures... In-Reply-To: <40D4F297.7020805@colannino.org> References: <40D4E91A.5050302@colannino.org> <1087696149.6285.2.camel@localhost> <40D4F297.7020805@colannino.org> Message-ID: <40D664C4.6020904@sbcglobal.net> James Colannino wrote: > Christopher Smith wrote: > >> On Sat, 2004-06-19 at 18:32, James Colannino wrote: >> >>> Ok, I have one more question about structures... I hope that these >>> three messages I've sent to the group in the past 30 minutes or so >>> haven't gotten annoying. I haven't seen any activity on the >>> oclug-devel list lately, so I'm hoping that at least people will be >>> happy that there's some traffic :-P >>> >>> This isn't about anything going wrong. I'm just curious about a >>> benign compiler warning that I'm getting. Consider the following code: >>> >>> #include >>> >>> int main() >>> >>> { >>> struct time { >>> unsigned short int hour; >>> unsigned short int min; >>> }; >>> >>> struct date_time { >>> unsigned short int month; /* 1-12 */ >>> unsigned short int day; /* 1-31 */ >>> unsigned short int year; >>> >>> struct time time_of_day; >>> } date; >>> >>> date.time_of_day.hour = 1; >>> >>> return 0; >>> } >>> >>> Whenever I assign a value to a nested structure, I get the following >>> warning from the compiler: >>> >>> warning: declaration does not declare anything >> >> >> >> Hmm... shouldn't it be: >> >> typedef struct date_time { >> /* stuff */ >> } date; >> >> Does that make the compiler warning go away? > > > Nope, that makes it compile with an error instead :-P What's weird is > that the code I have above compiles with a warning on my laptop (but > it still works), but not on my desktop machine. Weird... > > James If you move your declaration of date to outside main(), does it compile alright? It looks like your trying to declare an inline structure without declaring the variable that will use the inline structure. Or if your only going to use the structure once, try this: struct date_time { unsigned short int month; /* 1-12 */ unsigned short int day; /* 1-31 */ unsigned short int year; struct { unsigned short int hour; unsigned short int min; }time_of_day; } date; Btw, which line exactly is the warning occuring on? Thanks, Tim Thelin