Memory access violation. What's wrong with this seemingly simple program?

If you change this, which makes someString a pointer to a read-only string literal.

Up vote 3 down vote favorite share g+ share fb share tw.

This is a quick program I just wrote up to see if I even remembered how to start a c++ program from scratch. It's just reversing a string (in place), and looks generally correct to me. Why doesn't this work?

#include using namespace std; void strReverse(char *original) { char temp; int i; int j; for (i = 0, j = strlen(original) - 1; I.

As sbi's answer shows, you can do this much simpler using the C++ standard library. If, for some reason, you prefer sticking to C functions as your own code does, perhaps you should retag the question as C instead. – jalf Nov 18 '09 at 14:13 Retagged the question, Mark is obviously aiming for a C implementation here.

– mizipzor Nov 18 '09 at 14:14 2 He is using namespaces and including a C++ header though. – jalf Nov 18 '09 at 14:20 Now that you mention it, I should have tagged this as C. I'm aiming to avoid libraries for basic things like this :) Thanks – Mark Nov 18 '09 at 14:35 2 Using libraries when they are available is almost always the smarter thing to do.

Also, if it's such a basic thing, then why did you need help from the SO community? On top of that, strlen() is a library function... – Pieter Nov 18 '09 at 15:12.

If you change this, which makes someString a pointer to a read-only string literal: char *someString = ""; to this, which makes someString a modifiable array of char, initialized from a string literal: char someString = ""; You should have better results. While the type of someString in the original code (char*) allows modification to the chars that it points to, because it was actually pointing at a string literal (which are not permitted to be modified) attempting to do any modification through the pointer resulted in what is technically known as undefined behaviour, which in your case was a memory access violation.

If this isn't homework, the C++ tag demands you do this by using the C++ standard library: std::string s("This is easier. "); std::reverse(s.begin(), s.end()); Oh, and it's int main(), always int main(), dammit!

You're trying to modify a string literal - a string allocated in static storage. That's undefiend behaviour (usually crashes the program). You should allocate memory and copy the string literal there prior to reversing, for example: char *someString = ""; char* stringCopy = new charstrlen( someString ) + 1; strcpy( stringCopy, someString ); strReverse( stringCopy ); delete stringCopy;//deallocate the copy when no longer needed.

Personally, I'd use strdup, but I'm oldschool. – Paul Tomblin Nov 18 '09 at 13:12 strdup() is nice, but it requires using free() and using free(0 is not considered good C++, but rather oldschool C. Otherwise I'd also prefer strdup().

– sharptooth Nov 18 '09 at 13:13 So what's the difference? Is it that i'm giving it its value on declaration? If I had left it as char *someString; and then passed user input to it, I could perform the operation?

– Mark Nov 18 '09 at 13:14 The key is how the memory is allocated. If it is heap- or stack-allocated, it is allright. In origonal code it is allocated statically when the program is started and marked read-only, so writing to it is prohibited.

– sharptooth Nov 18 '09 at 13:16 As @sharptooth said, you've declared that "somestring" is a pointer to the literal string - ie. It's pointing to some storage that is part of the program code, and therefore not modifiable. You have to put it in modifiable memory in order to reverse it.

– Paul Tomblin Nov 18 '09 at 13:17.

The line char *someString = ""; makes someString point to a string literal, which cannot be modified. Instead of using a raw pointer, use a character array: char someString = ".

You can't change string literals (staticly allocated). To do what you want, you need to use something like: int main() { char *str = new chara_value; sprintf(str, "%s", ); strReverse(str); delete str; return 0; } edit strdup also works, also strncpy... i'm sure there's a variety of other methods :).

Just asked this question to the person above and then read your answer :) Thanks. – Mark Nov 18 '09 at 13:15.

See sharptooth for explanation. Try this instead: #include void main() { char someString27; std::strcpy( someString, "" ); strReverse( someString ); } Better yet, forget about char * and use instead. This is C++, not C, after all.

When using more strict compiler settings, this code shouldn't even compile: char* str = "Constant string"; because it should be constant: const char* str = "Now correct"; const char str = "Also correct"; This allows you to catch these errors faster. Or you can just use a character array: char str = "You can write to me, but don't try to write something longer! "; To be perfectly safe, just use std::string.

You're using C++ after all, and raw string manipulation is extremely error-prone.

1 Actually it has to compile. For compatibility, there's a conversion from const char to char*. It's deprecated and you shouldn't sue it, but it's (still) part of the standard.

– sbi Nov 18 '09 at 13:23 No, char* str = "Constant string" must compile. 4. 2/2 says that a string literal that is not a wide string literal can be coverted to an rvalue of tpy "pointer to char".

A conforming compiler cannot reject this initialization. – Charles Bailey Nov 18 '09 at 13:23 Thanks, I made my post more accurate - it's about compiler settings (which you can set to be more strict than the standard is). – Pawe?

Hajdan Nov 18 '09 at 13:25 1 If you say "You're using C++ after all", I don't think that you can really say "shouldn't even compile" when referring to strictly conforming code. You could say that you can ask a compiler to reject this otherwise conforming code, but I saying that it should reject it is too strong (IMHO). – Charles Bailey Nov 18 '09 at 13:40 1 there's no such thing as "more strict than the standard is", since the strictness is determined by how closely it enforces the standard.

– jalf Nov 18 '09 at 14:15.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions