c++ Segmentation Fault 11 -
this returns segmentation fault 11, can't seem figure out whats causing error.
the code doesnt error out until tries output red blue green file, dont think indexing vector should use remaining memory cant seems put finger on causing 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 =.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; < 1025; i++){ if(i <= 255){ red.push_back(i); green.push_back(25); blue.push_back(25); } if(i >= 256 && <= 511){ red.push_back(25); green.push_back(i-256); blue.push_back(25); } if(i >= 512 && <= 767){ red.push_back(25); green.push_back(25); blue.push_back(i-512); } if(i >= 768 && <= 1023){ red.push_back(100); green.push_back(i-768); blue.push_back(100); } else{ red.push_back(255); green.push_back(255); blue.push_back(255); } } std::cout << "colors profiles created" << std::endl; } void assigncolors(){ std::vector<int> red; std::vector<int> green; std::vector<int> blue; makecolors(red, green, blue); std::ifstream fin(fileoutput); std::ofstream fout(colorfile); std::string line; size_t = 0; while(std::getline(fin, line)){ if(i > 0){ fout << red[i] << ' ' << green[i] << ' ' << blue[i] << '\n'; += 1; } else{ fout << line << '\n'; += 1; } } fin.close(); fout.close(); } int main(){ juliaoutput(); assigncolors(); return 0; }
it looks you're expecting function makecolors modify vectors, pass arguments value. 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 reference instead.
Comments
Post a Comment