Thread: Access Violation when writing to String

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    10

    Access Violation when writing to String

    Why do I get an access violation when I try to change the value of individual characters in a char array via pointers?

    If I pass a pointer-to-pointer-to-char (char **string) and try to change any of it's elements I get an access violation. This has happened with a few projects working with strings and I was never able to figure out why so I just ignored it, but now it's bugging me

    So I'm attempting a recursive function to reverse a string in place and this is what I've got: I'll bold the line where I am getting an access violation.

    Code:
    #include <stdio.h>
    #include "recursive.h"
    
    char *buf;
    int buf_p;
    
    void reverse_string(void);
    
    char *reverse(char **string){
    	buf = *string;
    	buf_p = 0;
    	reverse_string();
    	buf[buf_p] = '\0';
    	return *string;
    }
    
    void reverse_string(){
    	char c;
    	if((c = buf[buf_p++]) != '\0')
    		reverse_string();
    	else{
    		buf_p = 0;
    		return;
    	}
    	buf[buf_p++] = c;
    }

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    You didn't show the actual call to reverse(), so I can only guess. My guess, though, is that it looks something like this:
    Code:
    char *s = "hello";
    reverse(&s);
    If so, the problem is that s is pointing to a string literal, and it's undefined behavior if you modify a string literal. On lots of systems, strings literals are placed into read-only memory, causing a fault when you try to write to them. Instead of using a string literal, allocate space with malloc() and strcpy() a string into it. See if that fixes the problem. If not, post an entire program that exhibits the problematic behavior.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    10
    Oh, sorry

    Code:
            char *cp = (char *)malloc(STRLEN);
    
    	cp = "hello world";
    	reverse(&cp);
    Any ideas?

    edit:

    Missed the part about using strcpy...is there a good explanation why it doesn't work when i assign cp a literal like that?
    Last edited by emef; 11-08-2009 at 10:14 PM.

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    I gave the explanation. You can't modify a literal. What you're doing when you assign "hello world" to cp is overwriting the memory you just allocated.

    After you assign the result of malloc() to cp, cp holds an address. At that address are however many bytes you asked malloc() to allocate. You're free to do what you want to those bytes, including putting a string there, which strcpy() will do.

    Once you assign "hello world" to cp, you've changed the address that cp contains. At that new address are 12 bytes (namely, your "hello world"). You are not allowed to modify those bytes.

    After you've assigned the result of malloc() to a pointer, you should not assign a new value to the pointer with = (unless, of course, you've freed the previous memory). It's similar to this:
    Code:
    int a = 10;
    a = 7;
    /* now a has nothing to do with 10; you've replaced the previous value. */
    When you assign "hello world" to a pointer that you've already set to a value, that previous value disappears. In order to actually use the malloc()'d space, you must somehow modify the bytes that the pointer points to, and strcpy() is one way of doing that.

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    10
    ahhhhh thanks man, got it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unable to compare string with 'getter' returned string.
    By Swerve in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2009, 05:56 PM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. Access violation... can't figure it out...
    By Raigne in forum C++ Programming
    Replies: 7
    Last Post: 10-11-2007, 10:52 AM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM

Tags for this Thread