In some none understandable cases, push_back for vectors does not work

Hi all,

I really don't understand what happen. In my test program, it works :

1
2
3
4
5
6
7
8
9
10
11
12
vector<string> t1;
cout << "t1 should be empty -        size =  " << t1.size() << ", contents : "; for (u_int32_t i = 0; i < t1.size(); i++) cout << t1[i] << " "; cout << endl;
t1.push_back("str1");
cout << "t1 should have 1 element -  size =  " << t1.size() << ", contents : "; for (u_int32_t i = 0; i < t1.size(); i++) cout << t1[i] << " "; cout << endl;
t1.push_back("str2");
cout << "t1 should have 2 elements - size =  " << t1.size() << ", contents : "; for (u_int32_t i = 0; i < t1.size(); i++) cout << t1[i] << " "; cout << endl;
vector<string> t2 = {"str1", "str2"};
cout << "t2 should have 2 elements - size =  " << t2.size() << ", contents : "; for (u_int32_t i = 0; i < t2.size(); i++) cout << t2[i] << " "; cout << endl;
t2.push_back("str3");
cout << "t2 should have 3 elements - size =  " << t2.size() << ", contents : "; for (u_int32_t i = 0; i < t2.size(); i++) cout << t2[i] << " "; cout << endl;
t2.push_back("str4");
cout << "t2 should have 4 elements - size =  " << t2.size() << ", contents : "; for (u_int32_t i = 0; i < t2.size(); i++) cout << t2[i] << " "; cout << endl;


t1 should be empty -        size =  0, contents : 
t1 should have 1 element -  size =  1, contents : str1 
t1 should have 2 elements - size =  2, contents : str1 str2 
t2 should have 2 elements - size =  2, contents : str1 str2 
t2 should have 3 elements - size =  3, contents : str1 str2 str3 
t2 should have 4 elements - size =  4, contents : str1 str2 str3 str4 


But in my application, push_back does nothing. I checked with the debugger, it does nothing, nor throw anything, nor complain.

1
2
3
vector<string> col = {"col1", "col2"};
// some code
col.push_back("col3");


col before push_back = "col1", "col2"
col after push_back  = "col1", "col2"


Any idea please ?
¿what about posting your code?
hmmm! can u expect people to go through it, no, but u r lucky, i like vectors, please paste ur code in proper format next time.
To traverse a vector use a vector iterator:
1
2
3
4
vector<int>::iterator it;
 cout << "myvector contains:";
 for ( it=myvector.begin() ; it < myvector.end(); it++ )
 cout << " " << *it;

modify ur code accordingly.
There is nothing wrong with using an index to traverse a vector. He doesn't need to modify his code at all.

EDIT:

That said, for other container types like a deque, list, etc.... yes you should use iterators. And it makes sense to use iterators with vectors also for sake of consistency.

But it's not worth going back and changing the code for.
Last edited on
pls share ur entire code, it may be of help to find whats wrong.
Sorry for answering so late - I was not in.

Here is my code :

main.cpp : http://pastebin.com/xZUMk89T
MyFile.h : http://pastebin.com/4BeFNfuu
MyFile.cpp : http://pastebin.com/pvrwB2Lw

What does not work are the push_back in main.cpp at lines :

1
2
3
  col.push_back("file1");
  typ.push_back("u_int32_t");
  mod.push_back(READ);


The vectors are not increased with the data passed as arguments and remains with its size unchanged.
Last edited on
Strange, because I changed nothing in the code, and now push_back works normally ! But my code still have problems.

When I am in this section of code with the debugger :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
  col.push_back("file1");
  typ.push_back("u_int32_t");
  mod.push_back(READ);
  vector<u_int32_t> file1_bleams(getHCS(QT_RED_DESKS, id));
  vec.push_back(static_cast<void *>(&file1_bleams));
  if (getHCSd(DateFile1Calc, id, 0) < getHCS(DateMinFile1Found, id)) {
   cout << "Calcul du fichage de rang 1....." << endl;
   mod[2] = WRITE;
   TableInFile t(file1_name, col, typ, vec, mod);
   calc_file1(clipBleams, file1_bleams);
   t.write();
   setHCS(DateFile1Calc, id, time(NULL));
   mod[0] = mod[1] = mod[2] = IGNORE;
  }


t exists before the constructor is called !!! t is created in other and previous sections of the code, but in separate blocks that should have led it to be destroyed. I don't thing the compiler or the debugger anticipates ?
Local variables are all pushed to the stack at the very start of the function, regardless of where in the function they are declared. So even if you declare a variable at the very last line of a function, the debugger will see it at the very start of the function.

However, the constructor is not called until the line in which you declare the variable is reached... so if you look at 't' in the debugger before it's declared, you'll notice it contains garbage / uninitialized values.

EDIT: is that what you are asking?
Last edited on
Yes, that was the question. Thanks Disch. But the data in t before the constructor is called TableInFile t(file1_name, col, typ, vec, mod); is not garbage !!!
Yes it is. =P
OK. Thanks.

The initial problem is not anymore reproducable. I really don't know nor understand what happened.

Anyway I have learned and I thank you very much all of you.
Topic archived. No new replies allowed.