mmap()-related question - Linux

This is a discussion on mmap()-related question - Linux ; Would somebody be so good as to tell me why operating with the pointer returned from map_open() isn't valid: void map_open( void* map ) { int fd; struct stat fst; fd = open( "test.map", O_RDWR | O_CREAT, 0644 ); fstat( ...

+ Reply to Thread
Results 1 to 3 of 3

Thread: mmap()-related question

  1. mmap()-related question

    Would somebody be so good as to tell me why operating with the pointer
    returned from map_open() isn't valid:

    void
    map_open( void* map )
    {
    int fd;
    struct stat fst;

    fd = open( "test.map", O_RDWR | O_CREAT, 0644 );
    fstat( fd, &fst );

    map = mmap( 0, fst.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd,
    0 );
    close( fd );
    }

    int
    main( void )
    {
    void* my_map;

    map_open( my_map );
    strcpy( my_map, "Hello, world!" );
    munmap( my_map, MAP_SIZE );

    return( 0 );
    }

    The symptoms: I experience a segmentation fault when calling strcpy()
    with my_map. If I mmap() in main(), everything works correctly.
    Please clarify this for me, I'm ignorant!

    Thanks in advance, and my regards.


  2. Re: mmap()-related question

    quigstah@gmail.com wrote:
    > Would somebody be so good as to tell me why operating with the pointer
    > returned from map_open() isn't valid:
    >
    > void
    > map_open( void* map )
    > {
    > int fd;
    > struct stat fst;
    >
    > fd = open( "test.map", O_RDWR | O_CREAT, 0644 );
    > fstat( fd, &fst );
    >
    > map = mmap( 0, fst.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd,
    > 0 );
    > close( fd );
    > }
    >
    > int
    > main( void )
    > {
    > void* my_map;
    >
    > map_open( my_map );
    > strcpy( my_map, "Hello, world!" );
    > munmap( my_map, MAP_SIZE );
    >
    > return( 0 );
    > }
    >
    > The symptoms: I experience a segmentation fault when calling strcpy()
    > with my_map. If I mmap() in main(), everything works correctly.


    What you pass into map_open is the problem. You pass it a pointer to a void.
    That pointer is local to the function, so when you set 'map = mmap(...)' what
    you are really doing is only altering the value of 'map' in your 'map_open'
    function.

    In other words, you're passing a type (in your case, a 'void *') by value,
    and any modifications you make to such a type in the function modify the
    passed copy -- not the original.

    There are two ways to solve your problem: either use a pointer to a pointer
    to void (void **) as the parameter to the function, and dereference it, or
    have your map_open function return the pointer and assign it directly.

    -n

  3. Re: mmap()-related question

    "quigstah@gmail.com" writes:

    > Would somebody be so good as to tell me why operating with the pointer
    > returned from map_open() isn't valid:


    Read a bit about the C language, and how function arguments are passed
    by value, rather than by reference as some languages do. In short,
    modifying an argument in a function will not affect the value of any
    variables in the calling function.

    --
    Måns Rullgård
    mru@inprovide.com

+ Reply to Thread