Append Pointer To A String

Started by
22 comments, last by Nosaj 22 years ago
How do I append a pointer to a string without using CString
Advertisement
strcat
I get an access violation error when I do this:

char*str="";
str=strcat(str,"asdf");
You didn''t allocate any memory. What did you expect?
AP, don''t answer at all if that''s all your going to explain.

Nosaj, in your example, str is allocated 1 byte (just the Null at the end).

When you try to append "asdf" to it, your actually over-writing memory allocated for other variables.

You could do this for example

  char str[16] = "";str = strcat(str, "asdf");// orchar *str;str = (char *)malloc(16);str[0] = 0;str = strcat(str, "asdf");// Don''t forget to free str sometime later!free(str);[source]they both do the same thing. You don''t have to free it if you allocate it space with char str[16];Hope that helps.Nutts  

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

quote:Original post by BeerNutts
Nosaj, in your example, str is allocated 1 byte (just the Null at the end).

This is still not quite right. There is no space "allocated" - even over-writing with a single byte would be illegal. He''s actually pointed the string at an empty char literal, which does not belong to his program for manipulation.
quote:There is no space "allocated" - even over-writing with a single byte would be illegal. He''s actually pointed the string at an empty char literal, which does not belong to his program for manipulation.


You''re right that he cannot do anything with it, except overwrite the 0, but that screws the "string" (since it won''t be NULL-terminated), which as you "wisely" pointed out, would be illegal.

I was being very literal. If you delcare a string
char *str="1234";

it needs 5 bytes, so it saves off that many bytes off the stack or data space if it''s a global (I''m not getting into byte-alignment on different platforms either). Just like:
char *str="";
needs 1 byte. So it wasn''t "allocated" off the heap, rather "allocated" off the stack. Even though you can''t use it, it''s still there.

Of course, if he''s wise in the way his compiler works, he can do all sorta of things with char *str""; For you to tell him it doesn''t belong to his program is just wrong. He can do whatever he wishes (if, for some stupid reason he wants to get to the previous byte allocated on the stack (and, we all know the stack grows from high-to-low) he could reference it through str, no?)

OK, why did I even bother to reply. sheesh

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

quote:Original post by BeerNutts


You''re right that he cannot do anything with it, except overwrite the 0, but that screws the "string" (since it won''t be NULL-terminated), which as you "wisely" pointed out, would be illegal.


Some (most?) compilers will interpret something like char *hey="hello" as a refernce to a literal. In a compiler like that, doing something like strcpy(hey, "a"; would cause a segmentation fault.

I don''t know about Visual C, but older (and maybe current, too) versions of GCC do this.
quote:Original post by SabreMan

This is still not quite right. There is no space "allocated" - even over-writing with a single byte would be illegal. He's actually pointed the string at an empty char literal, which does not belong to his program for manipulation.


Not exactly... He is pointer is pointing to a single character ('\0' to be exact) that he can do _whatever_ he wants with. Just in his situation that wasn't what he wanted. To say that overwriting a single byte would be illegal is _wrong_. What exactly do you mean by "empty char literal"? If you mean that this is in some way an empty set (as it seems you are saying) then you are wrong, as I said above.

I woudn't even be writing this if I wasn't anoyed that you cut down BeerNutts explenation (which was well done, by the way) with information that was _wrong_. Check out your facts before you tell someone they are wrong.

- mongrelprogrammer

[edited by - mongrelprogrammer on March 21, 2002 4:13:13 PM]
- I hate these user ratings. Please rate me down. (Seriously) -
quote:Original post by BeerNutts
You''re right that he cannot do anything with it, except overwrite the 0, but that screws the "string" (since it won''t be NULL-terminated), which as you "wisely" pointed out, would be illegal.

I never said that he could do that, and indeed he can''t. Or rather, he can, but it invokes undefined behaviour.
quote:
Of course, if he''s wise in the way his compiler works, he can do all sorta of things with char *str"";

If it involves attempting to modify the storage pointed to, then it is illegal.
quote:
For you to tell him it doesn''t belong to his program is just wrong.

No it''s not - it belongs to the program''s const data area. I suppose you could argue that entire area belongs to his program, but not for the uses you are suggesting. Even then, there''s no guarantee the const data area belongs to the program - it might belong to the OS.
quote:
He can do whatever he wishes (if, for some stupid reason he wants to get to the previous byte allocated on the stack (and, we all know the stack grows from high-to-low) he could reference it through str, no?)

No. It won''t be on the stack. And any attempt to modify it, or use it as a launch pad to otherwise access out-of-bounds is a direct violation of the C++ Standard.
quote:
OK, why did I even bother to reply. sheesh

I don''t know. Maybe you''re a sucker for punishment.

This topic is closed to new replies.

Advertisement