| |

VerySource

 Forgot password?
 Register
Search
View: 2572|Reply: 17

I do n’t understand a question about enrollment, everyone helps

[Copy link]

1

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-1-25 22:40:01
| Show all posts |Read mode
I wrote a program like this

char * z = new char [6];
z = "abcde";
for (int k = 0; k <5; k ++)
{
cout << hex <<&z + k << endl;
cout << * (z + k) << endl;
}

delete [] z;

The previous operation is quite normal, and then delete [] z; there, it can not run, what is the reason? I guess it has something to do with that const, please help.

Then, in order to run this program, I wrote this:

char z [] = {'a', 'b', 'c', 'd', 'e'};
Ranch
          for (int k = 0; k <5; k ++)
{
cout << hex <<&z + k << endl;
cout << * (z + k) << endl;
}

No need to delete, there is nothing wrong, but I suddenly found a strange phenomenon. This is its output:
0012FF24
a
0012FF29
b
0012FF2E
c
0012FF33
d
0012FF38
e
How can their memory locations differ by five? My original experiments were all four bits apart. Isn't a computer word four bytes long? Why is that?
Reply

Use magic Report

1

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-2-15 21:15:01
| Show all posts
Everyone helps, C pointer is really annoying.
Reply

Use magic Report

0

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-2-16 17:15:02
| Show all posts
char * z = new char [6];
z = "abcde"; // pointed elsewhere ... you didn't use your dynamically allocated memory to save "abcde"
                      // strcpy (z, "abcde"); // That's fine
for (int k = 0; k <5; k ++)
{
cout << hex <<&z + k << endl;
cout << * (z + k) << endl;
}

delete [] z;
Reply

Use magic Report

0

Threads

7

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-2-16 18:45:01
| Show all posts
z = "abcde";

Change the pointer of z, and of course an error will occur when delete.

strcpy (z, "abcde");
Reply

Use magic Report

2

Threads

54

Posts

34.00

Credits

Newbie

Rank: 1

Credits
34.00

 China

Post time: 2020-2-16 19:45:01
| Show all posts
char * z = new char [6];
========================
This sentence your z points to the memory allocated on the heap

z = "abcde";
======
In this sentence you point z elsewhere

delete [] z;
======
This sentence you want to release the memory on the heap, but z does not point to the heap memory, so
Reply

Use magic Report

2

Threads

54

Posts

34.00

Credits

Newbie

Rank: 1

Credits
34.00

 China

Post time: 2020-2-16 21:15:01
| Show all posts
Solution
Save a pointer to the memory on the heap
For release
char * z = new char [6];
char * p = z; // here
z = "abcde";
// ...
delete [] p; // ok
Reply

Use magic Report

2

Threads

54

Posts

34.00

Credits

Newbie

Rank: 1

Credits
34.00

 China

Post time: 2020-2-16 21:45:01
| Show all posts
Personal opinions, experts advise
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 Invalid IP Address

Post time: 2020-3-3 11:45:01
| Show all posts
second question........
cout << hex <<&z + k << endl; The output is an address other than the array z.
Reply

Use magic Report

0

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-3-7 18:45:01
| Show all posts
for (int k = 0; k <5; k ++)
{
cout << hex <<&z + k << endl; //&z points to the address of a data structure whose type is char [5]. The value of&z + k is actually equal to
                       // address of z + sizeof (char [5]) * k
cout << * (z + k) << endl;
}
Reply

Use magic Report

1

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-4-15 18:15:01
| Show all posts
How can I get the addresses of all the elements in an array?
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