| |

VerySource

 Forgot password?
 Register
Search
View: 2707|Reply: 18

What is the difference between new char (50) and new char [50]?

[Copy link]

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-1-24 15:40:02
| Show all posts |Read mode
Which one should be used to dynamically request a piece of memory when the program is running?
What is the difference between the two?
Reply

Use magic Report

0

Threads

17

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-2-10 21:45:01
| Show all posts
new char (50) is equivalent to applying only one byte, and its value is ASCII = 50 character
new char [50] applies for space of 50 characters
Reply

Use magic Report

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-2-11 11:45:01
| Show all posts
For char * p = new char [50];
I can * (p + 51) = 'a';
Or p [51] = 'a';
Even p [-10] = 'a';
1. Access the memory beyond the scope of the application and assign a value to it. Is this too dangerous?

2. After char * p = new char [50] in a program block, how to release it?
for (char * q = p; q <p + 50; p ++)
    delete q;
Reply

Use magic Report

0

Threads

5

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-2-11 14:00:01
| Show all posts
1. Address is dangerous
2.delete [] q;
Reply

Use magic Report

0

Threads

2

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-2-11 16:45:02
| Show all posts
new char (50) is equivalent to applying only one byte, and its value is ASCII = 50 character
new char [50] applies for space of 50 characters
wrong! !!
The C ++ compiler gives two ways of writing to take into account the user's habits.
The former looks like function parameters.
The latter is for considerations like array applications.
There is no difference or problem between the two writing methods in actual operation.
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-2-12 09:15:02
| Show all posts
makeyou

...
There is no difference or problem between the two writing methods in actual operation.
===========================================
You said it so categorically, you are wrong
Reply

Use magic Report

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-2-12 10:00:01
| Show all posts
These two are still different
int * p = new char (97); // The string at p: "a" starts with a, followed by some spaces, it is estimated to end at\0
int n = strlen (p); // n = 16 the length of the string is variable

int * p = new char [97]; // pThe output string is: "Tun Tun Tun Tun Tun Tun Tun Tun Tun Tun Tun Tun Tun Tun Tun Tun Tun Tun Tun Tun Tun Tun Tun Tun Tun Tun Tun "Tuntun Tuntun Tuntun Tuntun Tuntun Tuntun Tuntun Tuntun"
int n = strlen (p); // n = 112 The value is uncertain, it is estimated that it will end when it encounters\0 in memory.

I'm confused about these two ways of writing! Which one should I use, and how much memory are allocated to them in these two ways?
Reply

Use magic Report

0

Threads

6

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-2-12 16:00:01
| Show all posts
Upstairs, right?
"new char (50) is equivalent to applying only one byte, and its value is a character with ASCII = 50" is right
#include <iostream.h>
int main ()
{
char * p = new char (50);
cout << * p << endl;
char * p1 = new char [50];
p1 = "pananwei";
cout << p1 << endl;
return 0;
}
The output is: 2
         pananwei
Reply

Use magic Report

0

Threads

5

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-2-14 10:15:01
| Show all posts
agree:
new char (50) is equivalent to applying only one byte internally, and its value is ASCII = 50. The character "is right
Disagree with whatmakeyousays
Reply

Use magic Report

0

Threads

9

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-2-15 21:30:01
| Show all posts
Themakeyoubrothers did not understand that C ++ constructors are also valid for basic types ...
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