Bytehunter February 2016

c++ Segmentation Fault 11

This returns a Segmentation Fault 11, I can't seem to figure out whats causing the error.

The code doesnt error out until it tries to output the Red Blue Green to the file, I dont think indexing into a vector should use up all the remaining memory but I cant seems to put my finger on what is causing the error.

#include <iostream>
#include <fstream>
#include <vector>
#include <string>

const int PIX_SIZE = 40;
const int ESCAPE = 1024;
const char FILEOUTPUT[64] = "juliaOutput";
const char COLORFILE[64] = "juliaColor";




void juliaOutput(){
    std::ofstream fout(FILEOUTPUT);
    fout << PIX_SIZE << ' ' << PIX_SIZE << ' ' << ESCAPE << std::endl;

    double dx = 4./(PIX_SIZE-1);
    double dy = 4./(PIX_SIZE-1);

    double a =.5;
    double b = -.5;

    double row;
    double col;

    int count;

    for(row=2; row>=-2; row-=dy){
        for(col=-2; col<=2; col+=dx){
            count = 0;
            double new_x = col;
            double new_y = row;
            while(((new_x*new_x) + (new_y*new_y) <= 4) && count <= ESCAPE-1){
                double old_x = new_x;
                double old_y = new_y;
                new_x = (old_x*old_x) - (old_y*old_y) + a;
                new_y = (2*old_x*old_y) + b;
                count++;
            }
            fout << count << ' ';   
        }
        fout << std::endl;
    }
    fout.close();
}

void makeColors(std::vector<int> red, std::vector<int>  blue, std::vector<int>  green){
    int i;
    for(i =0; i < 1025; i++){
        if(i <= 255){
            red.push_back(i);
            green.push_back(25);
            blue.push_back(25);
        }
        if(i >= 256 && i <= 511){
            red.push_back(25);
            green.push_back(i-256);
            blue.push_back(25);
        }
        if(i >= 512 && i <= 767){
            red.push_bac        

Answers


Sam Cristall February 2016

It looks like you're expecting the function makeColors to modify the vectors, but you pass all the arguments by value. You can change:

void makeColors(std::vector<int> red, std::vector<int> blue, std::vector<int> green)

to

void makeColors(std::vector<int>& red, std::vector<int>& blue, std::vector<int>& green)

to pass by reference instead.

Post Status

Asked in February 2016
Viewed 2,885 times
Voted 8
Answered 1 times

Search




Leave an answer