is it possible to new a const char * ?

Can i do something like this ?

1
2
3
4
5
6
7
8
9

 cout << "Enter a  command: ";
getline(cin, command);
		
const char * com = new const char * (command.c_str());
size_t bytesSent = send(hSocket, com, strlen(com) + 1, 0);  


Last edited on
I think you are doing it more complicated than it needs to be.

Something like this should work:
 
size_t bytesSent = send(hSocket, command.c_str(), command.size() + 1, 0);

Last edited on
For that send() you should not need to:
1
2
3
std::string com;
getline( cin, com );
size_t bytesSent = send( hSocket, com.c_str(), com.size() + 1, 0 );


If you really need dynamic array, then you can always create one and pretend that it is const:
1
2
3
4
5
6
7
8
9
10
11
12
const char * foo( const std::string& bar ) {
  char * tmp = new char [bar.length()+1];
  std::strcpy( tmp, bar.c_str() );
  return tmp;
}


std::string command;
getline( cin, command );
const char * com = foo( command );
size_t bytesSent = send( hSocket, com, strlen(com) + 1, 0 );
delete [] com;

Last edited on
I am very curious as to why I cannot use the new on const char * at the line:

const char * com = new const char * (command.c_str());
new always returns a pointer to the thing that was created.
Since you are creating a pointer you need to use a pointer to a pointer to store the result.

 
const char** com = new const char* (command.c_str());
Last edited on
Overall, or this specific case?

In this case,
std::string::c_str
The pointer returned may be invalidated by further calls to other member functions that modify the object.


const char** com = new const char* {command.c_str()};
is practically same as
1
2
const char* tmp = command.c_str();
const char** com = new const char* {tmp};

The c_str() returns a pointer to const. The tmp is a copy of that.
The dynamically allocated pointer is a copy of that
The automatic variable (pointer com) points to the dynamically allocated pointer.

However, if you call members of command, the *com might not be valid any more.

Perhaps:
const char* gaz = new const char { command.c_str()[0] };
No problem there. the gaz points to a char that was (1) dynamically allocated and (2) was initialized with the first character of 'command'.


In other words: yes, you can allocate const memory. Like any const objects you have to initialize it, for you cannot assign value to it later.

You can even allocate an array of const:
1
2
3
4
5
6
#include <iostream>
int main ()
{
  const char* gaz = new const char [6] {'h', 'e', 'l', 'l', 'o', 0 };
  std::cout << gaz;
}

Alas, initializing that array with a pointer is not possible.
Furthermore, the "const new" is nominal:
1
2
3
4
5
6
7
8
9
#include <iostream>

int main ()
{
  const char* gaz = new const char [6] {'h', 'e', 'l', 'l', 'o', 0 };
  char* bar = const_cast<char*>(gaz);
  bar[0] = 'D';
  std::cout << gaz;
}







Dello
for the same reason you can't set const int x to 10 after it was initialized to 20.
its a constant pointer, and new attempts to change that (note the syntax of x = new.. x is being modified, but its a constant, no can do. ) Apart from voodoo code, which I don't recommend (you can sometimes fool the compiler to strip away const but its a BAD IDEA) you can accept it as truth that you cannot use an = (assignment) on a constant more than once, that once being when it was created (if you used = syntax for that).





Last edited on
Furthermore, the "const new" is nominal
I don't think so: do you have a source?

https://en.cppreference.com/w/cpp/language/new
Doesn't mention const or cv-qualifications;
http://eel.is/c++draft/expr.new#1
Leads me to think that new is quite capable of creating cv-qualified objects (that are actually cv-qualified, not the result of a qualification conversion.)
Last edited on
Topic archived. No new replies allowed.