C/C ++ FAQ: (for beginners ).

Source: Internet
Author: User

Give yourself a few more minutes and analyze it with patience. Don't look for experts to help you solve problems as you habitually encounter them. Be patient, think more, read more, and practice more. You are a master.
The main purpose is to summarize some common errors in C/C ++, which can be easily referenced or written. (My referenced friends will not mind it ..), naturally, this is not detailed enough. If you are interested in the specific issues involved, You can Google. There is no waste of time or space here.
I have sorted out some items for the time and energy. Please add more or correct them.
If you have any questions, I hope you can check this post first. It may be helpful to you and may save some repetitive questions.
/***** No header file is added to all codes. To run the command, add the header file. *************************/

1: Try to use cout to output the global class destructor. (It may be a problem with vc6. If your compiler doesn't have a problem, it's good. Don't spray me)

Reference from whillcoxdennis question: http://topic.csdn.net/u/20090302/14/ca44881f-9664-4be8-9687-1dd098612d11.html
Class cdemo
{
Public:
Cdemo (const char * Str );
~ Cdemo ();
PRIVATE:
Char name [20];
};
Cdemo: cdemo (const char * Str)
{
Strcpy (name, STR );
Cout <"cout: construction called for" <name <Endl;
}

Cdemo ::~ Cdemo ()
{
Cout <"cout: destruction called for" <name <Endl;
Printf ("printf: destruction called for % s/n", name );
}

Static cdemo globleobject ("globeobject"); // This is our concern. cout: destruction called for globeobject does not appear.
Void main ()
{
Cdemo localobjectinmain ("localobjectinmain ");
Cdemo * pheapobjectinmain = new cdemo ("heapobjectinmain ");
Cdemo * pheapobjectinfunc = new cdemo ("heapobjectinfunc ");
Static cdemo staticobject = "staticobject ";

}
The analysis structure of globeobject is not displayed here. Why?
Add printf and you will know why.
Because the cout structure is before the globleobject structure.

2. Questions about vc6:
Some people say that it is true to cherish life and stay away from vc6.
For several bugs such as friend and template, vc6 is recommended for vs testing.
For example, if the definition of the friend symbol is in vitro, an error is reported.
Template nesting:
Vector <vector <int> bad; // error, vc6 recognizes a two-dimensional container> as an output symbol.
Vector <vector <int> good;
In addition, if there are a bunch of warnings in the compiling Template under vc6, you should add
# Pragma warning (Disable: 4786)

3. the arguments are not specific, and the pointer is unclear. I will not go into details. Let's look at the following code and clarify the three sections. for more information, see Dr. Lin's <thoroughly solve the C pointer>
Void exchg1 (int x, int y)
{
Int TMP;
TMP = X;
X = y;
Y = TMP;
Printf ("exchg1: x = % d, y = % d/N", x, y );
}
Void exchg2 (Int & X, Int & Y)
{
Int TMP;
TMP = X;
X = y;
Y = TMP;
Printf ("exchg2: x = % d, y = % d/N", x, y );
}

Void exchg3 (int * X, int * Y)
{
Int TMP;
TMP = * X;
* X = * Y;
* Y = TMP;
Printf ("exchg3: x = % d, y = % d/N", * X, * y );
}

Void main ()
{
Int A = 4, B = 6;
Exchg1 (A, B );
Printf ("A = % d, B = % d/N", a, B );
Exchg2 (A, B );
Printf ("A = % d, B = % d/N", a, B );
Exchg3 (& A, & B );
Printf ("A = % d, B = % d/N", a, B );
}

4. C-style string and standard library string
Int I;
Char * Ch = "cchars"; // This is a C-style string ending with '/0'
Char MCH [] = "mchars ";
String STR (CH); // constructor of the string class receives CH and converts it
I = strcmp (CH, STR); // error. Some people prefer to input the string class as a parameter to C string functions such as strcmp. This will definitely cause problems.
Ch [0] = 'D'; // error. The memory obtained by char * Ch is const, which is equivalent to const char * Ch = "cchars". It cannot be modified.
MCH [0] = 'F'; // OK

5. pointer and memory application and out-of-bounds
Int * ptr1, ptr2;
* Ptr1 = 10; // error, ptr1 has not applied for memory, not initialized, and the result is undefined. It seems that it is not prone to errors, but many people do not apply for memory when writing a linked list.
Ptr1 = (int *) malloc (sizeof (INT ));
* Ptr1 = 10; // OK;
* Ptr2 = 20;/Error
Ptr2 = ptr1;
* Ptr2 = 20; // OK, which is actually the same region that ptr1 and ptr2 point.
Ptr2 = new int [10];
Ptr2 [10] = 5; // error. It is silly to write or to you, but in fact many times you may have made these mistakes without knowing it.
Free (ptr1 );
Delete ptr2; // error. In this way, delete will only parse the first element.
Delete [] ptr2; // OK

I want to talk more about pointers, but this problem involves the function scope. So I put it in article 9th.

6. About byte alignment
Struct ST1
{
Char P1; [0]
Char P3; [1]
Short P2; [2] [3]
Int P4; [4] [5] [6] [7]
};

Struct st2
{
Char P1; [0]
Short P2; [2] [3] // occupies 2 bytes. The starting address is two integer times.
Char P3; [4]
Int P4; [8] [9] [10] [11] // 4 bytes. The starting address is an integer multiple of 4.
};

Why is sizeof (ST1) = 8; sizeof (st2) = 12, 2 different? View comments.

7. Output display of Data smaller than 32 bytes

8. Io stream
Scanf ("A = % d, B = % d", & A, & B); // many people like this, and then enter 3 4 to ask, why is the program running wrong? Enter 3, 4 here.
Scanf ("A = % db = % d", & A, & B); // This is 3 4
The input buffer can easily contain characters that you don't need or are prone to problems,
We recommend that you refresh it if it is null.
Fflush (stdin );

9. variable scope: Actually, it is also a pointer application problem.
Reference is a post http://topic.csdn.net/u/20090302/17/900b3797-3642-4569-a623-dc0f8ebd8401.html that rwjbjn1 asked? Seed = 1325371970
// Code 1
Int ()
{
Int test = 10;
Return test;
}
Int main ()
{
Int A = ();
Printf ("% d/N", );
Return 0;
}
// Code 2
Char * ()
{
Char P [] = "Hello World ";
Return P;
}
Int main ()
{
Char * STR = NULL;
STR = ();
Printf ("% s", STR );
}
Code 1 is okay, but Code 2 has a problem (there can be results, but the results are not necessarily correct, this is an undefined operation). Why can I analyze it myself and check the post.


10. Data Overflow

Data overflow is simple, but the code is long. someone makes a mistake without knowing it.
Int main ()
{
Int B = 1294754725065626467; // The int of a 32-bit machine only supports the valid count of 32-bit (2 ^ 32-1)
Char A = 199; // The 8-bit char data can only be counted from 0 to 127.
Cout <A <Endl;
Cout <B <Endl;
Printf ("% C", );
Printf ("% C/N", );
}
Don't try to explain the results. If it overflows, there is nothing to explain. There are all weird things.

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.