sepp2k 378 Practically a Master Poster

Even if you make abc global, it still doesn't work in Visual Studio 2015

Because your function is returning a copy of abc and that copy is a temporary object that goes out of scope at the sequence point, just as deceptikon explained.

returnValue seems to point to I'm not sure where, but clearly it's not pointing to whatever c_str() returned?

returnValue is exactly equal to the pointer retunred by c_str() - after all you just assigned it to that on the line before. However you called c_str() on the temporary object, not on abc and c_str() on the temporary object returns a different pointer than c_str() on abc, specifically it returns a pointer to temporary storage that gets freed when the temporary goes out of scope, which is why you see those results.

AssertNull commented: Good explanation +6
sepp2k 378 Practically a Master Poster

Any implementation will definitely read everything (well some languages have some kind of "stop here" token, which causes the impelementation to stop reading at that token, but in those case the stuff after would simply not be considered part of the program at all). Even if it did decide to skip, say, the body of an if-statement, it would still need to actually read it in order to know how far to skip.

A compiler will also never skip anything based on user input as the compiler can't possibly know what the user input will be (as the compiler is long done doing its job when the program actually runs). It might skip generating code if it can tell that a piece of code will never ever get executed (say if the programmer wrote something impossible like if(1==2)), but generally the code would still be parsed - just not translated to machine code.

An interpreter could skip based on user input, as interpreters actually run at the time as the program, but generally the only thing it'd skip is execution. That is if you have an if whose condition is false, the body obviously won't be executed. In most interpreters it would still be parsed though. However there are some interpreters, most notably shells, that actually do skip syntax analysis of any lines that do not get executed, that is after seeing an if with a false condition, it discards all lines until one is equal to fi (but, as …

sepp2k 378 Practically a Master Poster

2147483647 is the largest number that an int can store. When you convert it to float, it becomes 2147483648.0 due to floating point accuracies. When you convert that back to int, you'd get 2147483648, except that that's too large to fit into an int. So instead you get an overflow, resulting in -2147483648, which is the smallest number an int can store - and thus the first number you get after an overflow.

If you used a long instead of an int for backAgain, it would have the value 2147483648 as (possibly) expected.

I assume there was some data loss when float was explicitly casted into int .

There was a loss of precision when the int was casted to float (note that this cast can be lossy even though it is implicit - this may perhaps be considered improper language design). There was then additionaly an overflow when casting back.

ravi_14 commented: Lucid and to the point! Thanks a lot! +1
sepp2k 378 Practically a Master Poster

but we can't access any character from char pointer with loop like str[0]. str[0] prints the whole string till last character.

That's not true. str[0] will give you the character 'T' in your example, str[1] will give you 'h' and so on.

sepp2k 378 Practically a Master Poster

When you don't specify the size of an array, the size is inferred from the initializer. So when you write int arr[] = {1,2,3}, that's the same as int arr[3] = {1,2,3}. Likewise char formula[] = {'\0'} is the same as char formula[1] = {'\0'} and equivalently char fomula[] = "" is the same as char formaula[1] = "".

So you're defining an array of size one. You're then writing into that array using cin >> formula. This will write more than one character into the array, meaning you're writing out of bounds. This invokes undefined behavior, possibly overwriting other variables' memory and/or causing a crash.

sepp2k 378 Practically a Master Poster

It'd have helped us if you had described in what way the code did not behave as expected, but your problem is that you access the matrix out of bounds (the matrix can hold 2x2 items, but you're trying to store 3x3).

Also you're printing "1th" and "2th" when the correct forms would be "1st" and "2nd" (also "0th" isn't very pretty, you should probably add one the index when displaying it, so that the first item is referred to as the first, rather than the zeroth).

tobyITguy commented: Exactly. Descriptions are important +1
sepp2k 378 Practically a Master Poster

I am not to properly understand why, and when we have to dynamic cast.

Strictly speaking, you don't ever have to use dynamic_cast. You can use dynamic_cast when the castee is a pointer or reference to a class that contains at least one virtual member (that's what's meant by the pointer being polymorphic).

The reason that you should use dynamic_cast over static_cast (when downcasting) is that dynamic_cast will throw an exception when the pointed-to object does not actually have the correct type, whereas static_cast would invoke undefined behavior in that case.

sepp2k 378 Practically a Master Poster

Can you tell me onw thing why we have to commit?

To apply changes from the workspace to the repository.

I read somewhere that workspace is different from the local repository. How?

The workspace contains one specific version of your files. The repository (i.e. the .git folder) contains a complete history which keeps track of all changes you ever commited with branches and everything.

I know there is one .git hidden folder. I checked into that. There is no copy of testfile1.txt present.

There is, it just doesn't exist as a file as such. Rather you can think of the repository as a database which keeps track of all actions you ever committed (like creating files, changing, moving or deleting them). It contains all information necessary to undo and redo any change you every committed as well as combining changes from different branches.

Your workspace on the other hand only contains the contents of the files right now.

Do I have two copies of the same file in git system?(one i am editing and one i over-write after commoting) ? Explain.

You can sort of think of it as having one copy for each version that ever existed. Like if you create a file with the contents "bla", commit it, change it to "blablubb", commit it again and then change it to "blobbel", your workspace will contain a file with the contents "blobbel". Your repository will (sort of) contain the versions containing …

sepp2k 378 Practically a Master Poster

If you know how many elements you're going to add to the vector (or you at least have a reasonable upper bound), you can also use reserve to reserve the proper amount of memory up front, preventing reallocations. Then you'd have exactly one move constructor call per push_back and no calls of the copy constructor.

sepp2k 378 Practically a Master Poster

The controller variable is not defined within the counter method. You need to either pass it as a parameter or make it a member of the object (by doing self.controller = controller in the constructor) and then access it as self.controller instead.

In the future please include any error messages you get with your code (including line numbers and such). This makes it much easier for us to find the problem.

sepp2k 378 Practically a Master Poster

gcc produces a binary executable, not a shell script. To run it, type ./main.out.

CattleOfRa commented: Really stupid of me... thank you so much +0
sepp2k 378 Practically a Master Poster

Something else to consider, using the String.IndexOfAny method will shorten your code considerably

And, perhaps more importantly, improve the runtime from being quadratic in the worst case to being linear (in the length of the string).

overwraith commented: That's a good point. +2
sepp2k 378 Practically a Master Poster

How long are the strings you enter? If the first string has more than 2 characters, your first call to gets overflows your array. Likewise your second call to gets overflows if the second string entered is 2 * sl characters long or longer.

Either way the call to strcat will overflow str unless str2 is the empty string.

Further the arguments given to strcpy must not overlap, so your call to strcpy always invokes undefined behavior. What is the goal of that call?

sepp2k 378 Practically a Master Poster

If you do make them variables, dircopy $src $dst will work fine.

sepp2k 378 Practically a Master Poster

Learning Java on Arch Linux is no different than learning Java on any other system. You don't need an emulator - both the Oralce JDK and OpenJDK are available for Linux natively, as are all commonly used Java IDEs (eclipse, netbeans, IntelliJ).

So your steps for learning Java would be: pick the Java learning resource of your choice (be it a book, website, online course, personal tutor, whatever - this is not affected by your using Linux), install the JDK (pacman -S jdk8-openjdk - you might also want to install openjdk8-doc to view the Java docs offline and/or from within your IDE and openjdk8-src if you want to look into the sources for deeper understanding), install the IDE or editor of your choice, start programming.

sepp2k 378 Practically a Master Poster

If static modifier is not applied before does it makes it an instance method ?

Yes.

JamesCherrill commented: Sometimes the simplest answers are the best! +15
sepp2k 378 Practically a Master Poster

You check the file size after you've already opened the file for writing. Since opening a file for writing creates an empty file with that name (overriding the existing file if it exists), this means that a) the file will always exist and b) it's size will always be 0.

You need to check the size before opening the file instead.

sepp2k 378 Practically a Master Poster

When I try to compile this program in gcc I get this error

As the error message says, you need to supply the mentioned language options to make the code compile. Alternatively, if you want to make your code compile in C89 mode, you need to replace char *p = message with p = message and then put char *p; at the beginning of the function. I'd personally prefer using C99(or even C11) though.

Also I'm having trouble understanding the for loop because I don't see a condition that says, if we hit '\0' we are done.

The condition is *p. In C an expression counts as false if it evaluates to 0 and true otherwise and \0 is simply another way of writing 0. So the conditions *p, *p != 0 and *p != '\0' are all equivalent.

sepp2k 378 Practically a Master Poster

To tell you where you went wrong, it'd be good to know how you got to ten. For each iteration of the outer while loop what's the value of i going in, what's the value of j after the inner while loop and what's the value of i after incrementing it by j?

sepp2k 378 Practically a Master Poster

Did you create the text file on Windows? If so, it probably uses '\r\n' as the line ending, so after stripping the '\n', the passwords will end up as 'password\r' and thus not compare equal to 'password'.

That would explain why the problem does not occur in Windows as Python (like most languages) will automatically translate '\r\n' to '\n' when opening files in text mode on Windows (and only there).

sepp2k 378 Practically a Master Poster

This has nothing to do with const. If you remove the const keyword from your program, you'll get the same error.

The problem is that the initializer for a global variable must be a constant. Confusingly the meaning of the word "constant" here has nothing to do with the const keyword. Here "constant" means either a literal (something like 5, 'c' or "hello") or the application of an operator where all operands are constants. So something like 3 + 5 would be okay, but 3 + x or just x would not be.

Basically the initializer of a global variable must not depend on the value of any other variable or function.

sepp2k 378 Practically a Master Poster

Also asked (and answered) here.

ddanbe commented: OK +15
sepp2k 378 Practically a Master Poster

What you posted does not look like source code, it looks like what you might see when you open a binary file in a text editor. Presumably you downloaded the zcode file (which is a bytecode format), not the source code. I do not think the source code of Zork is publically available at all, which would explain why you were unable to find it.

PS: Neither Zork nor Adventure were written in Python, so even if you did find the source code, it wouldn't be in Python and reading it would probably not help you at all with learning Python.

sepp2k 378 Practically a Master Poster

What's the definition of o that you learned? Do you understand that definition? What are your thoughts on how that definition may apply to a^n = o(n!)?

zeedote commented: I just understood that big O is an asymptotic notation and used to classify algorithms according to their response time and memory they take. and I have no idea how to apply that def on this equatn. +0
sepp2k 378 Practically a Master Poster

The compiler will not ever complain, so why should a person?

That's not very sound reasoning. A compiler will also not complain when you name all your variables variable1, variable2 etc., but I can still think of plenty reasons why you shouldn't do that.

sepp2k 378 Practically a Master Poster

why do most people use brackets in this way when it comes to Java.

Because that's what the official style guide recommends.

The second method is obviously easier to read.

That's not obvious at all. I imagine that most people will find the style easier to read that they are used to.

sepp2k 378 Practically a Master Poster

Oh, I seem to have misread the last sentence of your question (I thought you wanted us to explain what an AST is and how to implement it in Java).

To answer your actual question: I think by "specify" the assignment simply means "define". That is it wants you to define the classes that make up the AST.

sepp2k 378 Practically a Master Poster

An abstract syntax tree is a tree where each node represents a syntactical element and each child of a node represents a sub-element of that element.

For example for the statement set x := y + 42; the AST would consist of an assignment node whose left child node would be an identifier node with the value "x" and whose right child would be an addition node. The addition node's left child would be an identifier node with the value "y" and its right child would be an integer literal node with the value 42.

To represent an AST in Java you'd define a class for each type of syntactical element that your language contains, using abstract base classes and inheritance to allow for the fact that multiple types of elements can be used as child nodes for some nodes (for example the child nodes of most expressions are themselves arbitrary expressions, so you'd have an abstract base class called Expression from which classes like Addition and IntegerLiteral inherit, so if a node has a member of type Expression, either an Addition or an IntegerLiteral (or any other type of expression that you implement) can be used there).

sepp2k 378 Practically a Master Poster

The power function has three returns. The first is inside the first if, the second (which is wrongly indented) is inside the second if and the third is outside of any if. The third will only execute if none of the previous ifs were executed (just like the second will only execute if the first did not execute (and the if condition is true, of course)), so you can think of it as being inside an implicit else, yes.

sepp2k 378 Practically a Master Poster

"instead"? Don't you get all four?

Anyway what happens is that first the super class constructor is called (causing "B::B(3)"), then the member constructors are called (causing "B::B()" because of your member b) and then the body of the constructor is executed (causing the other two outputs).