stumping in memory mapped

This is a discussion on stumping in memory mapped within the C Programming forums, part of the General Programming Boards category; I think some of this is GNU specific. Anyway, the following program creates a new one page (4k) file with ...

  1. #1
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    stumping in memory mapped

    I think some of this is GNU specific. Anyway, the following program creates a new one page (4k) file with nothing in it, maps it to memory, and then my intent was, of course, to write different things to different parts of the map/file and see if I could use them.

    Which is as far as I've gotten with memory mapping, because even though the file is really 4096 bytes and everything, attempting to write past byte 1024 causes a segfault. WHY OH WHY?

    Code:
    #include <stdlib.h>
    #include <unistd.h>
    #include <stdio.h>
    #include <fcntl.h>
    #include <sys/mman.h>
    #include <errno.h>
    
    int main () {
            int *map, i;
            char buffer[4096];
            const char file[]="/tmp/maptest";
            int FD=open(file,O_RDWR|O_CREAT);
            write(FD,buffer,4096);
            map=mmap(NULL,4096,PROT_READ|PROT_WRITE,MAP_SHARED,FD,0);
            close(FD);      // if "i" goes to 1024, segfault 
            for (i=800;i<1024;i++) map[i]=54;
            printf("&#37;c",map[1800]);  // is a 0 (null terminator)
    }
    Last edited by MK27; 09-12-2008 at 08:01 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    because int's are four bytes wide on your system. 4096 = 1024*4, so you can think of your 4096 bytes as char[4096] (like buffer) or int[1024] (like map).

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    golly gosh, thanks tabstop
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutex and Shared Memory Segment Questions.
    By MadDog in forum Linux Programming
    Replies: 14
    Last Post: 06-20-2010, 04:04 AM
  2. Suggestions on this C style code
    By Joelito in forum C Programming
    Replies: 11
    Last Post: 06-07-2007, 03:22 AM
  3. Relate memory allocation in struct->variable
    By Niara in forum C Programming
    Replies: 4
    Last Post: 03-23-2007, 03:06 PM
  4. Memory allocation and deallocation
    By Micko in forum C++ Programming
    Replies: 3
    Last Post: 08-19-2005, 06:45 PM
  5. CreateProcess and Memory mapped files
    By kevcri in forum C++ Programming
    Replies: 14
    Last Post: 12-10-2003, 02:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21