RE: Calling sequence
Very interesting problem, and a more complete answer would be j = 6, i = 4, and this is why: i = 2; j = i++ + ++i; Obviously, the j line is more interesting, so we'll talk about sequence of operations. The preincrement operator is done before everything else, so "++i" is done, giving i the value of 3, even before the addition, so you end up with "j = 3++ + 3" which equals 6. After the line is completed, i is incremented, causing i to be 3++ = 4. ~Patrick On my compiler it seems to do this: i = 2; j = i++ + ++i; It does the i = 2 just fine. So at the start of the j line i = 2. Now, it does i + ++i, and *then* it increments i the second time. So it becomes (2 + ++i)++; Actually I just tried it with a couple different compilers. g++ and gcc give me 5 for j while CC and cc both give me 7 for j. I assume that the GNU compilers are incrementing the i++ after then assignment is done, while the CC compilers are incrementing i++ before the addition begins. --Joe
RE: Calling sequence
Here is the ouput on our SGI server running IRIX 6.5 #include stdio.h main() { int i=2; int j = i++ + ++i; printf("%d %d\n",j,i); } Now, you think that j would be 7 and i would be four right? Here it is with g++/gcc (both give the same output): elvis 586% g++ b.c elvis 587% a.out 5 3 And now with the CC type compilers from SGI: elvis 588% CC b.c elvis 589% a.out 7 4 I'll have to take a look at the rest of what you said because something seems a little off, but this behaviour is totally wrong -- while the CC compilers are incrementing i++ before the addition begins. This is completely against the C standard, which means the post-increment is done after the assignment. If it does the increment before the assignment, it is a preincrement (++i). The reason it seems there is a preincrement on the first i is because the second i is preincremented. It doesn't surprise me that it may be a compiler- dependant thing on whether or not the first i sees the preincrement. On my gcc, both i's see the preincrement, and then i is postincremented due to the "i++". To show that this is the case, do the following (again probably compiler-dependant): +snip #include stdio.h void main(void) { int i=2; int p= ++i + ++i; printf("p=%d, i=%d\n", p, i); } +snip You still get the expected i = 4, but p is now 8 because i is preincremented twice (to 4) and then added together (for 8). All of this stems from both the compiler, OS, and machine used. It depends upon whether or not local copies of i are made for the addition, when the values are read from memory, etc. However, the values I have given are from linux gcc on an x86, and are what is expected from ANSI C. Pre-increment is done before everything, then addition, then assignment, then postincrement. On a UltraSparc w/ SunOS 5.5.1, you get j=7, i=4. This is because Sparc has local registers for the addition. Not to start a holy war, but I feel this is a bug in the hardware in this case. Sparc should not have cached the value of i after the first increment because it results in i == 3 == 4 when the final addition is made to assign to j. ~Patrick From: Joseph Keen Very interesting problem, and a more complete answer would be j = 6, i = 4, and this is why: i = 2; j = i++ + ++i; Obviously, the j line is more interesting, so we'll talk about sequence of operations. The preincrement operator is done before everything else, so "++i" is done, giving i the value of 3, even before the addition, so you end up with "j = 3++ + 3" which equals 6. After the line is completed, i is incremented, causing i to be 3++ = 4. ~Patrick On my compiler it seems to do this: i = 2; j = i++ + ++i; It does the i = 2 just fine. So at the start of the j line i = 2. Now, it does i + ++i, and *then* it increments i the second time. So it becomes (2 + ++i)++; Actually I just tried it with a couple different compilers. g++ and gcc give me 5 for j while CC and cc both give me 7 for j. I assume that the GNU compilers are incrementing the i++ after then assignment is done, while the CC compilers are incrementing i++ before the addition begins. --Joe
RE: Calling sequence
Yeah, I asked a few professors here at school about it and they said that it's a fundamentaly undefined statement. Also, I've been meaning to ask you what the perl script at the bottom of your emails does. I beleive statements with multiple side effects are undefined in ISO C standard and that is why everyone is getting all sorts of weird answers, but I may be wrong. Kevin ... [EMAIL PROTECTED] `:::' Kevin Sivits ... .. ::: * `::.::' ::: .:: .:.::. .:: .:: `::. :' ::: :: :: :: :: :::::. ::: .::. .:: ::. `. .:' ::. ..:::.::' .. #!/bin/perl -sp0777iX+d*lMLa^*lN%0]dsXx++lMlN/dsM0j]dsj $/=unpack('H*',$_);$_=`echo 16dio\U$k"SK$/SM$n\EsN0p[lN*1 lK[d2%Sa2/d0$^Ixp"|dc`;s/\W//g;$_=pack('H*',/((..)*)$/) Please visit http://online.offshore.com.ai/arms-trafficker/
Re: C Compilers
g++ compiles C and C++ and it's worked for everything I've ever tried to compile. What are the differences between: gcc egcs pgc and which should i use? Last i tried, egcs wouldn't compile kernels. All i want is a good, quick C and C++ compiler that works 100% with all code. Also, where am i supposed to install a c compiler and how do i remove an existing one (after compiling the new one of course!)? -- +++ The program isn't debugged until the last user is dead. +++ [EMAIL PROTECTED] http://www.penguinpowered.com/~a_out
Re: gcc with optimization
Well, if it's breaking it then don't use it :) You probably don't need to anymore anyway. Most modern compilers perform as much optimization as possible when they compile anyway. The -O flag is there because back in the old days when it took a while to compile, trying to optimize at the same time would have been too much of a strain on the processor. Hi all, This is a really newbie question: After compiling my source files with the -O (or -O2) options, my executable file gives me bus errors. When I compile without the -O options, everything works flawlessly. Why is this happening and what can I do to fix it? anukool.
Re: Arrays is confusing
The output is actually correct for what you are telling it to do. You are telling to to assing i[j++] to j++. Now, i[j++] is the same as saying i[0] because j++ only gets incremented after it is used. You probably want something like i[++j] here. The =j++ does the same thing. So you are assigning i[0] = 0. And when you printf the first vaule i[0] is 0 so the output is correcct. The rest of it is random garbage because it's never been assigned a value. Why does this code give the strange output they give? #includestdio.h main() { int i[4] ; int j = 0; i[j++] = j++; printf("%d %d %d %d\n",i[0],i[1],i[2],i[3]); return 0; }
Re:
Try vi, g++, and gdb Darius Blaszijk wrote: Is there any compiler/editor/debugger-package anvailable from the internet? Or am I missing something here? I've noticed that MC has an edit feature which shows source code using different colours. I find this very helpfull. You can use wpe or xwpe for this. When you have bought the the RedHat box with book etc. Then you alse have the application cdrom with Code Crusader on it. Henk Jan
Re: strtok()
maybe it's just me, but i find this funny... (from the strtok() manpage) BUGS Never use this function. it's a fairly big bug! :) i expect there are similar things stuck to nuclear warheads (Warning! This device is hazardous to most forms of life, do not use). where is this? It's not in my manpage.
Re: mkdir();
The mode_t structure holds the permissions that you want to set on *Path. Take a look at the mkdir(2) and the chmod(2) man pages for more info. The syntax for mkdir is : #include sys/mode.h #include sys/stat.h int mkdir (Path, Mode) const char *Path; mode_t Mode; I don't understand the mode_t structure. Can somebody mail me an example of how to use this function in a C-program ? Thx and greetings - De Messemaeker Johan Research Development HEMMIS n.v. Koning LeopoldIII-laan 2, 8500 Kortrijk Tel.: 32 (0)56/37.26.37 Fax: 32 (0)56/37.23.24 Current Project : VMM Aalst
Quad Trees
I was wondering if anyone on this list knew anything about quad trees, such as any good site for information on them, or any good reference books.
Re: Yet another beginners question ...
Try doing something like this const char Logfile = '/usr/people/bob/APHOME/applogfile' FILE *fp remove("LogFile"); fp = fopen("LogFile", "w"); . . . What it sounds like is that your #define isn't getting the entire path to the file so it's just doing what it can with what it knows, which is the directory where the program lives. I'm writing a program and i have the following question. I'm defining a homedirectory and logfile for my application like this #define LOGFILE /APPHOME/applogfile /* APPHOME is the actual home directory for the application */ But when i try to delete the old logfile and open the logfile like below, it doesn't work (it won't access the file described as above, instead, it creates a file LOGFILE in the directory where i run the program. FILE *LogFile; remove("LOGFILE");/* First remove the old logfile */ LogFile = fopen("LOGFILE", "w"); . . do some stuff . . fclose("LOGFILE"); What is the problem ? De Messemaeker Johan HEMMIS n.v. Koning LeopoldIII-laan 2, 8500 Kortrijk Tel.: 32 (0)56/37.26.37 Fax: 32 (0)56/37.23.24 Current Project : VMM Aalst
RE: Yet another beginners question ...
It might be 'old C', I'm not really sure, but it is the definitive C book. It's only about 150-200 pages but it can answer just about any C question. Another thing you might want to try, most unix systems have man pages for most C functions which are usually pretty helpful; giving examples and things like that. Oeps, found it myself :-) I should have done : #define LOGFILE "/APPHOME/applogfile" ... LogFile = fopen(LOGFILE,"w"); I think I should buy a better C-book. My book (a dutch one, not very known) defines #define as follows : #define identifier string with an example : #define TITLE This is the title. So I thought that i didn't have to add "" around my logfile-description ... My friend says that I should buy the KR but i think I read somewhere that it's 'old C', not ANSII C. Is this correct ? -Oorspronkelijk bericht- Van:Johan De Messemaeker Verzonden: dinsdag 2 maart 1999 11:13 Aan:Linux-C-Programming mailing list (E-mail) Onderwerp: Yet another beginners question ... I'm writing a program and i have the following question. I'm defining a homedirectory and logfile for my application like this #define LOGFILE /APPHOME/applogfile /* APPHOME is the actual home directory for the application */ But when i try to delete the old logfile and open the logfile like below, it doesn't work (it won't access the file described as above, instead, it creates a file LOGFILE in the directory where i run the program. FILE *LogFile; remove("LOGFILE"); /* First remove the old logfile */ LogFile = fopen("LOGFILE", "w"); . . do some stuff . . fclose("LOGFILE"); What is the problem ? De Messemaeker Johan HEMMIS n.v. Koning LeopoldIII-laan 2, 8500 Kortrijk Tel.: 32 (0)56/37.26.37 Fax: 32 (0)56/37.23.24 Current Project : VMM Aalst
Re: link lists
http://elvis.rowan.edu/~berman/Book/Transparencies/index.html This has all the lecture notes from Dr. Bermans Data Structures class converted to HTML. This is the class where you actually learn about linked lists, stacks, queues, trees, and graphs. If you can find it the book itself is extremely helpful. Can someone please point me out to a place on the net where I can learn about link lists? I checked the site of Majon's online snippets and was disappointed since it only gave the source of a linked list rather than an explanation. (I have read somewhat about it but it stays confusin' still) Thank you _ DO YOU YAHOO!? Get your free @yahoo.com address at http://mail.yahoo.com
getpwent()
Does anyone know if the getpwent() series of function calls are smart enough to work with shadow password files?
Re: Syntax highlighting ...
use vim -g and the file name. then within vim use :syntax on it'll pick a syntax based on the file extension. What would be the most efficient way to do syntax highlighting ? De Messemaeker Johan HEMMIS n.v. Koning LeopoldIII-laan 2, 8500 Kortrijk Tel.: 32 (0)56/37.26.37 Fax: 32 (0)56/37.23.24 Current Project : VMM Aalst
Re: Core dump
If you compiled the program with the -g option (I think it's the same option in g++ and CC) then you can type dbx a.out which is a debugger. Once in the debugger type where. Most times that will give you a trce of what the program was doing when it crashed, usually you'll even get line numbers to tell you where it died. Compiling with the -Wall option will also help, it turns all the warnings on. --Joe I know that a core dump is a memory dump to the harddisk. How can I use this to ? How do I use the core file to determine where the bug is ? Regards, Johan De Messemaeker Johan HEMMIS n.v. Koning LeopoldIII-laan 2, 8500 Kortrijk Tel.: 32 (0)56/37.26.37 Fax: 32 (0)56/37.23.24 Current Project : VMM Aalst
Re: trees and such?
Go here http://elvis.rowan.edu/~berman/Book/index.html I took this class with Dr. Berman two years ago and I still use the code from this book every time I have a project to do. You can look at chapters 1-12 from this web page. That should cover Trees, Graphs, Efficiency, and basic data structures. If you have any more questions feel free to ask, understanding lists and trees are the most important thing a programmer can learn. --Joe
Vim question
I know this question is a little off topic but I was wondering how may of you vim users out there have managed to get syntax to appear in color within an xterm. I know that there is a gui version, I've used that up until now, but I've been trying to find doc's on how to do color highlighting from within an xterm. Any help you can offer would be most appreciated. --Joe
Re: Search sub-strings ...
I don't know of any built in functions that will do that but it's easy enough to do on your own. something like this: ... static const char whitespace[] = " \t\n\r\b\f\v"; ... find_string(str1,str2) { char *cp; for (cp = strtok(str1, whitespace); /* get first token */ cp != NULL; /* more left */ cp = strtok(NULL, whitespace) )/* get next */ if(cp==str2) return 1; } This should go through and pick out the first occurance of str2 in the string str1. --Joe Hi, Is there any function which search a sub-string in a string !? Example: String1 = "My name is Nuno" ; SubString = "name" ; function_I _would_like_to_have (String1, SubString) ; As "name" exist on String1 it should return something like: 1 or anything else ... Thanks. Best regards, Nuno Carvalho
Re: A debug equivalent utility
Hi, I'm looking for a dos's debug equivalent utility. i.e: a tool to trace into the assembly code of a linux executable (elf/a.out). Any ideas? Have you tried gdb and dbx? I don't knnow if they go down to assembly level but they're two of the best debuggers out there.
Re: C++ Destructor Question
In C++ you must always specifically call the destructor before the variable goes out of scope. When it goes out of scope the variable name and the memory it points to become unlinked and there is no way for you to go back and free that memory. That's how you get memory leaks :) i.e. class Foo { Foo() { word = new char[LENGTH + 1]; } ~Foo() { delete word; } ... } When I create an object of class Foo memory will be allocated for the char buffer "word". Now when the object is no longer needed must I make an explicit call to the destructor ~Foo() to destroy the object and subsequently call "delete", or, is the destructor somehow called automatically when the object is no longer needed,i.e. outside of it's scope?
Re: C++ Destructor Question
Yes, once the delete routine is used the memory is freed but in the code sample he gave the delete call was enclosed in the destructor for the class. Since from the code he wasn't using delete anywhere else he had to call the destructor implicitly. Not doing so would kill the variable but not free the memory used by the variable. There is the chance that it will be allocated to another variable along the line but it's always better to make sure you kill the variable yourself or else you risk losing some of your available memory. --Joe Joseph Keen wrote: In C++ you must always specifically call the destructor before the variable goes out of scope. Wrong. The destructor will be called automatically when the variable ceases to exist (i.e. upon leaving the enclosing function for automatic variables, upon delete for dynamic variables, and upon program termination for static variables). -- Glynn Clements [EMAIL PROTECTED]