Difficult interview questions

Source: Internet
Author: User
Difficult interview questions

Q:
If you want to write a function (software development Customer) for the customer, this function is used to process an array
The entry is void test (int * P)
The array will be operated in the function. There are two types:
1. The input array is statically allocated.
2. The input array is dynamically allocated (such as malloc ).
So how can we determine the distribution of these two conditions?

Wanguodu (foot Text D)Answer:

Not modestly, I think I have provided the answer.
As for the use of the _ msize function, this is the patent of VC, and other drawbacks have been discussed before. Use free? When it releases non-dynamically allocated memory and memory allocated on the stack, an exception is thrown. Obviously, static memory and stack memory cannot be distinguished. The EBP and ESP are obtained by means of Assembly to determine whether the stack memory is acceptable. But how can we distinguish static memory or heap memory?
To sum up, it is the most practical way to use compilers to process variables and the public policies for dynamic memory allocation. I believe that experienced C/C ++ developers will not deny the following facts:
1. the compiler always distributes static variables in the program at the low end of the memory, including data and BSS. The data area stores initialized global variables. The BSS area stores uninitialized global variables in a centralized manner, and the BSS area only stores (relative) Start address and length information in the executable file to save the external storage space, it is expanded only when the memory is loaded and initialized to 0;
2. Address Space Layout of processes: the Environment zone, command line parameters, and code zone are not considered. Static zones (including data zone and BSS zone) are arranged in sequence by address from low to high) + heap + stack. The heap and stack share a large segment of High-address memory, which is used to store the dynamically allocated memory (such as malloc) of the program and run the function call stack (including the local variables of the current function, return address, some storage register values, etc.), but the heap memory allocation method is from low to high growth, while the stack is from high to low growth.
With this analysis, we can easily draw the following conclusion: "various memory types, such as static memory, dynamically allocated memory, and stack, are stored in blocks, there is no crossover between each other, so for any given legal memory address P, the distance between it and the same class address A1 and the distance between it and the heterogeneous memory address A2, there is always the following formula:
| P-a1 | <| p-a2 |.
With this conclusion, we can think that we can set three variables in the static zone, heap, and stack respectively, and compare them with the user-input addresses (such as Q) through their addresses, the block where the variable closest to Q is located, that is, the block where Q is located, to determine whether Q is statically allocated and dynamically allocated is a local array. I have already provided the specific procedures in the previous article. I will try to paste them here for you to read:

# Include <stdio. h>
# Include <stdlib. h>

# Define smaller (A, B) (a)> (B )? (B): ())
# Define min (a, B, c) smaller (a, B), c)
# Define ABS (x) <0? -(X): (x ))

Void test (int * P)
{
Int X;/* X in stack */
Static int y = 1;/* Y is in the static zone */
Static int * q = malloc (sizeof (INT);/* the memory that Q points to is located in the heap. Q is defined as static to only malloc once, otherwise, memory fragments may easily occur */

Unsigned int u = ABS (unsigned INT) q-(unsigned INT) P);/* calculate the distance between Q and p */
Unsigned int v = ABS (unsigned INT) & X-(unsigned INT) P);/* calculate the distance between Q and & x */
Unsigned int W = ABS (unsigned INT) & Y-(unsigned INT) P);/* calculate the distance between Q and & Y */
Unsigned int S = min (U, V, W);/* minimum distance */

If (P = NULL)/* sanity check */
{
Printf ("test () in file % s: NULL pointer parameter./N", _ file __);
Return;
}

If (S = U)
{
Printf ("P located in heap./N");/* closest to Q, so P indicates that the heap exists */
}
Else if (S = V)
{
Printf ("P located in stack./N");/* closest to & X, so P indicates that the stack exists */
}
Else
{
Printf ("P located in data section./N");/* closest to & Y, so P indicates that the memory is statically allocated */
}

}

Int A [100] = {1 };

Int main (void)
{
Int * B = (int *) malloc (100 );
Int C [100];

Test (a);/* test static memory */
Test (B);/* test dynamic memory */
Test (c);/* test stack memory */

Free (B );

Return 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.