I'm not a man anymore! JOJO, I want to be a ruthless problem brushing machine

Except for the generic relational operator " Member pointer operator* ", scope operator":: ", sizeof operator and ternary operator"?: "All operators in C + + can be overloaded except.

  • Malloc function: void *malloc(unsigned int size)
    Allocate a continuous space with a length of size in the dynamic allocation area of memory. If the allocation is successful, the first address of the allocated memory space will be returned; otherwise, NULL will be returned, and the requested memory will not be initialized.
  • Calloc function: void *calloc(unsigned int num, unsigned int size)
    Allocate a continuous space of num * size according to the number of data given and the number of bytes occupied by the data type. After applying for memory space, calloc will automatically initialize the memory space to 0, but malloc will not initialize. Its memory space stores some random data.
  • Realloc function: void *realloc(void *ptr, unsigned int size) dynamically allocates a memory space with a length of size, assigns the first address of the memory space to ptr, and adjusts the ptr memory space to size. The requested memory space will not be initialized.
  • new is an operator for dynamically allocating memory. It automatically calculates the space to be allocated. When allocating the memory space of a class type, it also calls the constructor of the class to initialize the memory space, that is, to complete the initialization of the class. Whether the built-in type of dynamic allocation is automatically initialized depends on the position of variable definition. All variables defined outside the function are initialized to 0, and none of the built-in type variables defined inside the function are initialized.
int a=5,be ++(a++)The value of is?

A + + returns a value of 5
++Operators can only act on variables, not a number (+ + 5 causes an error)

The length of data cannot be a variable: int i=2017; int arr[i];

A pointer, 4 bytes under 32 bits and 8 bytes under 64 bits

 a=1;b=1;a+ = b++;b+=++a;What is the result
  • The first step is to initialize a = 1 and B = 1. The second step is to initialize a+=b + +. B + + is suffix addition. First operation and then addition, that is, first execute a = (a + b) = (1 + 1) = 2, and then b = b+1=2. At this time, a=2 and b=2
    The third step, B + = + A, + A is prefix addition. Add first and then operate, that is, first execute a=a+1=3, then b=b+a=2+3=5, and finally a=3, b=5
  • The formal parameter of the copy constructor is not limited to const, but it must be a reference to pass the parameter by address. Otherwise, the copy constructor will recurse infinitely, and the pointer will not work. The essence is to pass the value.
  • In c + +, when the variable int *p is declared, it means that P is a variable that stores the address; For ex amp le, int *p=0 means that P points to an address unit with an address of 00000000. After declaring the pointer P, use * P to represent the content of the storage space pointed to by p& Indicates the address of the variable;
A. static Class variables are also called static member variables. They can exist in memory without creating objects
B. When creating an instance object, a memory space will be opened for each non static member variable of each instance object to store all non static member variable values of the object
C. static Class variable is shared by all objects. One object changes its value, and other objects get the result of the change
D. Instance variables are private to objects. If an object changes its value, it will not affect other objects

A: The static variable is stored in the BSS section when it is not initialized and in the data section after initialization. A is correct
B: Static members will not open up space. B is correct. C: static variables are class variables, which can be understood as only one copy. C is correct
D: It can be understood as: for instance objects, each instance has its own variable. Changing its value only changes its own share. D is correct

int main()
{
int a=1,b=2,m=0,n=0,k;
 
k=(n=b<a)&&(m=a) ;
printf("%d,%d\n",k,m);
 
return 0;
}

Output: 0, 0

(n = B < a) & & (m=a) & & the front is false, and the rear m=a is not executed

stay C In language, the correct statement about static variables is ().

A. Static variables and constants work the same way
B .Static variables in functions,It is not released after the function exits
C. Static variables can only be assigned once,After assignment, it cannot be changed
D. The scope of static global variables is all source files of a program

A. Different, the scope of a constant can be the whole project, while the scope of a static global variable can only be in the source file of its definition
C. The value of a static variable can be changed
D. Same as A

#include<iostream>
using namespace std;
 
class A
{
char a[3];
public:
virtual void fun1(){};
};
 
class B : public virtual A
{
char b[3];
public:
virtual void fun2(){};
};
 
class C : public virtual B
{
char c[3];
public:
virtual void fun3(){};
};
 
int main ()
{
cout << sizeof(A) << endl;
cout << sizeof(B) << endl;
cout << sizeof(C) << endl;
return 0;
}

Bytes occupied by the subclass in memory = bytes occupied by the parent class + bytes occupied by its own members. If there is a virtual function, the virtual function table pointer should be included.

#include <stdio.h>
#include <stdlib.h>
void fun ( double *pl,double *p2,double *s)
{
    s = ( double*) calloc ( 1,sizeof(double));
    *s = *pl + *(p2+1);
}
main( )
{
    double a [2] = {1.1,2.2},b [2] = {10.0,20.0}, *s = a;
    fun (a,b,s);
    printf ( "%5.2f\n",* s) ;
}

Output: 1.10
Take the array name as the function parameter. After executing the fun function, the value of s does not change and still points to a, so the output result is 1.10 and option D is correct.

int main(){
   int pid;
   int num=1;
   pid=fork();
   if(pid>0){
   num++;
   printf("in parent:num:%d addr:%x\n",num,&num);
   }
   else if(pid==0){
   printf("in child:num:%d addr:%x\n",num,&num);
   }
}

The num output in the parent-child process is different, and the num address is the same
Virtual address space. The value of num address is the same, but its real physical address is different.
Implemented under linux, it is found that the address value is really the same.
After fork, the child process copies the data and stack of the parent process.
But because of magic like address relocation,
Therefore, the seemingly identical address space (virtual address space),
In fact, it is a different physical address space.
At the same time, it can be verified that the output address space in c program is actually a virtual address space.

Posted by porta325 on Sat, 21 May 2022 09:50:42 +0300