简体   繁体   中英

C++ pointers and dynamic memory

its been a while since i've done C++ and i am trying to brush up on it, recently i been using python and java, thus a refresher in pointers was needed.

#include <iostream>
using namespace std;
int main()
{
  char *s=new char;
  cin >>s;
  cout <<s<<endl;
  delete s;
  s=0;
  return 0;
 }

when i try to do this i understand that i have a char of size 1 Bytes and when i type in something it is stored; and i can print it out. but if i try a large input it overflows, i understand that the size is limited.

now i tried this:

#include <iostream>
using namespace std;
int main()
{
  char *s=new char;
  cin >>*s;
  cout <<s<<endl;
  delete s;
  s=0;
  return 0;

}

only the first letter of what i type is stored, i first through *s is same as s[0], but even if so, it should store everything in s[0].

i am not so sure how to understand this.

Also i tried this:

char *s=new char [2];
cin>>s; // i enter lets say "hello"
cout<<s[3]; // this prints out "l"; 

what i don't understand is when i said new char [2], what did i exactly do, did i allocate two chars?,

When you said new char[2] , you allocated an array of characters having length two.

That means you have room for two char s, one at s[0] and one at s[1] .

You may access beyond s[1] , but what you find there is undefined - reading or writing there may crash your program or make it behave in unpredictable ways.

What you have produced here is what is called a buffer overflow - and a malicious attacker could exploit that to make your program behave as they wish.

i first through *s is same as s[0]

It is the same, exactly.

but even if so, it should store everything in s[0].

How can it store "everything"? s[0] is only big enough to store a single character.

recently i been using python and java, thus a refresher in pointers was needed

What you need is not just a refresher on pointers, but std::string , std::vector , and other classes which help you manage memory by knowing their own size and not letting you overflow.

In the first example, you are allocating a single character, but you are passing cin a char* which it will treat as a string. You might have only allocated a single character, but it doesn't know that and will place character after character into the memory following that single allocated character until it causes problems.

In the second example, you allocated the same character, but are passing the single character to cin and it will therefore only read a single character into it.

In the third example, you certainly allocated two characters, but cin doesn't care and neither does the array dereferencing operation. Both assume you've allocated enough for them to work.

Below is how you might use a character pointer to keep track of a string. As you know pointers only hold addresses,they cannot hold all the characters in a character array. This means that when we use a char * to keep track of a string, the character array containing the string must already exist or for better words allocated(staticaly or dynamicaly)

char label[] = "Temperaure";
char label2[10] = "Cold";
char *labelPtr;
labelPtr = label;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM