zajcev
zajcev

Reputation:

Segmentation fault on string assignment in C++

Take a look at this example function:

RuntimeConfiguration* conf_rt_conf() {

    RuntimeConfiguration *conf;
    conf = new RuntimeConfiguration();
    conf->arch_path="./archive";
    conf->err_log="./err_log";
    conf->fail_log="./fail_log";
    conf->msg_log="./msg_log";
    conf->save="html, htm, php";
    conf->ignore="jpg, gif";
    conf->cookies="";

    return conf;
}

Everything here works fine, but when I run something like this:

DatabaseInput** conf_db_input() {

    DatabaseInput **db_input;
    db_input=(DatabaseInput **)malloc(NUMB_SITES*sizeof(DatabaseInput *));
    for (int i=0;i<NUMB_SITES;i++) db_input[0]= new DatabaseInput();

    db_input[0]->full_name="ABCNews";
    db_input[0]->alias="abcn";
    db_input[0]->prefix="/eng";

    db_input[1]->full_name="Rzeczpospolita";
    db_input[1]->alias="rp";
    db_input[1]->prefix="/pol";

    return db_input;
}

I get segmentation fault on first assignment. It probably has something to do with the fixed memory block allocated for this struct. How do I get it to work properly?

Upvotes: 1

Views: 1739

Answers (9)

zajcev
zajcev

Reputation:

db_input[0]= new DatabaseInput();

It actually had the i instead of 0, I just tried several things out, and missed that zero there when copying source code, so that's not the answer.

===EDIT===

db_input = new DatabaseInput*[NUMB_SITES];

That worked, thanks Gaston :)

Upvotes: 0

Brian Agnew
Brian Agnew

Reputation: 272427

Doesn't your 'for' loop need to reference db_input[i] ?

Upvotes: 1

Gaston
Gaston

Reputation: 1848

Maybe this:

DatabaseInput *db_input[];
db_input = new DatabaseInput*[NUMB_SITES]; // Creates an array of pointers
for (int i=0; i<NUMB_SITES; i++) db_input[i]= new DatabaseInput();

could work? (I didn't test it)

Note, to free the memory used, you should do something like:

for (int i=0; i<NUMB_SITES; i++) delete db_input[i];
delete[] db_input;

Upvotes: 1

David Cournapeau
David Cournapeau

Reputation: 80770

We can't determine the error without more details. But a few remarks:

  • mixing new and malloc, specially in the same function, is really looking for trouble. Unless you have a really good reason to do so, do not do it, as you are very likely to use free on a new-allocated buffer or delete and a malloc allocated one.
  • you most likely have an error when you allocate individual DatabaseInput, as the index never changes

Upvotes: 0

Daniel H.
Daniel H.

Reputation: 1842

At first look

db_input[0]= new DatabaseInput();

I think it should be

db_input[i]= new DatabaseInput();

It is also recommended to check the result of the malloc operation

Upvotes: 2

anon
anon

Reputation:

Your code prompts several questions:

  • what is the declaration of RuntimeConfiguration?
  • why are you mixing the use of malloc and new?
  • what aren't you using C++ containers like std::vector?

Upvotes: 3

Fabio Vinicius Binder
Fabio Vinicius Binder

Reputation: 13214

Perhaps, you should use db_input[i]

Upvotes: 0

paxdiablo
paxdiablo

Reputation: 882776

I'd change

for (int i=0;i<NUMB_SITES;i++) db_input[0]= new DatabaseInput();

to this for a start:

for (int i=0;i<NUMB_SITES;i++) db_input[i]= new DatabaseInput();

Upvotes: 6

unwind
unwind

Reputation: 400159

First ... Did you never hear about (default) constructors? This reads like C code using "new", which is always a bit scary.

Second, all your newly allocated structures are stored at db_input[0], which seems wrong.

Upvotes: 2

Related Questions