Rave
Rave

Reputation: 843

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?,

Upvotes: 1

Views: 176

Answers (4)

Frantz Romain
Frantz Romain

Reputation: 846

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;

Upvotes: 0

Ben Voigt
Ben Voigt

Reputation: 283624

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.

Upvotes: 2

mmodahl
mmodahl

Reputation: 176

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.

Upvotes: 1

Borealid
Borealid

Reputation: 98459

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

That means you have room for two chars, 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.

Upvotes: 2

Related Questions