« first day (1720 days earlier)      last day (1199 days later) » 

04:39
@JohnRennie yes sir, noted.
 
2 hours later…
06:35
@JohnRennie Hello.
@pi-π Hi. I'm busy I'm afraid. I can ping you when I'm free if you want.
@JohnRennie okay.
07:13
@pi-π Hi, I'm free now :-)
07:27
@JohnRennie How are strings stored in memory in C++?
@pi-π In C++ there is a string class, but I assume you mean the basic strings e.g. if you do: char* s = "Hello world".
Yes?
@JohnRennie okay let's first look into basic strings and later on we can come to string class.
In both C and C++ a string is an array of char.
The end of the string is indicated by a char with the value 0.
So take the string "Hello" i.e. a string five letters long.
In memory this is an array of the 6 characters 'H', 'e', 'l', 'l', 'o', 0
So it's the five characters of the string followed by zero. The zero marks where the string ends.
@pi-π Ok so far?
@JohnRennie Yes.
@JohnRennie Here both the string variable s and its contents are stored in stack and not in heap. Yes?
07:43
In C and C++ string literals are generally stored in the heap.
So if you write: char* s = "Hello"; then the six bytes needed to store the string "Hello" will be in the heap.
@JohnRennie It means that the pointer s from the stack will be pointing to the array of characters(string) in the heap. Is it so?
Correct
@JohnRennie Is that same in case of arrays too?
You mean if you write int* p = {1,2,3,4,5};
@JohnRennie Is it different from writing int p[] = {1,2,3,4,5} ?
07:50
Ah, int* p = {1,2,3,4,5}; is actually invalid anyway.
You can do:
#include <iostream>
using namespace std;

int main()
{
  int p[] = {1,2,3,4,5};

  for (int i = 0; i < 5; i++)
    cout << p[i] << endl;

  return 0;
}
That will put the 5 element array p on the stack.
@JohnRennie This also gives an error.
It gives a warning not an error. It still compiles and runs.
When I compile it using Microsoft C++ it compiles with no warnings:
D:\rhs\c>cl test.cpp /EHsc
Microsoft (R) C/C++ Optimizing Compiler Version 19.29.30136 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

test.cpp
Microsoft (R) Incremental Linker Version 14.29.30136.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:test.exe
test.obj
I guess you're using GNU C++.
@JohnRennie Yes.
GNU C++ is obviously fussier than MS C++.
@JohnRennie And how is the string class string literal stored?
07:59
I don't know how the string class stores its data.
But if you write:
#include <iostream>
using namespace std;

int main()
{
  string s = "Hello";
  cout << s << endl;

  return 0;
}
The string literal "Hello" will still be in the heap.
@JohnRennie Unlike java Strings in C++ are mutable. Yes?
27
Q: Are C++ strings mutable UNLIKE Java strings?

abcdeIs it alright to manipulate strings in C++ this way: string s = "Sting"; s[2] = 'a'; It works alright (and prints 'Sting'), but is it safe to do so? If yes, does this mean they are mutable?

I tried this:
#include <iostream>
using namespace std;

int main()
{
  string s = "Hello";
  s[1] = 'a';
  cout << s << endl;

  char* t = "Hello";
  t[1] = 'a'; // This line causes a crash
  cout << t << endl;

  return 0;
}
And it compiles, but the line:
t[1] = 'a'; // This line causes a crash
causes the program to crash.
I think the point is that strings are mutable, but string literals are not.
 
3 hours later…
11:32
hello why is it $\cos(\alpha)$ in the integral ?
I think if it is cos alpha then it only directs force vector to middle
I am confused why not simply ignore cos alpha?
And I don't know why force vector needs to direct it's direction to middle
the force is not even acting in center
12:13
@BannedUser If we take a similar differential element to the opposite(bottom) of the element in the picture...then the vertical components will cancel out as they exert equal forces at P. So we take the resultant force only...
@BannedUser The derivation will be much simpler if you take rings as differential elements...
 
4 hours later…
16:12
@JohnRennie Hello.
@pi-π Hi :-)
@JohnRennie Can we use smart pointers in place of raw pointers?
I haven't done a lot with smart pointers - only regular pointers - so I don't know a lot about them I'm afraid.
@JohnRennie No problem.
#include<iostream>
#include<vector>
using namespace std;
int main() {
int row;
cout << "Enter the number of rows : ";
cin >> row;
int col;
cout << "Enter the number of columns : ";
cin >> col;
vector<vector<int>> vec;
//Taking user input
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
int x;
cin >> x;
vec[i].push_back(x);
}
}
//Printing output
for (auto vctr : vec) {
for (auto ele : vctr) {
cout << ele << " ";
}
cout << endl;
}
system("pause>0");
}
@JohnRennie What's wrong in this code?
You need to click the "fixed width" button to keep the indents.
This
16:25
#include<iostream>
#include<vector>
using namespace std;
int main() {
	int row;
	cout << "Enter the number of rows : ";
	cin >> row;
	int col;
	cout << "Enter the number of columns : ";
	cin >> col;
	vector<vector<int>> vec;
	//Taking user input
	for (int i = 0; i < row; i++) {
		for (int j = 0; j < col; j++) {
			int x;
			cin >> x;
			vec[i].push_back(x);
		}
	}
	//Printing output
	for (auto vctr : vec) {
		for (auto ele : vctr) {
			cout << ele << " ";
		}
		cout << endl;
	}
	system("pause>0");
@JohnRennie It is fine now.
It compiles OK but it hangs when I run it. I'm eating my lunch now so I'll hve to look at this later.
@JohnRennie Yeah I got the same problem . Okay we'll look into this later.
17:03
@pi-π Try this:
#include<iostream>
#include<vector>
using namespace std;

int main() {
    int row;
    cout << "Enter the number of rows : ";
    cin >> row;
    int col;
    cout << "Enter the number of columns : ";
    cin >> col;
    cout << "Now enter all the data" << endl;

    // Declare a vector of vector<int> to hold all the data
    vector<vector<int>> vec;

    //Taking user input
    for (int i = 0; i < row; i++) {
        // We'll add the data for the current row to this vector<int>
        vector<int> v_int;

« first day (1720 days earlier)      last day (1199 days later) »