• Advertisement
Sign in to follow this  

Question about: char * t = "test"

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Try std::stringstream.

std::stringstream ss;

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

ss.str() returns a std::string

Share this post


Link to post
Share on other sites
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.5
Press any key to continue



Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Quote:
Original post by benryves
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.


It's true that C++ strings are confusing in the "I don't know what this thing does" way. This kind of confusion is annoying, but it only requires you to take a look at a doc and then you should understand how that particular feature works.

C-style strings, on the other hand, are guilty of several "I think this should be doing a thing, but in fact does another". Instead of forcing you to read the manual (which is a good thing), C-string semantics make you write something that doesn't do what you want:

a = b doesn't work the same as with other types.
a == b doesn't work the same as with other types.
a += 'x' doesn't work as you would expect from a string.
b = new char[strlen(a)]; doesn't do what you think.

C-style strings behave like pointers, not objects that are strings.

Share this post


Link to post
Share on other sites
Quote:
Original post by Jonus
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.


You can convert std::strings to and from constant character pointers (std::string::c_str() returns a null-terminated const char*).

Share this post


Link to post
Share on other sites
Quote:
Original post by ToohrVyk
C-style strings, on the other hand, are guilty of several "I think this should be doing a thing, but in fact does another". Instead of forcing you to read the manual (which is a good thing), C-string semantics make you write something that doesn't do what you want:

a = b doesn't work the same as with other types.
a == b doesn't work the same as with other types.
a += 'x' doesn't work as you would expect from a string.
b = new char[strlen(a)]; doesn't do what you think.

C-style strings behave like pointers, not objects that are strings.


This is apsolutely right. The thing is C-style strings ARE pointers there are just methods in the libaries that help you act on them. Therefore the fact that =, ==, += etc. don't work the way "expected" is actually the way they should act because they are pointers and not strings they can just be used like strings because they can used used as an array of characters.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by ToohrVyk
a = b doesn't work the same as with other types.
a == b doesn't work the same as with other types.
a += 'x' doesn't work as you would expect from a string.
b = new char[strlen(a)]; doesn't do what you think.

C-style strings behave like pointers, not objects that are strings.


This is highly dependant upon one's background. Everything you list operates exactly as I expect it to on c-strings. I would never expect += to append anything to a c-string. C isn't Java, and c-strings are C++ classes. When in Rome, do as the Romans. When using C strings, understand that you are using an array of characters and these problems go away.

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
When using C strings, understand that you are using an array of characters and these problems go away.


Yes, I am eagerly looking forward to more mental overhead when writing my programs.

Why are C-style strings called "strings" anyway? I mean, they're arrays of characters but they don't act like strings.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement