| |

VerySource

 Forgot password?
 Register
Search
View: 1139|Reply: 6

Why doesn't the function return value exist? ? ?

[Copy link]

3

Threads

5

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-10-9 11:30:01
| Show all posts |Read mode
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char* strsave(char* s)
{
//In the first case, you can return
/*
char* p = new char(10);
strcpy(p,"abc");
*/
To
//The second case cannot return
char p[10] = {'a','b','c'};
To
return p;
}
void main(void)
{
char* str = "China";
char* cp;
cp = strsave(str);
printf("str = %s, cp = %s\n", str, cp);
}
Reply

Use magic Report

2

Threads

20

Posts

12.00

Credits

Newbie

Rank: 1

Credits
12.00

 China

Post time: 2020-10-9 11:45:01
| Show all posts
In the second case, p is a dynamic local variable, and the space will be released after the function call ends, so that the content pointed to by the pointer you return is unexpected
In the first case, because new is used to dynamically allocate memory, this will not happen.
Must not return the address of a dynamic local variable
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-10-9 13:00:02
| Show all posts
Of course, the address of a local variable cannot be returned, because the address will be revoked if it leaves the function body.
The new one is fine.
Reply

Use magic Report

0

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-10-9 13:15:01
| Show all posts
char* p = new char(10);
Dynamically allocate data in the heap. Once allocated, delete must be used to clear the data, otherwise the data information will remain

char p[10] = {'a','b','c'};
Allocated in the stack, only valid in the scope, and disappears out of the scope
Reply

Use magic Report

0

Threads

37

Posts

28.00

Credits

Newbie

Rank: 1

Credits
28.00

 China

Post time: 2020-10-9 19:00:01
| Show all posts
Search C++ memory allocation, stack memory and heap memory
Reply

Use magic Report

0

Threads

8

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

Post time: 2020-10-9 19:45:01
| Show all posts
The first case:
use "new" to apply space is available through the main.
Reply

Use magic Report

1

Threads

39

Posts

27.00

Credits

Newbie

Rank: 1

Credits
27.00

 China

Post time: 2020-10-9 21:15:01
| Show all posts
new is a method of applying memory beyond the scope of general nature.
That is, after new, the language does not manage your memory space, but you manage it yourself.

In addition, the language features will automatically reclaim some memory space for you.
Such as
void fun()
{
int i; // The memory space of i here is applied
}
// was released outside.
//This is the so-called life cycle of the scope, that is, the memory space that will be released if the scope is exceeded.

The scope is divided into (non-standard naming):
1. The scope of the function is the scope of the function, and it will be released when it jumps out
2. The expression scope is an expression application, and the memory that is deleted if the expression exceeds this expression
3. Program scope, the memory released at the end of the program.

It should be noted that although there are these scope restrictions, it is not necessary to use new to pass variables. As long as the internal scope of the variable
Copy to the outer range can be passed. But having said that, the array can not be copied directly, the array name is passed its address, and use
This visit is still the original space, which will cause problems.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list