Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Nosaj

Append Pointer To A String

This topic is 5965 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

Advertisement
Guest Anonymous Poster
strcat

Share this post


Link to post
Share on other sites
I get an access violation error when I do this:

char*str="";
str=strcat(str,"asdf");

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You didn''t allocate any memory. What did you expect?

Share this post


Link to post
Share on other sites
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");

// or

char *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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites

This topic is 5965 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.

Guest
This topic is now closed to further replies.

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!