Home > Back-end >  About return value c address
About return value c address

Time:11-23

The same statement multiple calls it the same when the return value is the function of char * type, the result is wrong, for example
The main program statement
Printf (" [] % s % s ", fun (p1), fun (p2));
The function
Char * fun (p);

The main program execution results for
[a value of 1 1]
This is because what

CodePudding user response:

You stick the code all
Want to know what fun is how to return a result to help you analyze

CodePudding user response:


CodePudding user response:

I don't know.
But if it's level is not in place of the program is easy to make a mistake is to return a local variable addresses, this is safe is not correct

CodePudding user response:

Your code? From the point of your description, don't see why, what is fun function returns the address of the local variable addresses, or global address and static variables, and so on the not clear

CodePudding user response:

reference 1st floor qybao response:
you pasted the code all
Want to know what fun is how to return a result to help you analyze

Header files define the data returned
Char RTN [1024].
Here are the external function logic
Char * fun (char * p) {
Char * LPTR;
The following logic omit wrote ha
The malloc open space LPTR
A series of operations
The LPTR assigned to the RTN in the header file
Free fall LPTR
Return RTN;
}
Logic is roughly such

CodePudding user response:

references 4 building self-confidence boy reply:
your code? From the point of your description, don't see why, what is fun function returns the address of the local variable addresses, or global address and static variables, and so on the not clear?

Header files define the data returned
Char RTN [1024].
Here are the external function logic
Char * fun (char * p) {
Char * LPTR;
The following logic omit wrote ha
The malloc open space LPTR
A series of operations
The LPTR assigned to the RTN in the header file
Free fall LPTR
Return RTN;
}
Logic is roughly such

CodePudding user response:

I am the landlord, because the project information security reasons cannot post code, can only say sorry, roughly (T ^ T)

CodePudding user response:

refer to 6th floor subosen response:
LPTR assignment for the RTN in the header file
Free fall LPTR
Return RTN;

Return a pointer to a wild

CodePudding user response:

Printf (f (1), f (2));

F (1), f (2) before the printf ();

F (1), f (2) two
Forms of function calls in A row, the first malloc [1], such as the structure of the size of A data free fall after it, followed by the second function the same function call, malloc again [due to context, and the environment little change, so I think] operating system most likely they bring you release that piece to you use [such as you are here to create A structure the size of A data 2), and then free it,
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- this time f (1), f (2) returns are wild pointer, but points to the second execution of [f (1) or (2) one of the f] f () function after processing of data: data 2
Followed by printf (, 2 the pointer to the data/wild pointer, dangerous, data pointer/wild pointer, danger of 2)

CodePudding user response:

refer to 6th floor subosen response:
Quote: refer to 4th floor confident boy reply:
your code? From the point of your description, don't see why, what is fun function returns the address of the local variable addresses, or global address and static variables, and so on the not clear?

Header files define the data returned
Char RTN [1024].
Here are the external function logic
Char * fun (char * p) {
Char * LPTR;
The following logic omit wrote ha
The malloc open space LPTR
A series of operations
The LPTR assigned to the RTN in the header file
Free fall LPTR
Return RTN;
}
Logic is roughly such

The original poster is return to global variables, if inconsistent, so have a look at the last modified?

CodePudding user response:

refer to 6th floor subosen response:
Quote: refer to 4th floor confident boy reply:
your code? From the point of your description, don't see why, what is fun function returns the address of the local variable addresses, or global address and static variables, and so on the not clear?

Header files define the data returned
Char RTN [1024].
Here are the external function logic
Char * fun (char * p) {
Char * LPTR;
The following logic omit wrote ha
The malloc open space LPTR
A series of operations
The LPTR assigned to the RTN in the header file
Free fall LPTR
Return RTN;
}
Logic is roughly such

I can't see parameter p do? Then this parameter have what meaning?

CodePudding user response:

11 references, confident boy reply:
Quote: refer to the sixth floor subosen response:
Quote: refer to 4th floor confident boy reply:
your code? From the point of your description, don't see why, what is fun function returns the address of the local variable addresses, or global address and static variables, and so on the not clear?

Header files define the data returned
Char RTN [1024].
Here are the external function logic
Char * fun (char * p) {
Char * LPTR;
The following logic omit wrote ha
The malloc open space LPTR
A series of operations
The LPTR assigned to the RTN in the header file
Free fall LPTR
Return RTN;
}
Logic is roughly such

I can't see parameter p do? Then this parameter have what meaning?

In a series of operations

CodePudding user response:

Give the RTN in the header file LPTR assignment,
If use memcpy or sprintf, this sentence should be no problem,

Can only add in free fall LPTR printing, print the RTN, at this time and see where the problem is,
  • Related