Punteros de C ++ y memoria dinámica

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.

También probé esto:

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

preguntado el 01 de febrero de 12 a las 03:02

yes i think you allocated two chars -

It's quite disturbing that there are three answers and only one person has pointed out that he's using C++ and no using strings. Or that he's writing C++ like Java.. -

Please consider using std::vector and std::string. -

4 Respuestas

Cuando dijiste new char[2], you allocated an matriz de caracteres having length two.

That means you have room for two chars, one at s[0] y uno en 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 desbordamiento de búfer - and a malicious attacker could exploit that to make your program behave as they wish.

Respondido 01 Feb 12, 08:02

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.

Respondido 01 Feb 12, 08:02

sorry, i mis worded it, it should only hold 1 char, (1 Byte) - Delirio

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.

Respondido 01 Feb 12, 08:02

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;

Respondido 01 Feb 12, 08:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.