Question about: char * t = "test"

Started by
13 comments, last by ToohrVyk 18 years, 8 months ago
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.
Advertisement
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.
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
Quote:Original post by SiCrane
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.
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 dimebolt
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
Ok, i looked it up and stuff created with strdup has to be freed with free.
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.
Quote:Original post by RDragon1
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.
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.
Try std::stringstream.

std::stringstream ss;

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

ss.str() returns a std::string
Quote:Original post by RDragon1
Try 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
Quote:Original post by RDragon1
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.
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.

[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]

Quote:Original post by Jonus
Streams 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.

This topic is closed to new replies.

Advertisement