Thread: Invalid Memory Access when passing string literals to an argument

  1. #1
    Registered User
    Join Date
    Oct 2020
    Posts
    7

    Invalid Memory Access when passing string literals to an argument

    I'm trying to call this search and replace function, and it says invalid memory access.
    How should I modify the function, so that it would be possible to pass literal strings as arguments.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    
    
    void function(const char * string){
    
    
        char *pointer = strstr(string, "out");
        if (pointer != NULL) {
            memcpy(pointer, "in", 2);
            memmove(pointer + 2, pointer + 3, strlen(pointer + 3) + 1);
        }
        printf("%s\n", string);
    
    
        
        
    }
    
    
    int main(void) {
        function("cat is out roaming");
    
    
        return 0;
    }

    Code:
    75cc8e8a : at ???: RUNTIME ERROR: invalid memory access
    ../lib/bcheck.c:1699: by __bound_memcpy
    replace_text.c:9: by function
    replace_text.c:24: by main
    Expected output:
    Code:
    cat is in roaming
    Last edited by boqsc; 07-26-2021 at 12:56 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You can't modify string literals because they're placed in read-only memory.

    You either have to
    - pass another parameter pointing to some memory where you want the modified string placed.
    - allocate some memory inside function and return that pointer back to the caller.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2021
    Posts
    139
    Building on what @Salem has said, try changing your `main` function like this:

    char buffer[] = "cat is out roaming";

    function(buffer);

    This uses the string literal to initialize a local variable, which *is* modifiable. So you should be able to do `memcpy`, `memmove`, etc. on it inside your `function`.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing a string as argument to fgets
    By koplersky in forum C Programming
    Replies: 3
    Last Post: 10-01-2012, 10:45 PM
  2. fscanf question - invalid memory access error
    By kerrymaid in forum C Programming
    Replies: 2
    Last Post: 08-31-2010, 01:17 PM
  3. Invalid access to memory
    By vin_pll in forum C++ Programming
    Replies: 1
    Last Post: 01-11-2010, 04:36 AM
  4. error: passing const std::string as this argument
    By noobcpp in forum C++ Programming
    Replies: 2
    Last Post: 07-23-2008, 01:11 PM
  5. ReadFile giving "invalid access to memory location"
    By neandrake in forum Windows Programming
    Replies: 2
    Last Post: 09-28-2004, 07:46 PM

Tags for this Thread