Use LEFT and RIGHT arrow keys to navigate between flashcards;
Use UP and DOWN arrow keys to flip the card;
H to show hint;
A reads text to speech;
121 Cards in this Set
- Front
- Back
int i = 2;
int j = ++i; j++; cout << i << " "; cout << j << " "; cout << (&i == &j) << endl; |
3 4 false
|
|
int i = 2;
int& j = ++i; j++; cout << i << " "; cout << j << " "; cout << (&i == &j) << endl; |
4 4 true
|
|
const int a[] = {2,3,4}
creates an array where in C++ |
stack
|
|
final int[] a = {2,3,4}
creates what on where in Java |
reference, heap
|
|
final int[] a = {2,3,4}
changing possible? |
reference to array in Java is immutable, but array contents are mutable
|
|
const int a[] = {2,3,4}
changing possible? |
no
|
|
int i = 2
int& j = ++i explain |
reference ties j to i (so changing j changes i)
|
|
Object's equals() compares ..., not ...
|
references, not contents
|
|
C++ const vars must be ... when declared
|
initialized
|
|
type*, e.g. int*
means |
reads as pointer to an int and denotes the address of the var which is of type int
|
|
*variable, e.g. *p
|
p is an int* and reads "value at address". So ++p means incrementing the pointer (or address), whereas ++*p means incrementing the value at the address (stored by p) by 1.
|
|
type&, e.g. int& j = ++i
|
this should be read as the "address of an int stored in j has been assigned the incremented value of i", which means i and j are now tied forever so whenever i changes, j changes to match it and vice versa. This also means j is another name for i and cannot be assigned to another value later (I think) and you also can't increment j like ++*j since j isn't an address. It'd the equivalent of saying ++*i which doesn't make sense. If you print both of i and j's address in memory, they both have the same address.
|
|
variable&, e.g. int* p = &j,
|
says that the "address of" j is stored in a pointer to an int and that pointer is called p in this case.
|
|
void g(int* p){ ++p;}
int main () { int j = 3; g(&j); cout << j } prints what |
3
|
|
void g(int* p){
++*p; cout << *p } int main () { int j = 3; g(&j); cout << j } prints what, explain |
3
4 (p == &j) = true (even if p dies after going back to main) |
|
void g(int& p){ ++p;}
int main () { int j = 4; g(j); cout << j } prints what. explain |
5, p is another name for j (big picture)
|
|
void h(int& r) is a method whose argument demands a ... that cannot be ...
|
l-value that cannot be 0 since it's a null address
|
|
& should be read as ...
|
address-of operator
|
|
* should be read as ...
|
value-at-address operator
|
|
int i = 2;
++-i; explain (C++) |
++-i will throw an error bc C++ requires an l-value as increment operand
|
|
int i = 2
++++i explain (C++) |
++++i is allowed bc C++ requires an l-value as increment operand
|
|
int a[] = {2, 3, 4};
assert(a[1] == 3); // array index ++a[1]; a[1] is |
4
|
|
const int a[] = {2,3,4};
const int b[] = a; cout << "a address: " << &a << "\nb address: " << &b << endl; |
runtime error: initializer fails to determine size of 'b' not bc it's const though, but bc int[] needs to be initialized w/specific size
|
|
int a[] = {2,3,4};
int b[] = a; cout << "a address: " << &a << "\nb address: " << &b << endl; |
runtime error: initializer fails to determine size of 'b' bc int[] needs to be initialized w/specific size
|
|
int j = 2;
int* p = &j; ++*p; cout << j << endl; |
3
|
|
int j = 2;
int* p = &j; ++*p; cout << *p << endl; |
3
|
|
int j = 2;
int* p = &j; ++*p; cout << (p == &j) << endl; |
true
|
|
int j = 2;
int* p = &j; ++*p; cout << (*p == &j) << endl; |
ISO C++ forbids comparison between pointer and integer [-fpermissive]
|
|
final int[] a = {2, 3, 4};
final int[] b = a; print (a == b); ++b[1]; print a[1] print b[1] print (a == b) |
JAVA
true 4 4 true |
|
final int[] a = {2, 3, 4};
final int[] b = a.clone(); print (a == b); ++b[1]; print a[1] print b[1] print (a == b) |
JAVA
false 3 4 false |
|
int k = 2;
int& r = k; ++r; cout << r << endl; cout << k << endl; |
3
3 |
|
int i = 2;
int* p = &i; int*& r = p; ++*r; cout << i |
3
|
|
int i = 2;
int* p = &i; int*& r = p; ++*r; cout << *p |
3
|
|
int i = 2;
int* p = &i; int*& r = p; ++*r; cout << *r |
3
|
|
int i = 2;
int* p = &i; int*& r = p; ++*r; cout << (&r == &p) |
true
|
|
int a[] = {2,3,4};
int* p = &a[0]; (*p == a[0]) |
true
|
|
int a[] = {2,3,4};
int* p = &a[0]; (p == a) |
true
|
|
int a[] = {2,3,4};
int* p = &a; (*p == a[0]) |
error: cannot convert ‘int (*)[3]’ to ‘int*’ in initialization
|
|
int a[] = {2,3,4}
sizeof(a) with respect to sizeof(&a[0]) |
not equal
|
|
int a[] = {2,3,4}
sizeof(a) |
12
|
|
int a[] = {2,3,4}
sizeof(a[1]) |
8
|
|
int a[] = {2,3,4}
++a |
error: lvalue required as left operand of assignment
|
|
int a[] = {2,3,4}
++*(a+1) |
same as ++a[1]
|
|
int a[] = {2,3,4}
a[3] == 0 |
undefined
|
|
int a[] = {2,3,4}
int* b = a a == b |
true
|
|
int a[] = {2,3,4}
int* b = a sizeof(a) |
12
|
|
int a[] = {2,3,4}
int* b = a sizeof(b) |
8
|
|
int a[] = {2,3,4}
int* b = a ++b[1] |
4
|
|
int a[] = {2,3,4}
int* b = a ++b[1] a[1] |
4
|
|
int a[] = {2,3,4}
int* b = a b[1] |
3
|
|
int a[] = {2,3,4}
int* b = a &a is the same as what |
&b[0] and &a[0]
|
|
int a[] = {2,3,4}
int* b = a *b |
2
|
|
int a[] = {2,3,4}
int* b = a *b[0] |
error: invalid type argument of unary '*' (have 'int')
|
|
int a[] = {2,3,4}
int* b = a &b is the same as what |
none of the addresses. the pointer's address is where the pointer lives so it should not be the same as anything in the array. &b[0] does contain the same as &a[0] though
|
|
bool equal_1 (int* b, int* e, int* c) {
while (b != e) { if (*b != *c) return false; ++b; ++c;} return true;} template <typename II1, typename II2> bool equal_2 (II1 b, II1 e, II2 c) { while (b != e) { if (*b != *c) return false; ++b; ++c;} return true;} main() { int a[] = {2, 3, 4}; int b[] = {2, 3, 4, 5}; assert(equal (a, a + 3, b)); assert(equal_1(a, a + 3, b)); assert(equal_2(a, a + 3, b)); list<int> x(a, a + 3); list<int> y(b, b + 4); assert(equal (x.begin(), x.end(), y.begin())); // assert(equal_1(x.begin(), x.end(), y.begin())); assert(equal_2(x.begin(), x.end(), y.begin())); } |
// error: cannot convert ‘std::_List_iterator<int>’ to ‘int*’ for argument ‘1’ to ‘bool equal_1(int*, int*, int*)’
|
|
java
Integer x = new Integer(2); Integer y = new Integer(2); x == y |
false
|
|
java
Integer x = new Integer(2); Integer y = new Integer(2); x.intValue() == y.intValue(); |
true
|
|
java
Integer x = new Integer(2); Integer y = new Integer(2); ++x; ++y; x == y; |
true
|
|
java
Integer x = 128; Integer y = 128; x == y; |
false, cache: [-128, 127]
|
|
java
Long x = 128L; Long y = 128L; x == y |
false
|
|
java
Integer x = 127; Integer y = 127; x == y; |
true, cache: [-128, 127]
|
|
java
Long x = 127L; Long y = 127L; x == y |
true
|
|
java
Long x = (long) 2 compiles? |
yes
|
|
java
Long x = 2 compiles? |
no
|
|
java
long x = 2 compiles? |
yes
|
|
java
long j = 2; // Integer x = j; // doesn't compile Integer x = (Integer) j compiles? |
no, error: inconvertible types
|
|
java
long j = 2; // Integer x = j; // doesn't compile Integer x = (int) j compiles? |
yes
|
|
java
int[] a = {2, 3, 4}; Integer[] b = a compiles? |
no, error: incompatible types
|
|
java
int[] a = {2,3,4} Integer[] b = {2,3,4} compiles? |
yes
|
|
java
String s = "abc" String t = "abc" s == t |
true, literal Strings are cached
|
|
java
String s = "abc" String t = "ab" + "c" s == t |
true
|
|
java
String s = "abc" String u = "ab" String v = "c" String t = u + v s == t |
no, t doesn't have literal String
|
|
java
String s = "abc" String u = "ab" String v = "c" String t = u + v s.equals(t) |
true
|
|
unary ‘&’ operand requires
|
l-value
|
|
c++
const char* a = "abc" char** pointerofa = &a |
error: invalid conversion from const char** to char**
|
|
c++
const char* a = "abc" char* pointerofa = &a |
error: cannot convert const char** to char* in initialization
|
|
c++
const char* a = "abc" char* pointerofa = a |
error: invalid conversion from const char* to char*
|
|
c++
const char* a = "abc" char* pointerofa = *a |
error: invalid conversion from char to char*. recall *a gives you the char 'a' from the string "abc" so you're trying to make it a pointer to a char
|
|
c++
const char* a = "abc" char pointerofa = *a |
ok, same as
char pointerofa = 'a' |
|
c++
const char* a = "abc" const char* b = "abc" &a == &b |
false
|
|
c++
const char* a = "abc" const char* b = "abc" a == b |
true as contents of a and b are equal
|
|
c++
const char* a = "abc" const char* b = "abc" *a == *b |
true as *a and *b is the char 'a'
|
|
c++
const char* a = "abc" a = "def" compiles? |
yes, the address of the new a is the same as when it was declared, but content is different
|
|
c++
char[]* a = "abc" |
error: expected unqualified-id before ‘[’ token
|
|
c++
const char* a = "abc" &(*a) |
abc
why? |
|
c++
const char* a = "abc" char p = *a &(p) |
a
why |
|
c++
const char* a = "abc" *a |
letter a
|
|
c++
const char* a = "abc" char p = *a p |
letter a
|
|
C++
const char* a = "abc" const char* addressOfLetterA = &(*a) |
a is forever the pointer of "abc" with const char * so if you want to store the address of "abc" somewhere else, use const char*.
addressOfLetterA and a will have the same memory address |
|
char* salutation = "Hello world!";
|
/* 'salutation' addresses immutable memory */ If you need to change the content, use:
char salutation[] = "Hello world!"; |
|
& requires what kind of value?
|
l-value
|
|
const char a[] = "abc";
const char b[] = "abc"; cout << *a << endl; cout << *b << endl; |
a
a |
|
const char a[] = "abc";
const char b[] = "abc"; cout << &a << endl; cout << &b << endl; |
a and b have different addresses
|
|
c++
char* a = "abc" char* r = &(*a) do a and r's address match? |
no
|
|
c++
const char* a = "abc" const char* r = &(*a) do a and r's address match? |
yes
|
|
char a[] = "abc";
char b[] = "abc"; cout << &a << endl; cout << &b << endl; |
a and b have different addresses
|
|
java
int[] a = {2, 3, 4}; a.equals(new int[]{2, 4, 4}); |
false
|
|
java
int[] a = {2, 3, 4}; Arrays.equals( a, new int[]{2, 4, 4} ); |
true
|
|
int a[] = {2,3,4};
a == &a |
error
|
|
int a[] = {2,3,4};
a == &a[0] |
true
|
|
size_t s = 10;
int a[s] = {2,3,4}; cout << a[4] |
error: variable-sized object ‘a’ may not be initialized
|
|
int a[10] = {2,3,4};
cout << a[4] |
0
|
|
int a[] = {2, 3, 4};
int* const b = a; a == b |
true
|
|
int a[] = {2, 3, 4};
const size_t s = sizeof(a) / sizeof(a[0]); int b[s]; copy(a, a + s, b); a == b |
false
|
|
int a[] = {2, 3, 4};
const size_t s = sizeof(a) / sizeof(a[0]); int b[s]; copy(a, a + s, b); equal(a, a + s, b) |
true
|
|
int a[] = {2, 3, 4};
const size_t s = sizeof(a) / sizeof(a[0]); int b[s]; copy(a, a + s, b); a[0] == b[0] |
true bc content is the same//used to be false.
a == &a[0] is also true even if a == b is false. &a[0] != &b[0] |
|
int a[] = {2, 3, 4};
const size_t s = sizeof(a) / sizeof(a[0]); int b[s]; copy(a, a + s, b); a[1] == b[1] |
true bc content is the same. addresses of a[1] and [b1] are not the same
|
|
int a[] = {2, 3, 4};
const size_t s = sizeof(a) / sizeof(a[0]); int b[s]; copy(a, a + s, b); ++a[1]; a[1] == b[1] |
false bc content changed
|
|
int a[] = {2, 3, 4};
int b[] = {5, 6, 7}; b = a; |
error: invalid array assignment
|
|
difference between c string and c++ string
|
C string is a char array whose last element is '\0'.
The == operation checks if the references are the same. You need strcmp for content. C++ string is an object. The == operator checks the content. |
|
const ptrdiff_t s = 10;
const int v = 2; int* const a = new int[s]; //initialized w/0 fill(a, a + s, v); assert(count(a, a + s, v) == s); assert(a[1] == v); a == &a[0] // ++a//error: increment of read-only variable |
true, before and after fill operation, addresses of a and a[0] are the same (const)
|
|
const size_t s = 10;
const int v = 2; int* const a = new int[s]; fill(a, a + s, v); int* const b = a; assert(&a[1] == &b[1]); int* const c = a; //ok b = c; |
error: assignment of read-only variable 'b'
|
|
const size_t s = 10;
const int v = 2; int* const a = new int[s]; fill(a, a + s, v); int* b = new int[s]; fill(b, b + s, v); // b = a; // memory leak copy(a, a + s, b); |
copy overwrites, i guess? if you do "b = a" that creates a new int[]?
|
|
allocator<int> x;
const ptrdiff_t s = 3; const int v = 2; int* const a = x.allocate(s); //*a is 0 int* b = a; int* e = a + s; while (b != e) { x.construct(b, v); //*b is v ++b;} assert(count(a, a + s, v) == s); b = a; //assigning the beginning of a to b while (b != e) { --e; x.destroy(e);} x.deallocate(a, s); |
int* b = a where a is * to int/array can result in b being used as a *
|
|
struct A {
int i; void f () { } }; struct B : A { int j; void f () { } }; A a[] = {B(), B(), B()}; // slice a[1].f(); //which struct's f() gets called |
A::f();
|
|
A* const a = new B[10]; // dangerous
a[0].f(); // A::f() // a[1].f(); // undefined // delete [] a; // undefined static_cast<B*>(a)[1].f(); // B::f() delete [] static_cast<B*>(a); // ~B::B() and ~A::A() |
struct A {int i; void f () {}};
struct B : A {int j; void f () {}}; |
|
const size_t s = 10;
const int v = 2; vector<int> x(s, v); assert(x.size() == s); assert(x[0] == v); vector<int> y(x); assert(x.size() == y.size()); assert( x[1] == y[1]); assert(&x[1] != &y[1]); vector<int> z(2 * s, v); x = z; assert(x.size() == z.size()); assert( x[1] == z[1]); assert(&x[1] != &z[1]); |
content is the same. addresses are not
|
|
struct A {
A () {cout << "A() ";} // default constructor A (int) {cout << "A(int) ";} // int constructor A (const A&) {cout << "A(A) ";} // copy constructor ~A () {cout << "~A() ";} // destructor A& operator = (const A&) {cout << "=(A) "; return *this;}}; // copy assignment operator int main () { { A a[5] = {2, 3, 4}; cout << endl; } cout << endl; |
A(int) A(int) A(int) A() A()
~A() ~A() ~A() ~A() ~A() |
|
struct A {
A () {cout << "A() ";} // default constructor A (int) {cout << "A(int) ";} // int constructor A (const A&) {cout << "A(A) ";} // copy constructor ~A () {cout << "~A() ";} // destructor A& operator = (const A&) {cout << "=(A) "; return *this;}}; // copy assignment operator int main () { { A x; cout << endl; A* a = new A[5]; cout << endl; fill(a, a + 5, x); cout << endl; delete [] a; cout << endl; } cout << endl; return 0;} |
A()
A() A() A() A() A() =(A) =(A) =(A) =(A) =(A) ~A() ~A() ~A() ~A() ~A() ~A() |
|
public static void main (String[] args) {
{ final long[] a = {2, 3, 4}; arraycopy(a, 0, a, 1, 2); assert Arrays.equals(a, new long[]{2, 2, 3}); } { final long[] a = {2, 3, 4}; arraycopy(a, 1, a, 0, 2); assert Arrays.equals(a, new long[]{3, 4, 4}); }}} public static void arraycopy (long[] a, int i, long[] b, int j, int s) { |
public static void arraycopy (long[] a, int i, long[] b, int j, int s) {
if (j < i) { int n = 0; while (n != s) { b[j + n] = a[i + n]; ++n;}} else { int n = s; while (n != 0) { --n; b[j + n] = a[i + n];}}} |
|
{
int i = 2; int* p = &i; int** pp = &p; int** qq = pp; ++**qq; cout << i << " "; ++qq; cout << (pp == &p) << endl; } { int i = 2; int* p = &i; int** pp = &p; int**& qq = pp; ++**qq; cout << i << " "; ++qq; cout << (pp == &p) << endl; } |
3 true
3 false |