-2

In my application I have external text file which contain the configurations for my program. I'm reading that external file line by line and adding values to arrays. In some point I have to nested loop based on arrays to process some information. below is my code

#include <iostream>
#include <fstream>
#include <algorithm>

#include <stdlib.h>
#include <cstring>
#include <sys/stat.h>
#include <unistd.h>


using namespace std;

string ip_array[0];
string link_array[0];
string conn_array[0];

int readconf(){

    int ip_count = 0;
    int link_count = 0;
    int conn_count = 0;

    ifstream cFile ("net.conf");
    if (cFile.is_open())
    {
        string line;
        while(getline(cFile, line)){
            line.erase(remove_if(line.begin(), line.end(), ::isspace),
                                 line.end());
            if(line[0] == '#' || line.empty())
                continue;
            auto delimiterPos = line.find("=");
            auto name = line.substr(0, delimiterPos);
            auto value = line.substr(delimiterPos + 1);

                if ( name == "IP") {

                    //cout << value << endl;
                    ip_array[ip_count] = value;
                    ++ip_count;

                }
                else if ( name == "LINK") {

                    //cout << value << endl;
                    link_array[link_count] = value;
                    ++link_count;

                }
        }

    }
    else {
        cerr << "file read error.\n";
    }


}




int main()
{
    readconf();

        for( unsigned int a = 0; ip_array[a].length(); a = a + 1  ){

            cout << ip_array[a] << endl;

                for( unsigned int a = 0; link_array[a].length(); a = a + 1  ){

                    cout << link_array[a] << endl;

                }
            } 

}

But when I run this I always get seg fault. But if I comment out the one loop it working perfectly fine. When I COUT the values on readconf function I'm getting the correct values.

rafalefighter
  • 714
  • 2
  • 11
  • 39
  • 1
    *Where* does the crash happens? Have you tried to catch it with a debugger, and locating where in your code it happens? Also when catching crashes with a debugger, the debugger will let you examine values of variables to help you figure out what might have caused the crash. Please learn how to do that, or at the very least edit your question to show us where it happens in *your* code (and the values of all involved variables). – Some programmer dude Nov 19 '18 at 07:59
  • 4
    You're using arrays of length zero. That means they can hold zero items. – druckermanly Nov 19 '18 at 07:59
  • 1
    A big hint is those arrays though. Arrays and their sizes are fixed at compilation, they can not be extended. You need to start learning about [`std::vector`](https://en.cppreference.com/w/cpp/container/vector). – Some programmer dude Nov 19 '18 at 08:00
  • 1
    Also, don't use global variables, it's a bad habit. Perhaps you should learn about classes as well? Perhaps [get a couple of good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) and learn C++ properly? – Some programmer dude Nov 19 '18 at 08:02

2 Answers2

1

It looks like you're reusing the 'a' variable, which isn't a good idea because it's very easy to make an error this way.

However, your actual issue appears to be that you're calling some_array[a].length() as the for loop condition. If a is out of bounds, this could result in a segmentation fault. Instead, use a < array_len as your condition, where array_len is the length of the array.

Unsolved Cypher
  • 1,025
  • 10
  • 24
1
  1. learn how to debug! build and run your program in debug mode and examine exactly what went wrong. this is a critical skill for all software developers.
  2. make sure you understand the for loop syntax
uceumern
  • 877
  • 10
  • 14