| |

VerySource

 Forgot password?
 Register
Search
View: 2196|Reply: 13

How is this memory released?

[Copy link]

1

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-2-10 20:00:02
| Show all posts |Read mode
I wrote a function a few days ago, and to adapt to the situation with one parameter and two parameters, I thought of writing overload.
Later, I thought, do I have to write an overload? I tried the default parameter method. This is the following piece of code.
The question now is, how to judge whether to release the memory and how to judge whether that memory has been generated?

#include <iostream.h>

int f (int i, int&j = * new int (1))
{

j = i;
return i;
}

void main ()
{
int a, b, c, d = 0;

a = f (d);
b = f (d, c);
cout << "a =" << a << endl;
cout << "b =" << b << endl;
cout << "c =" << c << endl;
cout << "d =" << d << endl;
}
Reply

Use magic Report

0

Threads

55

Posts

44.00

Credits

Newbie

Rank: 1

Credits
44.00

 Invalid IP Address

Post time: 2020-4-5 09:45:02
| Show all posts
int f (int i, int j = 1)
{

j = i;
return i;
}
Reply

Use magic Report

0

Threads

14

Posts

15.00

Credits

Newbie

Rank: 1

Credits
15.00

 China

Post time: 2020-4-5 23:45:01
| Show all posts
No mistake,
1) The function always has a convention, you have to agree that the incoming parameters must be dynamically allocated.
2) Try to use overload instead of default parameters.
Reply

Use magic Report

0

Threads

24

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-4-6 09:15:01
| Show all posts
no method

you should record the pointer
Reply

Use magic Report

0

Threads

37

Posts

28.00

Credits

Newbie

Rank: 1

Credits
28.00

 China

Post time: 2020-4-7 11:30:01
| Show all posts
I don't understand why this place is new.
You lost the pointer, how to delete?
Reply

Use magic Report

0

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-4-9 08:15:01
| Show all posts
What you create should be regarded as j itself, and can only be processed inside the function; assuming that the function is processed inside, what about j?
Reply

Use magic Report

0

Threads

5

Posts

34.00

Credits

Newbie

Rank: 1

Credits
34.00

 China

Post time: 2020-4-9 16:14:23
| Show all posts
int f (int i, int j = 1) It seems that this is also ok.
Reply

Use magic Report

1

Threads

8

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-4-9 20:45:01
| Show all posts
int f (int i, int&j = * new int (1))
What is the definition, not to mention how strange this definition is, j is a reference, is j assigned to the address of new? If the address is changed, you can make it clear to everyone, otherwise you have a problem with the definition.
Reply

Use magic Report

0

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-5-16 23:15:01
| Show all posts
// crt_va.c
/ * The program below illustrates passing a variable
 * number of arguments using the following macros:
 * va_start va_arg va_end
 * va_list va_dcl (UNIX only)
 * /

#include <stdio.h>
#define ANSI / * Comment out for UNIX version * /
#ifdef ANSI / * ANSI compatible version * /
#include <stdarg.h>
int average (int first, ...);
#else / * UNIX compatible version * /
#include <varargs.h>
int average (va_list);
#endif

int main (void)
{
   / * Call with 3 integers (-1 is used as terminator). * /
   printf ("Average is:% d\n", average (2, 3, 4, -1));

   / * Call with 4 integers. * /
   printf ("Average is:% d\n", average (5, 7, 9, 11, -1));

   / * Call with just -1 terminator. * /
   printf ("Average is:% d\n", average (-1));
}

/ * Returns the average of a variable list of integers. * /
#ifdef ANSI / * ANSI compatible version * /
int average (int first, ...)
{
   int count = 0, sum = 0, i = first;
   va_list marker;

   va_start (marker, first); / * Initialize variable arguments. * /
   while (i! = -1)
   {
      sum + = i;
      count ++;
      i = va_arg (marker, int);
   }
   va_end (marker); / * Reset variable arguments. * /
   return (sum? (sum / count): 0);
}
#else / * UNIX compatible version must use old-style definition. * /
int average (va_alist)
va_dcl
{
   int i, count, sum;
   va_list marker;

   va_start (marker); / * Initialize variable arguments. * /
   for (sum = count = 0; (i = va_arg (marker, int))! = -1; count ++)
      sum + = i;
   va_end (marker); / * Reset variable arguments. * /
   return (sum? (sum / count): 0);
}
#endif
Reply

Use magic Report

2

Threads

9

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

Post time: 2020-6-26 19:45:02
| Show all posts
The method on the first floor is very good, you can directly plug a default value to it
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