Re: Way around error?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Joseph M. Newcomer (newcomer_at_flounder.com)
Date: 05/04/04


Date: Mon, 03 May 2004 22:13:27 -0400

See below....

On Mon, 03 May 2004 03:33:36 GMT, Balboos <balboos@masonicbrother.com.No.Spam> wrote:

>Hi, all.
>
>I'm making a number of function overloads for a 'capitlizing' function,
>it's purpose to convert an input string to mixed upper and lower case as
>though it were (for example) a name. The (char *) version's giving me
>some grief. The code (below) works if the string is allocated from the
>heap, but not from the stack (access violation).
>
>i.e., if I use:
>char instring[9] = "john doe";
>I get back "John Doe".
*****
That is because this creates an array on the stack, in writeable storage, no problem
****
>
>If I use
>char *instring = "john doe";
****
You have created a reference to constant storage, which is write-protected. So the access
failure is not at all surprising. It in fact would indicate a deep error in the system if
it did NOT give you an access fault.

Answer: don't pass constant strings to something that wants a non-constant value. I
consider it a deep error in the compiler that you don't get a warning that you have
attempted to convert a const value to a non-const pointer.
*****
>I get the access violation at the first attempt to modify the string.
>Similarly, if I call the function with the text as the argument.
>i.e., Capitalize("john doe"), it crashes.
>
>// SOURCE
>char *Capitalize(char *instring) {
>
>char *tmp; // for copy of pointer to input char string.
>bool needUC=true; // set if we 'need' to make next alpha UC; else LC
>
> if(instring!=NULL) {
> strlwr(instring); // Start out all LC - ACCESS VIOLATION!
> for(tmp=instring; *tmp; ++tmp) {
> if(!isalpha(*tmp)) {
> needUC = true; // We've hit a non-alpha - set UC flag
> continue;
> } // if(!isalpha(*tmp))
> if(needUC) {
> *tmp = toupper(*tmp);
> needUC = false; // set flag that UC is satisfied
> } // if(needUC)
> } // while(tmp=instring; *tmp; ++tmp)
> } // if(instring!=NULL)
>
>return instring;
>} // char *Capitalize(char *instring)
>
>Is there a way around this?
>
>Balboos

Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm



Relevant Pages

  • Re: Fortran-compiled DLLs in Python
    ... WindowsError: exception: access violation ... > I do not know how fortran passes this parameter, ... strings may pass a "string descriptor", ...
    (comp.lang.python)
  • AnsiString on Heap.
    ... AnsiString is inside the form. ... If no string is assigned to it it will still give an access violation making ...
    (alt.comp.lang.borland-delphi)
  • Re: Getting rid of access violation message
    ... A better solution is to correct the problem rather than kill the messenger, ... set the Reg_Sz string value ... Windows Server 2003 or later) so that if a program produces an access violation, that the AV will not be shown, i.e. the program just ends without an error dialog being shown? ...
    (microsoft.public.windows.server.general)
  • Re: Inline Assembly
    ... your stosb writes to *StringWorked, you're getting an access violation. ... Presumably you mean to do some kind of transform on the string while copying ... writing this code in C or C++, and it'll be less portable and a lot less ...
    (microsoft.public.vc.language)
  • Re: Large text file import: MVP question
    ... througput time than from the basic Net namespace. ... splitting that the normal string is not mutable. ... change, with mid, split, tolower, or whatever will result in a new string. ... that this is an MVP question. ...
    (microsoft.public.dotnet.languages.vb)