| |

VerySource

 Forgot password?
 Register
Search
Author: athena0554

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

[Copy link]

2

Threads

54

Posts

34.00

Credits

Newbie

Rank: 1

Credits
34.00

 China

Post time: 2020-2-17 07:00: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
================================
This is true
Reply

Use magic Report

0

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-2-17 11:15: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
================================
Correct
Reply

Use magic Report

0

Threads

15

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

Post time: 2020-2-17 22:45:01
| Show all posts
That's right:
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

new char (50); // Equivalent to the usage of the constructor
Reply

Use magic Report

0

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-3-9 16:45:02
| Show all posts
TO:leonim(
--- quote--
    int main ()
    {
       char * p = new char (50);
       cout << * p << endl;
       char * p1 = new char [50];
       p1 = "pananwei";
       cout << p1 << endl;
       return 0;
   }
:Reply
The output is correct, but there is something wrong with your code. You cannot use the equal sign for copying strings.
p1 = "pananwei"; This sentence is "re-assign the pointer p1" and gave up the "new char [50]" just applied,
This code caused a memory leak.

TOKTPOPO
"Themakeyoubrothers didn't understand that C ++ constructors are also valid for basic types ..."
This statement is true, and you can think of primitive types as having constructors.

After so much discussion, it seems that there is no result ^-^
I think that 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

Try changing char to an actual class
class obj
{
    pubic:
    obj ()
    {cout << "obj ()" << endl;}
    ~ obj ()
    {cout << "~ obj ()" << endl;}
};
Reply

Use magic Report

0

Threads

2

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-3-25 10:45:01
| Show all posts
A C language title: This is a game for Chinese folk. The two people report numbers in turn from 1. Each person can report one number or two consecutive numbers. Whoever reports to 30 first will be the winner. Please programmatically. Hope someone can help me answer, thank you!
Reply

Use magic Report

0

Threads

2

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-3-25 21:30:01
| Show all posts
There are 52 cards in a deck of poker. When playing bridge, the cards should be distributed to four people. Please design a program to complete the work of automatic licensing.
Requirements: Spades are represented by S (Spaces); hearts are represented by H (Hearts); squares are represented by D (Diamonds); plum blossoms are represented by C (Clubs).
Reply

Use magic Report

1

Threads

2

Posts

0.00

Credits

Newbie

Rank: 1

Credits
0.00

 China

Post time: 2020-3-27 12:52:39
| Show all posts
New Char (50) points to a bit-by-bit character that applies for several bytes of memory space, and this character happens to be equal to a string of 50 characters. Moreover, the character space address used by this string on different hosts can only be the space of one character, and the number of characters identified by different hosts can be seen by the benevolent and the wise. The general calculation unit establishes it as the storage unit of the character space size of the address segment of the heap segment part of the header information of a program or file;
New Char [50] points to a memory address space unit that applies for several bytes. It is a space of 50 bytes plus a reserved bit and a control bit plus a check bit. size. General computing devices use the stack address part of the control segment code that contains the storage frame and storage structure, and the code segment of the function in the library and function as the address space of the main storage unit, and its stored address bits are already bytes. Is the main storage unit;
Reply

Use magic Report

0

Threads

9

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

Post time: 2020-3-27 23:30:02
| Show all posts
The answer has been given above.
It is best to reopen a post upstairs.
Reply

Use magic Report

0

Threads

8

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

Post time: 2020-3-30 14: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

This should be
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