Thread: mmap assertion failing

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    57

    mmap assertion failing

    Hi, I'm using mmap for the first time in a program where I'm trying to benchmark different data structures. I'm checking the return value, through a assert, and its failing. I dont know why

    Heres the assert
    skipLists: skipLists.c:266: main: Assertion `buffer != ((void *) -1)' failed.
    Heres the code with the line commented
    Code:
    int main(int argc, char *argv[]) {
       	int i, inc;
        	int device_fd;
        	int *buffer;
    	time_t start, end;
    
    	int tmp;
    	list l; 
    	init();
    	l = newList();
    
    	/* 
    	 * Check arguments(file, fileSize, searchFile, searchSize) 
    	 */
    	assert(argc != 4);
    
    	/*
    	 * Load in dataset
    	 */
    	fprintf(stderr, "* Loading file %s\n", argv[1]);
    	device_fd = open(argv[1], O_RDONLY);
    	assert(device_fd != -1);
    	buffer = mmap(0, atoi(argv[2]), PROT_READ, MAP_FILE, device_fd, 0);
    	assert(buffer != MAP_FAILED);
    
    	inc = 2;
    	fprintf(stderr, "* Starting insert\n");
    	time(&start);
    		for(i = 0; i < atoi(argv[2])/4 - 1; ++i) {
    			if(i == inc) {
    				time(&end);
    				fprintf(stderr, "\t* %i in %f seconds\n", i, difftime(end, start));
    				inc <<= 2;
    			}
    
    			insert(l, buffer[i], buffer[i]);
    		}
    	time(&end);
    	fprintf(stderr, "\t* Done in %f seconds\n", difftime(end, start));
    	close(device_fd);
    
    	/*
    	 * Start random searches
    	 */
    	fprintf(stderr, "* Loading file %s\n", argv[3]);
    	device_fd = open(argv[3], O_RDONLY);
    	assert(device_fd != -1);
    	buffer = mmap(0, atoi(argv[4]), PROT_READ, MAP_FILE, device_fd, 0);
    	assert(buffer != MAP_FAILED);	// THIS LINE HERE
    
    	inc = 2;
    	fprintf(stderr, "* Starting search\n");
    	time(&start);
    		for(i = 0; i < atoi(argv[4])/4 - 1; ++i) {
    			if(i == inc) {
    				time(&end);
    				fprintf(stderr, "\t* %i in %f seconds\n", i, difftime(end, start));
    				inc <<= 2;
    			}
    
    			search(l, buffer[i], &tmp);
    		}
    	time(&end);
    	fprintf(stderr, "\t* Done in %f seconds\n", difftime(end, start));
    
    	/*
    	 * Start deleting (with same file)
    	 */
    	inc = 2;
    	fprintf(stderr, "* Starting delete\n");
    	time(&start);
    		for(i = 0; i < atoi(argv[4])/4 - 1; ++i) {
    			if(i == inc) {
    				time(&end);
    				fprintf(stderr, "\t* %i in %f seconds\n", i, difftime(end, start));
    				inc <<= 2;
    			}
    
    			delete(l, buffer[i]);
    		}
    	time(&end);
    	fprintf(stderr, "\t* Done in %f seconds\n", difftime(end, start));
    	close(device_fd);
    
    	freeList(l);
    	return 0;
    }
    
    Thanks for any help
    Last edited by zxcv; 04-25-2010 at 04:30 PM. Reason: figured having the entire program to compile couldnt hurt....

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    It might not hurt to read the man page for mmap. (At least on my system, it says that the MAP option must start with either MAP_SHARED or MAP_PRIVATE.)

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    57
    Doh!

    Looks like that was it. Totally missed that part.

    Thanks alot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BSD mmap for shared memory - am I right?
    By sean in forum Linux Programming
    Replies: 21
    Last Post: 03-09-2009, 01:57 PM
  2. File I/O Assertion Failure in VS2008
    By clegs in forum C Programming
    Replies: 5
    Last Post: 12-25-2008, 04:47 AM
  3. using mmap for copying large files
    By rohan_ak1 in forum C Programming
    Replies: 6
    Last Post: 05-13-2008, 08:12 AM
  4. Assertion error when looking for window handle
    By axr0284 in forum Windows Programming
    Replies: 3
    Last Post: 02-03-2005, 04:11 PM
  5. initializes all components of failing to false
    By romeoz in forum C++ Programming
    Replies: 21
    Last Post: 08-01-2003, 09:30 PM