Thread: next_permutation seg fault

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

    next_permutation seg fault

    Hello. Im writing a program that will test *a lot* of permutations, only while using the STL's next_permutation function I seem to seg fault.

    Heres the code, where I left the while that does the permutations and forced one if to never run. This seems to be the only way to get it to not seg fault. I'm fairly sure its not because of my other code, but ill attach it incase some one feels nice enough to look threw it quick

    Thanks

    Code:
    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    
    #include <fcntl.h>
    
    extern "C" {
    	#include "../../lib/present_conds/present_conds.h"
    }
    
    int createBoxes(int (*func)(unsigned char *), int buff_size, int device_fd) {
    	int retval;
    	int index = 0;
    	unsigned char sbox[16];
    	unsigned char buffer[buff_size][16];
    	
    	// Create initial sbox
    	for(int i = 0; i < 16; ++i)
    		sbox[i] = i;
    	
    	// Test all permutations
    	do {
    		if(false) {
    		//if(func(sbox) == 0) {
    			memcpy(buffer[index], sbox, sizeof(unsigned char[16]));
    			++index;
    
    			if(index + 1 == buff_size) {
    				retval = write(device_fd, buffer, 
    					       sizeof(char[buff_size][16]));
    				if(retval == -1) {
    					perror("Error while writing buffer");
    					return 1;
    				}
    
    				index = 0;
    			}
    		}
    	} while(true);
    //	} while(next_permutation(sbox, sbox + 16) == true);
    
    	return 0;
    
    	// Write whats left of buffer
    	retval = write(device_fd, buffer, sizeof(char[index][16]));
    	if(retval == -1) {
    		perror("Error while writing final buffer");
    		return 1;
    	}
    
    	return 0;
    }
    
    int main() {
    	int device_fd;
    	mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
    
    	device_fd = open("output", O_WRONLY | O_CREAT | O_TRUNC, mode);
    	if(device_fd == -1) {
    		perror("Error opening output");
    		return 1;	
    	}
    	createBoxes(testAllConds, 1 << 21, device_fd); 
    
    	return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    There are 16! = 20922789888000 permutations of 16 objects. You have room to store 2^21 = 2097152 of them.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    57
    Im not storing them, only a small percentage of them.

    Plus I'm really just worried about the seg fault now, space I can worry about after.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I didn't see func anywhere, so I have no idea how many you're actually storing. But since seg fault means "you are writing to memory you don't have", I would start worrying about space now, if I were you.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    57
    func is a function pointer.

    Im not actually storing anything yet, that branch never runs....

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I thought you said that it only segfaulted if you did run the branch. Or at least that's what I took this sentence to mean:
    I left the while that does the permutations and forced one if to never run. This seems to be the only way to get it to not seg fault.
    So if that's not true, when does it seg fault and when does it not?

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    57
    After being utterly disgusted by it for awhile I tried converting all my c libraries into c++ ones. That did the trick it seems.

    Very very weird...

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You can't have VLAs in C++.

    int buffer[buff_size][16];

    is invalid.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Not to mention, that it's also 32MB in size, given the current code.
    That's a lot, when your default stack size is what - 1MB?

    Sure you just didn't blow the stack with the segfault?
    Does it work with smaller sizes?
    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.

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by zxcv View Post
    Im not storing them, only a small percentage of them.

    Plus I'm really just worried about the seg fault now, space I can worry about after.
    You're not worried about the fact running through all permutations will take several days at the very least?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting a seg fault
    By ammochck21 in forum C Programming
    Replies: 11
    Last Post: 01-23-2009, 05:27 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  4. weird seg fault
    By Vermelho in forum C Programming
    Replies: 3
    Last Post: 05-10-2008, 08:27 PM
  5. Seg Fault Problem
    By ChazWest in forum C++ Programming
    Replies: 2
    Last Post: 04-18-2002, 03:24 PM