Archived

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

Nosaj

Append Pointer To A String

Recommended Posts

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
Guest Anonymous Poster

All right, guys. I just compiled this in MSVC .NET:


// temper.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

int _tmain(int argc, _TCHAR* argv[])
{

char *hey="hello";
strcpy(hey, "a";

return 0;
}


It compiled correctly, and when run it produces an access violation. Will the appropriate parties please print retractions?

Share this post


Link to post
Share on other sites
quote:
Original post by mongrelprogrammer
Not exactly... He is pointer is pointing to a single character ('\0' to be exact) that he can do _whatever_ he wants with.

Not legally. See my post above.
quote:

Just in his situation that wasn't what he wanted. To say that overwriting a single byte would be illegal is _wrong_.

No it's not.
quote:

What exactly do you mean by "empty char literal"?

I meant what I thought any C or C++ programmer would understand by it - that it contains only a terminating zero, as any C-string must.
quote:

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.

Please answer me one question: have you *ever* laid eyes on a copy of the C++ Standard? Honestly, have you? Because, you see, when it comes to *facts* regarding C++, they don't come from thin air, they come from the pages of the C++ Standard.

[edited by - SabreMan on March 21, 2002 4:23:07 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by SabreMan
Really long diatribe that you can read up there somewhere...



First off, yes I have seen the standard. I have read the standard. I know the standard. You are still wrong.

Performing an action that is ''undefined'' is not illegal:

"Undefined behavior may also be expected when the standard omits the description of any explicit definition of behavior. [Note: permis­sible undefined behavior ranges from ignoring the situation completely with unpredictable results, to behaving during translation or program execution in a documented manner characteristic of the environ­ment"

It is just compiler implementation dependent on what the action will be. If you know what your compiler will do then what is the problem? Like I know my compiler treats it as static, so why not use that? It may not be the _best_ programming style (in fact I wouldn''t use it) but it is NOT illegal. In fact, the standard says it is implementation dependent... not that it is illegal(remember, undefined does not mean illegal).

"Whether all string literals are distinct (that is, are stored in nonoverlapping objects) is implementation­ defined.
The effect of attempting to modify a string literal is undefined."

I think you need to be a little more careful in what you call illegal (not to mention accusing people of never even seeing the standard).

- mongrelprogrammer

Share this post


Link to post
Share on other sites
quote:
Original post by mongrelprogrammer
First off, yes I have seen the standard. I have read the standard. I know the standard. You are still wrong.

I am not wrong and never had been during this thread.
quote:

Performing an action that is ''undefined'' is not illegal:

Oh dear! Undefined behaviour falls into the set of illegal C++ behaviours. It is merely a more specific term than illegal, but it still means illegal. It is absolutely not legal to modify a string literal, and I don''t know what makes you think otherwise. Since you are quite clearly not going to believe me on this, I suggest you go and ask about this in c.l.c++.m, where various members of the Standard committee will be happy to help.
quote:

I think you need to be a little more careful in what you call illegal (not to mention accusing people of never even seeing the standard).

I didn''t accuse you of never seeing the Standard, I asked you if you have. There''s a difference. You also seem to have read the relevant section. It''s just a shame that you''ve missed out on the all important part that often comes after reading the required information.

Share this post


Link to post
Share on other sites
quote:
Original post by Oluseyi
Can we keep our ego clashes off the fucking board? I'm getting RSIs from clicking Close Thread so often...

If you're happy to let people go asserting such "facts", then fine. You're the moderator, you're partly responsible for the "quality" of discussion here. End of my input to this thread.

[edited by - SabreMan on March 21, 2002 5:59:14 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by SabreMan
If you''re happy to let people go asserting such "facts", then fine. You''re the moderator, you''re partly responsible for the "quality" of discussion here. End of my input to this thread.

I can''t make people accept my point of view, even when I quote the standard verbatim. At that point, you just leave the dumb to frolick in their shit.

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!

Share this post


Link to post
Share on other sites
i''m not going to take part in the c++-standard-dick-waving contest, but i''d like to mention that the code AP posted above compiled and also ran quite nicely with the borland c++ compiler.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I dont know why you people even bother. Theres no point arguing with saberman, or even talking to him. If he says anything useful at all then he always throws in comments to act superior to everyone else in an attempt to reinforce his (much needed, apparently) perception of intellectual integrity.

My favorite thing about sabermens posts are often the *vague* descriptions and solutions he provides. When people disagree with him, or supercede him, because he was vague, then he whips out the c++ standard, or starts with the personal attacks, or explains how he was correct in the first place (i especially enjoy those threads). Unlike beerhunter, you wont ever, in any thread, find him saying "oh, ok". instead he says "no... !!!".

Honestly, if he really was as smart as we''d all like everyone to be, then his first comments would have been, "thats illegal in accordace with the c++ standard", instead of letting people argue about valid program functonality. though, his last comments did make the savy switch from implied program functionality to clearly stated c++ standard compliance.

gee, you would think that the smart people could end arguments quickly. But then, why would they argue about highly documented things in the first place?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
A POINTER CANNOT BE APPENDED TO A STRING the way you think it can.

a pointer is a datatype, like int or char, and all pointers are a standard, system-dependent length. pointers point somewhere.
when you say "copy pointer to string", i beleive what you mean is copy the data pointed to by pointer A, to a string.

when you declare a pointer,
char* str;

then you have an uninitialized datatype. the pointer ''points'' to a random location, like "int i", your pointer contains garbage.

when you say:

str = "asdad"; // same as str = &"asdad"

you set the pointer, now it points somewhere special, a literal string . literal strings for the most part are reserved, or read only, so dont try to write to them.


char* str = "";
strcat(str, "Asdasd";

this is an invalid operaton because str points to either a random spot, or to a reserved area. you cannot write to a reserved area, and, you cannot write to a random area, without getting undefined results.

in order to use functions that write to memory pointed to by pointers like str, then BOTH pointers must point to valid memory. otherwise, your making a logic error, and get undefined and often terminal results.

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
I dont know why you people even bother. Theres no point arguing with saberman, or even talking to him. If he says anything useful at all then he always throws in comments to act superior to everyone else in an attempt to reinforce his (much needed, apparently) perception of intellectual integrity.

Thankyou for contributing to the general hounding of me which has occurred this week. You have helped me to realise that my involvement in these forums is entirely free from value from my standpoint. There seems to be little interest in technical accuracy or correctness.
quote:

My favorite thing about sabermens posts are often the *vague* descriptions and solutions he provides. When people disagree with him, or supercede him, because he was vague, then he whips out the c++ standard, or starts with the personal attacks, or explains how he was correct in the first place (i especially enjoy those threads). Unlike beerhunter, you wont ever, in any thread, find him saying "oh, ok". instead he says "no... !!!".

Honestly, if he really was as smart as we'd all like everyone to be, then his first comments would have been, "thats illegal in accordace with the c++ standard", instead of letting people argue about valid program functonality. though, his last comments did make the savy switch from implied program functionality to clearly stated c++ standard compliance.


I suggest you take a look up at the first comment I posted to this thread, which says:

"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."

That is a statement free from emotion, free from personal insult, and has basis only in fact. I'm not sure why you feel it was necessary to have mentioned "according to the C++ Standard" along with stating the behaviour is "illegal". I felt what I said stood-up on its own, I didn't see the need for all the extra baggage until I was contradicted. Also, read the ensuing post from mongrelprogrammer, and see if you can guess where the thread really turned into an argument.

Your complaint would carry more weight if I had argued for something which is demonstrably wrong.
quote:

gee, you would think that the smart people could end arguments quickly. But then, why would they argue about highly documented things in the first place?

Why would I need to quote chapter and verse of these highly documented things in the first place? For your information, I'm not interested in these arguments, I don't like arguing. However, I do value technical accuracy, and I am sometimes prepared to argue something I believe to be fact. Still, there is clearly no interest in facts or accuracy here, and I am clearly getting nothing from these arguments.

Finally, I'd like to recommend an article from Eric Raymond's FAQ collection: Smart Questions. There is an underlying philosophy that I like. You may or may not like it or agree with it, but it should be of interest nonetheless.

--
"Many people would sooner die than think. In fact they do."

[edited by - SabreMan on March 22, 2002 6:12:00 AM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
oh please.
yes, i was clearly talking about this singal thread, saberman, good deduction, good job, pat on the back to you -you solved the case, again.

blah haha

ive already been to that link, but thanks for providing it again.
i cant beleive you use it to justify your behavior, lol.

Share this post


Link to post
Share on other sites
Enough!

If only people could seperate personality from principle and not feel the need to respond to their every criticism, these boards would be even more useful. If you''ve made your point and - as far as you''re concerned - the opposition is merely being pedantic, ignore them.

SabreMan, we''ve had this discussion in limited form and I generally agree that Standards-compliance and fundamental understanding are critical but in short supply, but I feel you should leave the ignorant to make their own mistakes where appropriate. I also feel you should refrain from bringing up credentials or any information external to the issue, such as who knows who or what. Principle, not personality. Thanks.

As for the rest of you, don''t always respond with indignation. It''s lame. Consider what SabreMan says, even if he''s "being a prick." Some of us have little use for frivolous niceties.

Thread closed.

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.