5

I am learning about dynamic memory in C++. What I learned as a standard way of allocating & deallocating dynamically for any data type is, for example,

//For double,
double* pvalue1 = nullptr;
pvalue1 = new double;
*pvalue1 = 17.3;
delete pvalue1; //free up when I'm done

BUT, for a char array, I learned that it's handled differently:

char* pvalue2 = nullptr;
pvalue2 = new char[6];
strncpy(pvalue2,"Hello",sizeof("Hello"));

std::cout << "Pointed-to value of pvalue2 is " << *pvalue2 << std::endl;
std::cout << "Value of pvalue2 is " << pvalue2 << std::endl;

delete [] pvalue2; //free up when I'm done

Then, on command prompt:

Pointed-to value of pvalue2 is H
Value of pvalue2 is Hello
  1. Why does the pointer pvalue2 give the "pointed-to" string literal instead of the memory address? Isn't a "pointer value" always the memory address which it points to?
  2. Why does dereferencing give only the first character in the array?
  3. How can I get the memory address then, in this case?
1
  • It's better to completely avoid raw pointers and use smart pointers or containers. Commented Jun 17, 2015 at 19:51

3 Answers 3

9

The operator<< of output streams has an overload to char* that interprets the character array as a string instead of a generic pointer. Thus, it prints the value of the string. Use static_cast<void*>(pvalue2) to print the address.

Dereferencing the string gives you a char, which is not a string.

If you are using C++, prefer using std::string instead of character arrays. If you need a C-style string for something, you can get a character array representation of the string by calling the c_str() method.

Sign up to request clarification or add additional context in comments.

Comments

6

Why does the pointer pvalue2 give the "pointed-to" string literal instead of the memory address?

Because there's a special overload of << so that streaming char* will give the string it points to, not the pointer value. That's usually what you want to happen.

Isn't a "pointer value" always the memory address which it points to?

Yes.

Why does dereferencing give only the first character in the array?

Because a pointer contains the address of a single object, which in this case is the first element of the array. Other elements can be accessed using pointer arithmetic, e.g. pvalue2[2] for the third element.

How can I get the memory address then, in this case?

To print it with <<, convert to a different pointer type to avoid the char* overload:

std::cout << static_cast<void*>(pvalue2);

Comments

0
  1. I'd have quoted "gives" in your question rather than "pointed-to" because the former is fuzzy in your mind. What you do with objects is up to you. pvalue2 is just a number.

  2. Same "gives". Well dereferencing a pointer to a char returns the char itself. For C strings it returns the first character.

  3. Of course, that's most of all what pointers are. The rest is the item size, intrinsic to the type itself. (Incrementing pointers increments by the item size, not 1).

  4. In what case? If you hold a pointer you already have the memory address. If you hold the first letter (case 3 I guess), take its address.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.