Public Group

# Question about: char * t = "test"

This topic is 4883 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

When i do someonething like this in cpp ...
char * t = "test";

... do i have to delete the t later?
delete t;

I know that you have to delete everything you create with new, but the "test" string is created by the compiler so i don't know what to do with it.

##### Share on other sites
Don't delete it, only delete things you new. In this case, the compiler usually allocates the string in a read only data segment of the executable image, which will cause a problem if you try calling delete on it.

##### Share on other sites
Yes, don't delete it. It's probably a pointer in constant space. In fact don't do this at all. If you would later on do something like
t[0] = 'b'
(because that's better :), it may crash (or not depending on the compiler). You should assign it like this:
const char *t = "test";

or
#include <stdio.h>char *t = strdup("test");

The first will make sure you can't accidently modify the contents. The second approach will make a copy that is freely accessable (but fixed size!).

Tom

##### Share on other sites
Quote:
 Original post by SiCraneDon't delete it, only delete things you new. In this case, the compiler usually allocates the string in a read only data segment of the executable image, which will cause a problem if you try calling delete on it.
Ok, somewhere in my brain the same thing you just told me lurks, but is there a paper or something where i can read things like that up? I think the topic is something like "immediates".

Quote:
 Original post by dimeboltYes, don't delete it. It's probably a pointer in constant space. In fact don't do this at all. If you would later on do something like t[0] = 'b' (because that's better :), it may crash (or not depending on the compiler). You should assign it like this:const char *t = "test";or#include char *t = strdup("test"); The first will make sure you can't accidently modify the contents. The second approach will make a copy that is freely accessable (but fixed size!).Tom
Ok, i looked it up and stuff created with strdup has to be freed with free.

##### Share on other sites
Typically prefer std::string over C-style strings when using C++... You really are wasting much more time and grinding over unnecessary details and complexities. Strings should be easy to work with.

##### Share on other sites
Quote:
 Original post by RDragon1Typically prefer std::string over C-style strings when using C++... You really are wasting much more time and grinding over unnecessary details and complexities. Strings should be easy to work with.
Man i would gladly leave old scool c style strings behind me, but i am working on a font class for opengl and i need something like glPrint(int x, int y, char * str, ...).

When i try to think of a way to do this in c++/strings/streams then i go astray or maybe i just got a good idea.

##### Share on other sites
Try std::stringstream.

std::stringstream ss;

ss << "X location: " << x << " Y location: " << y;

ss.str() returns a std::string

##### Share on other sites
Quote:
 Original post by RDragon1Try std::stringstream.std::stringstream ss;ss << "X location: " << x << " Y location: " << y;ss.str() returns a std::string
Streams are cool but it takes so long to write stuff into them. Here is the idea i got:
// test07string.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include <windows.h>											// Header File For Windows#include <iostream>#include <stdio.h>using namespace std;std::string myprint(const char *str, ...){	char		text[256];										// Holds Our String	va_list		ap;												// Pointer To List Of Arguments	if (str == NULL)											// If There's No Text		return string(" ");													// Do Nothing	va_start(ap, str);											// Parses The String For Variables		vsprintf(text, str, ap);								// And Converts Symbols To Actual Numbers	va_end(ap);													// Results Are Stored In Text	return string(text);}int _tmain(int argc, _TCHAR* argv[]){	cout << myprint("hugo ruft egon %i, %.1f",5,4.5f).c_str() << endl;		return 0;}// ---------------------output:hugo ruft egon 5, 4.5Press any key to continue

##### Share on other sites
Quote:
 Original post by RDragon1Typically prefer std::string over C-style strings when using C++... You really are wasting much more time and grinding over unnecessary details and complexities. Strings should be easy to work with.
Am I the only one who finds C-type strings easier and much less confusing? [rolleyes]
...and don't even get me started on streams.

I should have known that starting out with ASM was a bad plan.

##### Share on other sites
Quote:
 Original post by JonusStreams are cool but it takes so long to write stuff into them.

???

If you're talking about the time the computer takes, it is dominated by the time needed for the actual I/O anyway (all the internal system stuff that translates your data into pixel information for the screen, after looking at the font data, checking where the windows are, etc. etc.).

If you're talking about the time it takes you, haven't you noticed how much extra time it takes to type out all those weird formatting codes - and get them right?

Anyway, with a bit of setup work, you can do this stuff in-line with the streams:

#include <iostream>#include <string>#include <sstream>using namespace std;class text {  stringstream ss;  public:  template <typename T>  explicit text(const T& first) : ss() { ss << first; }  text() : ss() {}  template <typename T>  text& operator() (const T& next) { ss << next; return *this; }  operator std::string () { return ss.str(); } // <-- maybe not a good idea?};void funkyPrint(int x, int y, const std::string& input) {  cout << "Pretending to print '" << input << "' at (" << x << ", " << y << ")" << endl;}int main() {  // Now we get this nice, simple usage and we can just append what we like  // (of whatever data type) without worrying about formatting codes:  funkyPrint(0, 0, text("testing, ")(string("test") + "ing, ")(123));  // we could provide stream manipulators like std::hex, too - anything that  // the underlying stream could make use of.}

Unfortunately, just creating a stringstream in-line, doing insertions and then calling .str() won't work, because the insertion operator returns a reference to the base ostream class (which doesn't provide .str()). I think I saw someone make it work once, but it wasn't at all pretty.

1. 1
2. 2
3. 3
Rutin
16
4. 4
5. 5

• 11
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633723
• Total Posts
3013541
×