BDCOM pen questions

Source: Internet
Author: User

Memory Allocation in C:

1.

 
# Include <stdio. h>
 
# Include <stdlib. h>
 
# Include <String. H>
 
# Include <assert. h>
 
 
 
VoidGetmemory (Char** P)
 
{
 
* P = (Char*) Malloc (100 );
 
}
 
 
 
VoidTest ()
 
{
 
Char** STR;
 
Getmemory (STR );
 
 
Strcpy (* STR,"Hello World");
 
Printf ("STR is % s", * Str );
 
}
 
 
 
IntMain ()
 
{
 
Test ();
 
 
 
System ("Pause");
 
Return0;
 
}

Result: A running error occurs.

Getmemory does not properly allocate memory. Str starts to be null. After the input parameter is passed, P is null. * P will certainly encounter an error when referencing it.

Correct correction:

 
# Include <stdio. h>
 
# Include <stdlib. h>
# Include <String. H>
 
# Include <assert. h>
 
 
 
VoidGetmemory (Char** P)
 
{
 
Assert (P! = NULL );
 
* P = (Char*) Malloc (100 );
 
}
 
 
 
VoidTest ()
 
{
 
Char* STR = NULL;
 
Getmemory (& Str );
 
 
 
Strcpy (STR,"Hello World");
Printf (STR );
 
}
 
 
 
IntMain ()
 
{
 
Test ();
 
 
 
System ("Pause");
 
Return0;
 
}

Certificate ------------------------------------------------------------------------------------------------------------------------------------------------

2.

 
# Include <stdio. h>
 
# Include <stdlib. h>
 
# Include <String. H>
# Include <assert. h>
 
 
 
Char* Getmemory ()
 
{
 
Return "Hello World";
 
}
 
 
 
VoidTest ()
 
{
 
Char* STR = NULL;
 
STR = getmemory ();
 
Printf (STR );
 
}
 
 
 
IntMain ()
 
{
 
Test ();
 
 
System ("Pause");
 
Return0;
 
}

Print: Hello World

The pointer returned by getmemory points to the read-only zone, but the test function does not perform write operations, so the print is correct.

Certificate -----------------------------------------------------------------------------------------------------------------------------------------------

3.

 
# Include <stdio. h>
 
# Include <stdlib. h>
 
# Include <String. H>
 
# Include <assert. h>
 
 
 
StructPara
 
{
CharSTR [11];
 
CharLen;
 
};
 
 
 
VoidGetmemory (CharP [11],Char* Len)
 
{
 
* Len = strlen ("Hello World");
 
Strcpy (p,"Hello World");
 
}
 
 
 
VoidTest ()
 
{
 
StructPara;
 
IntI;
Getmemory (A. STR, & A. Len );
 
Printf ("% D", A. Len );
 
For(I = 0; I <A. Len; ++ I)
 
{
 
Printf ("% C", A. Str [I]);
 
}
 
}
 
 
 
IntMain ()
 
{
 
Test ();
 
 
 
System ("Pause");
 
Return0;
 
}

 

Certificate -----------------------------------------------------------------------------------------------------------------------------------------------

4.

 
# Include <stdio. h>
 
# Include <stdlib. h>
 
# Include <String. H>
 
# Include <assert. h>
 
 
 
VoidTest ()
 
{
 
Char* STR = (Char*) Malloc (100 );
 
Strcpy (STR,"Hello");
 
STR + = 6;
 
Free (STR );
If(STR! = NULL)
 
{
 
Strcpy (STR,"World");
 
Printf (STR );
 
}
 
}
 
 
 
IntMain ()
 
{
 
Test ();
 
 
 
System ("Pause");
 
Return0;
 
}

GCC print: World

VC: assertion failed, error during running

STR has been removed by 6 digits, and then free. I don't understand what this means. It should not be allowed !!! (VC does Assert failure, running error, but GCC is useless)

But STR must not be null. The memory to which it points should be uncertain. However, it is a pity that errors are not reported during compilation and running.

Last question:
Design a calculator. It is known that all input is stored in char buff [].

Last question:
Knapsack problems, recursion and non-recursionAlgorithm

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.