string parameter issue

Hi everyone...I´m having an annoying issue with strings...
I want to get a substring of a string that I receive as a parameter, but I keep getting a SIGILL error and I can't figure out how to solve it...
here is my function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
grasp::grasp(string instancia){
    d.dead_nodes_list.clear();
    d.qtd_transmissions.clear();

    d.setup(instancia);
    size_t inicio = instancia.find("/");
    string nome = instancia.substr(inicio);
    sprintf(nomeLOG,"LOG_GRASP %s.txt",nome);
    ofstream outfile(nomeLOG);
    outfile << "Arquivo de LOG do GRASP\n";
    outfile << "Seed utilizado: " << d.seed << "\n";
    outfile << "Dados da instancia: \n \tNumero de Nos: " << numnodes << "\n";
    outfile << "\tLocalizacao do SINK: (" << d.coord[numnodes-1][0] << " , " << d.coord[numnodes-1][1] << ")\n";
    outfile.close();

    numClusterHead = ceil(numnodes * PorcentagemClusterHead) + 2;
    for(int i = 0; i < numClusterHead; i++)
        ch_list.push_back(-1);

    for(int i = 0; i < numnodes - 1; i++)
        ch_index.push_back(-1);
}


When I call the nome.substr() function it raises an error. What's wrong with it?
I'm testing it with a string like this:
string instancia = "instancias/100nodes";
I don't see the nomeLOG's variable definition anywhere. I suppose it is of type char*, and if it is, you don't seem to be allocating memory for it, and you don't seem to be checking if the available memory in that pointer (if any at all) is enough for the sprintf() call to succeed without corrupting the stack.

You should just use a std::stringstream or std::string concatenation instread of sprintf().
Why do you think the error is cause by string access? Besides not checking inicio before using it, there's nothing wrong with the string access. Have you considered numnodes and the size of d.coord?
You're passing a std::string as parameter to sprintf, which isn't allowed.
changed the sprintf to string concatenation and it worked. thank you guys.
Topic archived. No new replies allowed.