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).

sepp2k 378 Practically a Master Poster

Your printf format strings don't include any specifiers (like %s, %d, %f etc.). So printf does not expect any arguments after the format string, but you do give arguments. These arguments will be ignored and the compiler warns you about this because that's most probably not what you want.

sepp2k 378 Practically a Master Poster

When you say "functor" are you using the term in the C++ sense (i.e. a callable object) or something else?

And would a good example be the most simple one that shows you how to create one or something that shows you where they'd be useful?

sepp2k 378 Practically a Master Poster

But, why can't we write all the functions directly in .h file and include it in my project files?

Because then linking would fail with a "multiple definitions" error if you include the same header into more than one compilation unit (i.e. if you include it from more than one .c file in the same project).

In header file, we have header guard which prevent to have the definitions again and again.

That will prevent the header being included multiple times into the same compilation unit. It will not prevent different compilation units from including the same header (after all different compilation units need to be able to include the same header).

nitin1 commented: nice. +4
sepp2k 378 Practically a Master Poster

<string.h> (also available as <cstring>) is a C header that declares functions that work on C strings (i.e. 0-terminated char*s). The std::string class is defined in <string>. You'll also need to refer to it as std::string as it's located in that namespace.

sepp2k 378 Practically a Master Poster

When you say 'member' are you refering to the, what would be an object if the structure were a class or something else?

No, he's talking about what comes after the . in foo.bar (foo is the object, bar is a member of that object) or in other terms: members are the things you define inside the class/struct/union body.

sepp2k 378 Practically a Master Poster

I assume it only looks at the last 5 bits of the right operand to << and ignores anything else (presumably that's how it's implemented in the CPU). So 32 is the same as 0, 33 as 1 etc. The reason it's allowed to do that is that, as I said, it's undefined behavior and thus any behavior is allowed.

sepp2k 378 Practically a Master Poster

Shifting an n-bit integer by m bits when m > n invokes undefined behavior in C and C++.

If you change your code like this, it will give you 0 from count 32 onwards as you'd expect:

unsigned int t = 1;
for (unsigned int p = 0; p < 40; p++)
{
    cout << "Count: " << p << " - "<< t << endl;
    t = t << 1;
}

Here I made two changes to your code:

  1. Instead of shifting t by p, I shift t by one and store the result in t. This will still lead to t having been shifted p times, but since I only ever shift by 1 bit at a time, I don't invoke undefined behavior.

  2. I made t into an unsigned integer. The reason for that is that signed integer overflow invokes undefined behavior as well.

sepp2k 378 Practically a Master Poster

I haven't read How to Design Programs, so I don't know how good it is, but if you're having success so far, I'd say just stick with it. In my opinion Racket/Scheme is a much easier language to learn than C++, so I wouldn't recommend starting with C++ (though you certainly can).

I don't know what Dr.Racket is but looks like some form of haskell.

Racket is a dialect of Scheme, which is a dialect of Lisp. It's not related to Haskell. Dr Racket is an IDE for Racket.

chriswelborn commented: +1 for clearing up the origin of Racket. +4
sepp2k 378 Practically a Master Poster

A destructor is called (automatically) when an object is destroyed. It does not itself destroy the object. An object is destroyed when it goes out of scope or when delete is used on it (in case of objects created using new).

You should never call a destructor manually unless you're doing advanced manual memory management (like defining your own allocator using placement new).

sepp2k 378 Practically a Master Poster

So I just started using the Goto command

Perfect time to stop.

But when I type in follow or Follow it gives me the text that I intend ( You get up and go to the door) but it still gets to the first goto it finds and sends me back to the beggining

That's because the goto isn't part of the else-block. It's outside of the if-else and thus gets executed either way (same as the print statement before it other than the first one).

To clarify this, here's what your code looks like indented correctly:

if ((sof=="follow")||(sof=="Follow"))
{
    cout<<"*You get up and go to the door*"<<endl;
    cout<<"-Well thats it for now xD"<<endl;
}
else
    cout<<"*5 hours later*"<<endl;

cout<<"-So, not getting anywhere, huh?"<<endl;
cout<<"Let me just warp you back a few hour so you can make the 'right' choice."<<endl;
cout<<"*You feel a strange buzz*"<<endl;
cout<<"So here we are at your first choice."<<endl;
cout<<"-Do you 'stand' there and TRY to get me out of your head..."<<endl;
cout<<"Or do you 'follow' the strange noises coming from the door?"<<endl;
goto firsttp;

As you see, everything other than "5 hours later" happens regardless of whether the if- or the else-block was hit. To add the other statements to the else-block, you'll need to add braces around them.

And stop using goto.

ddanbe commented: Wise advice! +15
sepp2k 378 Practically a Master Poster

According to this list Visual Studio did not support initializer lists until version 2013. So you just won't be able to use this feature with your version of Visual Studio.

sepp2k 378 Practically a Master Poster

That's not really true. The compiler will know whether you're using java.awt.Color regardless of whether or not you use an import. All the import does is to allow you to write Color instead of spelling out java.awt.Color. It's just a convenience feature.

JamesCherrill commented: Yes, Iknow. I was deliberately "over-simplifying" to try to get a point across to someone who was really struggling. JC +15
sepp2k 378 Practically a Master Poster

int& length = 0; does not work because 0 is not an l-value. As I said an l-value is something like a variable or an array access - something that can be assigned to. You can't assign to 0.

Think of it this way: defining length as a reference means "whenever I change length, I want the target of the reference to also change". That is when you write int& length = x; (where x is an int variable) and then length = 42;, the value of x will now also be 42. If you write int& length = 0; and then length = 42;, that would mean that now the value of 0 is now also 42. Of course that doesn't make any sense - the value of 0 can't be 42; the value of 0 is 0. So you're not allowed to write int& length = 0;.

I don't use the & in the declaration only when passing it?

You use & if you want length to be a reference to something, that is if you want something else to change whenever length changes. If that's not what you want, you don't use &.

sepp2k 378 Practically a Master Poster

my question is that when we write applet then we write only methods defination not method calling so i am confuse about this that who call the methods of applets.

Think of it this way: When you write a normal Java application you write a main method, but you never write any code that calls the main method. So who calls the main method? The Java VM does when your application is run.

Likewise when your applet is loaded in a browser, the browser's Java plug-in will load your applet class and call methods on it. It just doesn't call the main method, but rather calls the apropriate listeners whenever an event occurs.

these methods call without anything so how should be calling methods with this syntax.

They're called on the current object. It's exactly the same as writing this.setForeground(Color.cyan);. When calling a method on the current object, the this. is simply optional in Java (this is not specific to applets).

sepp2k 378 Practically a Master Poster

As the message says int& length is a reference (that's what the & means). You can not define a reference without initializing it. That is you need to do int& length = foo; where foo is an l-value (like a variable, an array access, a member access or a function call that returns a reference).

Just writing int& length; is not allowed because then the compiler does not know what length is a reference to.

sepp2k 378 Practically a Master Poster

Yes, because secs is the third argument and hrs is the first. You can use either MyTime(0, 0, totalsec) or MyTime(secs = totalsec).

sepp2k 378 Practically a Master Poster

As the error message says, tuples don't have a between method, so you can't do t.between(...) where t is a tuple.

You can call between on MyTime objects because you defined a between method for the MyTime class. So if you create an object of that class (by using MyTime(...)), you can call between on that object.

sepp2k 378 Practically a Master Poster

when you put i++ in to a for loop it will add the value of "i" after the compiler has checked the middle comparison statement but will add 1 to "i" before the compiler proceeds to the bracket

That's not true. The steps happen in the following order:

  1. Condition is checked. If false abort.
  2. Loop body is executed.
  3. Increment is executed.
  4. Goto 1

So no matter whether you write i++ or ++i or i=f(i) or whatever, the increment always happens before the condition is checked (except the first time when the increment doesn't happen at all).

For example if you were to print i++ and ++i one would print the value of i then add and the other would add then print the value. Makes a difference.

The difference between i++ and ++i is not when the increment happens. It always happens exactly when the expression is evaluated. The difference is the resulting value. The expression i++ results in the old value of i while the expression ++i results in the new value.

So if i starts as 42 and you do f(i++), then f will be called with the argument 42, but the value of i will already be 43 (you can test this by making i an instance variable and letting f print both its argument and i).

sepp2k 378 Practically a Master Poster

You are making the wrong typecast

One doesn't need to (and arguably shouldn't) typecast the result of malloc at all. And much more importantly, ptr is still not a pointer, so that line won't work no matter what he casts the result of malloc to.

sepp2k 378 Practically a Master Poster

When i is 0, we get arr[num[0]] += 1. num[0] is 1, so we get arr[1] += 1. By the same logic we get arr[4] += 1 when i is 1 and so on.

So no, it doesn't increase each element of arr by 1. Rather it increases each element in arr once for each time its index appears in num. So for example arr[1] is incremented twice because 1 appears twice in num and arr[0] is never increment because there is 0 in num.