Trying to decide which programming language I want to learn

In summary: C++ is considered a lower-level language, meaning that it is closer to the hardware and gives you more control over how your program runs. However, C# is a high-level language, which means it is easier to learn and use and has a lot of built-in libraries that make coding faster and more efficient. Both languages have their advantages and disadvantages, and it ultimately depends on what type of project you are working on. For gaming, C++ is often preferred because of its speed and control, but C# has become increasingly popular in game development as well. It's worth trying both and seeing which one you prefer working with.
  • #386
Mark44 said:
Sometime after that, i gets incremented to 2.
Indeed. 'sometime after that' is exactly the problem: rather than double-guess the compiler as to when that 'sometime' is, much better to be sure by moving the increment into a separate statement.
 
Technology news on Phys.org
  • #387
pbuk said:
Indeed. 'sometime after that' is exactly the problem: rather than double-guess the compiler as to when that 'sometime' is, much better to be sure by moving the increment into a separate statement.
I wrote "sometime after that" because I didn't want to bring up the concept of sequence points. Post-increments and post-decrements occur after sequence points, which are described in this wiki article: https://en.wikipedia.org/wiki/Sequence_point.

In my reply to @yungman's comment about not using increment operators, the example I had in mind was a simple one, with obvious sequence points, like this.
C++:
int array[] = {2, 4, 6};
int i = 1;
int val = array[i++];
val will be set to 4, and "sometime after that"; i.e., after the array expression is evaluated, i will be incremented to 2.
 
  • Like
Likes sysprog
  • #388
hmmm27 said:
I wouldn't be surprised if a c compiler accepted, and processed properly, something to the effect of a[0++], but it also wouldn't surprise me if it didn't : since the 0 is procedure-embedded, that's explicitly "self modifying code", might give a run-time OS hissy-fit. And, it might preclude an optimizer from reserving and using a register, which would be faster.

You could always split the difference :
register int i=0 ; ...a[i++] ;
The compiler would reject the 0++ incrementation directive inside the brackets of the a[0++] expression because 0 is not a variable.
 
  • #389
yungman said:
It kind of doing what I want, BUT there's a warning as shown when I ran the program that the vector is out of bound.
That's not a warning -- that's a run-time exception, an error.
yungman said:
It's only defined as 3 element vector, small and I only read in 5 numbers. It cannot be out of bound!
The vector started out with three elements, all of them 0. Each call to push_back() adds a new number after the three 0 elements.

Your logic to exit the 2nd while loop is faulty. The expression dynArr.empty() is true only if there are no elements in the vector. Since you aren't removing them from the vector, the loop condition won't ever be true.
yungman said:
Also if you look at line 32 of the code and look at the top left corner of picture above, I expect to display 12345. But I got 00012345. AND the 12345 is at the right side of the stream. Is it when I use dynArr.push_back, I pushed the 5 numbers AFTER the 3 empty elements? That's the reason I got 000 in front?
Yes.

I made two changes to your code.
C++:
vector<int>dynArr;
This change eliminates the first three 0 elements.
C++:
unsigned i = 0;
while (i < dynArr.size())
{          
      num = dynArr[i++];
      cout << num << " ";// display all the numbers until the end.          
}
This change eliminates the exception that you saw.
 
Last edited:
  • Like
Likes yungman
  • #390
Mark44 said:
I wrote "sometime after that" because I didn't want to bring up the concept of sequence points. Post-increments and post-decrements occur after sequence points, which are described in this wiki article: https://en.wikipedia.org/wiki/Sequence_point.
Exactly that. I don't think that it is a good idea to tell someone to do something with subtle side effects unless you are going to explain those side effects.

Mark44 said:
In my reply to @yungman's comment about not using increment operators, the example I had in mind was a simple one, with obvious sequence points, like this.
C++:
int array[] = {2, 4, 6};
int i = 1;
int val = array[i++];
val will be set to 4, and "sometime after that"; i.e., after the array expression is evaluated, i will be incremented to 2.
That's the problem with bugs during the code lifecycle, they tend to happen when something that looked simple is modified and an unintended side effect crops up - or even worse the side effect goes unnoticed because it doesn't affect anything until 2 years later when you add another feature.

The way to write that code avoiding all of these potential problems without incurring any performance penalty (even if that mattered) is:
C++:
int array[] = {2, 4, 6};
int i = 1;
int val = array[i];
i++;
 
  • #391
sysprog said:
The compiler would reject the 0++ incrementation directive inside the brackets of the a[0++] expression because 0 is not a variable.
I'll take your word for it but, as long as the context is only iteration and not re-iteration (ie: it doesn't need to be rezeroed), it saves a superflous explicit declaration.
 
  • #392
hmmm27 said:
I'll take your word for it but, as long as the context is only iteration and not re-iteration (ie: it doesn't need to be rezeroed), it saves a superflous explicit declaration.
What's wrong with a[1]?
 
  • #393
Mark44 said:
C++:
unsigned i = 0;
while (i < dynArr.size())
{         
      num = dynArr[i++];
      cout << num << " ";// display all the numbers until the end.         
}
This is of course also a common application for a traditional for-loop:
C++:
for (int i = 0; i < dynArr.size(); i++)
{
    cout << dynArr[i] << " ";
}
Or in a modern (post 2011) version of C++, a range-based for-loop:
C++:
for (int num : dynArr)
{
    cout << num << " ";
}
 
  • Like
Likes pbuk
  • #394
pbuk said:
What's wrong with a[1]?
Nothing, if your purpose is to explicitly access the second element in an array. In what context were you comparing it to a hypothetical a[0++] statement ?
 
  • #395
pbuk said:
What's wrong with a[1]?
hmmm27 said:
Nothing, if your purpose is to explicitly access the second element in an array. In what context were you comparing it to a hypothetical a[0++] statement ?
I believe pbuk was referring to this post of yours, #380:
hmmm27 said:
wouldn't be surprised if a c compiler accepted, and processed properly, something to the effect of a[0++]
There's nothing hypothetical about a[0++]. It just flat won't compile.
 
  • #396
This thread is getting very long, with just under 400 posts. @yungman, when you have another question, please start a new thread.
 
  • Like
Likes pbuk
  • #397
Thank guys, my big boss just had a hip replacement yesterday and she's home. I am too busy right now to read the replies right now. I'll get to it later tonight or tomorrow.

thanks
 
  • #398
I'm closing this thread now. If anyone other than the OP (@yungman) feels the need to reply to a post here, please let me know, and I'll reopen it temporarily.
 

Similar threads

  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
4
Replies
107
Views
5K
  • Programming and Computer Science
Replies
8
Views
878
  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
2
Replies
54
Views
3K
  • Programming and Computer Science
Replies
13
Views
1K
  • Programming and Computer Science
2
Replies
58
Views
3K
  • Programming and Computer Science
Replies
16
Views
2K
  • Programming and Computer Science
Replies
9
Views
1K
Back
Top