SIZEOF CONST preprocessing topic:

Source: Internet
Author: User
Tags array length assert constant int size min strlen

SIZEOF CONST pretreatment Topics:
1. SizeOf Related Series issues

A. For struct S{char a;int B}; sizeof (s) = 8; Because when the element length in the structure is less than the number of processor bits (32 bits = 4 bytes), the longest data element in the struct is the alignment condition, a is aligned by 1 bytes, b is aligned by 4 bytes, so s defaults to its parameter is 8

struct A{short a1;short a2;short a3};sizeof (A) = 6; For the same reason, the structure is short (2-byte aligned) by the longest element.
B. for int a[200]; sizeof (a) = 200* sizeof (int) = 800; Evaluation of the entire array size

int* a = new int[200]; sizeof (a) = 4; Evaluating the size of a pointer

Char str[]= "012345"; sizeof (str) = 7; STR is an array of characters, the initial size of the array is undefined, determined by the specific value "012345", 6*sizeof (char) = 6, and the implied "/0", so altogether 7 bits.

Char str[]= "s/n"; sizeof (str) = 3; The carriage return "/n" is a character that allows you to view the ASCII table. Plus/R,/T and so on characters
C. There is also this use of bit fields, where the elements are up to 1 bytes in size =8 bits, the elements are aligned according to 1 bytes, sizeof (b) = 1 + 1 (4 Bits+2bits < 8 bits) + 1 (3bits) = 3.
struct B
{
Char A:8;
Char B:4;
Char C:2;
Char D:3;
};

Write the result of the operation:
Union V {
struct X {
unsigned char s1:2;
unsigned char s2:3;
unsigned char s3:3;
} x;
unsigned char c;
} V;
V.C = 100;
printf ("%d", V.X.S3);

100 of the 2 binary is 1100100 to remove the back of the 5-bit more than 11 into the X.S3 results: 3
D. Evaluation of empty classes class A {}; sizeof (A) = 1; The default empty class is a placeholder

For virtual function Class a{virtual Test ()}; Class B:public a{};sizeof (A) =4;sizeof (B) = 4 Any class that contains a virtual function has a vptr pointer to the virtual function table vtable. sizeof (VPTR) = 4

class a{static int A;}; sizeof (A) = 1; For static member variables that are assigned to the global store, a is still equivalent to an empty class.

Class C:public virtual A {}; sizeof (C) = 4; For virtual inherited classes that have virtual function tables, empty Class C contains vptr.

E. The following descriptions and definitions are available:
typedef union {long I; int k[5]; char c;} DATE; sizeof (int) = 20
struct data {int cat; DATE Cow; Double Dog;} Too 4+20+8 = 32
DATE Max;
The statement printf ("%d", sizeof (struct date) +sizeof (max)) is executed with the result that: 52

For union unions, the maximum element length is taken as the Union size.
F. Allocating memory using malloc or new, void *pp = malloc (10); sizeof (p) = 4; Like a pointer, sizeof can only measure the length of a static array and cannot detect dynamically allocated or external array sizes
H. The following function output: For char str[100] or char str[] parameters are degraded to char* str, such functions are possible even if passed into Char s[10].

void Func (char str[100])
{
printf ("%d/n", sizeof (str));
}
Char s[10]; function is not checked for array length
Func (s);
Result: sizeof (char*) =4

How to force str to be a 100-bit array. Can declare this char (&STR) [100]

Understanding Order: 1.str is declared as a reference 2. Reference a 100 element array of 3. array element each is an int size

void Func (char (&STR) [100])
{
printf ("%d/n", sizeof (str));
}
Char s[100];
Func (s); A 100-bit length char array must be given here
Result: 100*sizeof (char) = 100
2. CONST Common topics:

A. Const and #define有什么不同

Answer: 1. Const constants have data types, and macros do not have data types. The compiler can type-check for const constants, and there is no type check for macro-only character substitution.

2. Some compilers can debug const constants, but they cannot debug macro constants

3. Const can be used to modify function parameters, function return values, C + + can also be used to modify functions, define a member function is a constant function
3. Write a standard macro min, this macro enter two parameters and return the smaller one

Answer: #define MIN (A) (A) <= (B)? (A):(B))

1. Macros are the only convenient way to generate embedded code.

2. The triple conditional operator can produce code that is more optimized than If-then-else for the compiler.

3. The parameters in the macro must be enclosed in parentheses

4. What is the difference between an inline function and a macro?

Answer: Inline functions can speed up the execution of a program when compared to normal functions, because there is no need to interrupt the call. The compile-time inline function code is embedded directly into the target code, and the macro is simply a character substitution. inline functions do type checking and are more secure and reliable relative to macro.

Inline is only used for the following situations:

1. A function is called continuously.

2. The function has only a few simple lines, and the function does not contain a for, while, switch, and other statements.

5. Const symbolic constants;
(1) const char *P
(2) Char const *P
(3) char * const P
Explain the differences between the above three descriptions:
1 and 2 are the same, if the const is on the left side of the asterisk, the const is used to decorate the variable that the pointer points to, that is, the pointer points to a constant, and the pointer to the object's contents cannot be modified
If the const is on the right side of the asterisk, the const is the modifier pointer itself, which means that the pointer itself is a constant and cannot modify the pointer's pointing


2) write the binary lookup code.
int bfind (int* a,int len,int val)
{
int m = LEN/2;
int l = 0;
int r = Len;
while (L!=m && r!= m)
{
if (A[m] > val)
{
r = m;
m = (m+l)/2;
}
else if (A[m] < val)
{
L = m;
m = (m+r)/2;
}
Else
return m;
}
return-1; Not found
}

Common String Interview questions:
1) write the code that looks up the number of substrings in the string.
int Count1 (char* str,char* s)
{
char* S1;
char* S2;
int count = 0;
while (*str!= '/0 ')
{
S1 = str;
S2 = s;
while (*s2 = = *s1&& (*s2!= '/0 ') && (*s1!= '/0 '))
{
s2++;
s1++;
}
if (*s2 = = '/0 ')
count++;
str++;
}
return count;
}

2) Find the first matched substring position, if return is S1 length len1 means no found
size_t Find (char* s1,char* s2)
{
size_t i=0;
size_t len1 = strlen (S1)
size_t len2 = strlen (s2);
if (len1-len2<0) return len1;
for (; i<len1-len2;i++)
{
size_t m = i;
for (size_t j=0;j<len2;j++)
{
if (S1[m]!=s2[j])
Break
m++;
}
if (J==len)
Break
}
Return i<len1-len2?i:len1;
}

3) Implement the strcpy function
Char *strcpy (char *destination, const char *source)
{
ASSERT (Destination!=null&&source!=null);
char* target = Destinaton;
while (*destinaton++=*source++);
return target;
}
The number of occurrences is quite frequent
4) Implement the STRCMP function
int Strcmp11 (char* l,char* R)
{
ASSERT (l!=0&&r!=0);
while (*l = = *r &&*l! = '/0 ') l++,r++;
if (*l > *r)
return 1;
else if (*l = = *r)
return 0;
return-1;
}

5) Implement string rollover
void Reserve (char* str)
{
ASSERT (str = NULL);
char * p1 = str;
char * p2 = str-1;
while (*++P2); General requirements cannot be used strlen
P2-= 1;
while (P1&LT;P2)
{
char C = *P1;
*p1++ = *P2;
*p2--= C;
}
}

6), using the pointer method, the string "Abcd1234efgh" before and after the display
Do not use Strlen to find the string length, so there is no point.
The code is as follows:
Char str123[] = "ABCD1234EFGH";
char * p1 = str123;
char * p2 = str123-1;
while (*++P2);
P2-= 1;
while (P1&LT;P2)
{
char C = *P1;
*p1++ = *P2;
*p2--= C;
}

7) given strings A and b, the largest common substrings in outputs A and B. For example a= "aocdfe" b= "PMCDFA" Output "CDF"
#i nclude<stdio.h>
#i nclude<stdlib.h>
#i nclude<string.h>

Char *commanstring (char shortstring[], char longstring[])
{
int I, J;
Char *substring=malloc (256);
if (Strstr (longstring, shortstring)!=null)//If ..., then return to Shortstring
return shortstring;

For (I=strlen (shortstring) -1;i>0; i--)//Otherwise, start loop calculation
{
for (j=0; J<=strlen (shortstring)-I.; j + +)
{
memcpy (substring, &shortstring[j], i);
Substring[i]= '/0 ';
if (strstr (longstring, substring)!=null)
return substring;
}
}
return NULL;
}


Main ()
{
Char *str1=malloc (256);
Char *str2=malloc (256);
Char *comman=null;

Gets (STR1);
Gets (STR2);

if (strlen (str1) >strlen (STR2))//Put short strings in front
Comman=commanstring (str2, str1);
Else
Comman=commanstring (str1, str2);

printf ("The longest Comman string is:%s/n", Comman);
}

8) Determine if a string is a palindrome
int Isreversestr (char *str)
{
int i,j;
int found=1;
if (str==null)
return-1;
char* p = str-1;
while (*++p!= '/0 ');
--p;
while (*str==*p&&str<p) str++,p--;
if (str < p)
Found = 0;
return found;
}

9) Write function to complete the memory copy
void* memcpy (void *dst, const void *SRC, unsigned int len)
{
Register char *d;
Register char *s;
if (len = = 0)
return DST;
if (DST > SRC)//Consideration of coverage
{
D = (char *) DST + len-1;
s = (char *) src + len-1;
while (Len >= 4)//loop expansion for improved execution efficiency
{
*d--= *s--;
*d--= *s--;
*d--= *s--;
*d--= *s--;
Len-= 4;
}
while (len--)
{
*d--= *s--;
}
}
else if (DST < SRC)
{
D = (char *) DST;
s = (char *) src;
while (Len >= 4)
{
*d++ = *s++;
*d++ = *s++;
*d++ = *s++;
*d++ = *s++;
Len-= 4;
}
while (len--)
{
*d++ = *s++;
}
}
return DST;
}

10) write a function whose prototype is int Continumax (char *outputstr,char *intputstr)
Function:
Finds the longest consecutive number string in the string, returns the length of the string, and takes the longest number string to one of the function parameters outputstr the memory. For example: When the first address of "abcd12345ed125ss123456789" is passed to Intputstr, the function returns
9,outputstr refers to a value of 123456789
int Continumax (char *outputstr, char *inputstr)
{
Char *in = inputstr, *out = Outputstr, *temp, *final;
int count = 0, maxlen = 0;

while (*in! = '/0 ')
{

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.