I'm getting a Segmentation fault after I clear a vector list

F

fudmore

Hello Everybody.

I have a Segmentation fault problem. The code section at the bottom
keeps throwing a Segmentation fault when it enters the IF block for the
second time.

const int WORDS_PER_LINE = 4;

when counter == 7 is when the string Concatenation fails within the IF
block.

BUT, the strange part is if the don't CLEAR the list ( // list.clear())
it works... but i'm afraid that I'm not freeing up memory when i
reassign different strings to the vector.



Code Snippet
---------------------------------------------------------------------------------------

while(str != NULL)
{
index = counter % WORDS_PER_LINE;
list[index] = *str;

cout << "i:" << index << " c:" << counter << " word : " << *str << endl;

if(index == (WORDS_PER_LINE-1))
{
string ref =

string("INSERT INTO BANKING(DATE,TRANS_TYPE,AMOUNT,BALANCE)
VALUES(TO_DATE('") +
list[0] +
string("','MM/DD/YYYY'),'") +
list[1] +
string("',") +
list[2] +
string(",") +
list[3]+
string(");");

cout << "processing .." << ref << endl;

list.clear();

}

counter++;
delete (str);
str = parser.readField();
}
 
V

Victor Bazarov

fudmore said:
I have a Segmentation fault problem. The code section at the bottom
keeps throwing a Segmentation fault when it enters the IF block for the
second time.

const int WORDS_PER_LINE = 4;

when counter == 7 is when the string Concatenation fails within the IF
block.

BUT, the strange part is if the don't CLEAR the list ( // list.clear())
it works... but i'm afraid that I'm not freeing up memory when i
reassign different strings to the vector.

What's "list" in your program?
Code Snippet
-------------------------------------------------------------------------- -------------

while(str != NULL)
{
index = counter % WORDS_PER_LINE;
list[index] = *str;

cout << "i:" << index << " c:" << counter << " word : " << *str << endl;

if(index == (WORDS_PER_LINE-1))
{
string ref =

string("INSERT INTO BANKING(DATE,TRANS_TYPE,AMOUNT,BALANCE)
VALUES(TO_DATE('") +
list[0] +
string("','MM/DD/YYYY'),'") +
list[1] +
string("',") +
list[2] +
string(",") +
list[3]+
string(");");

cout << "processing .." << ref << endl;

list.clear();

Why do you want to clear it here? What's the purpose of clearing?
 
I

ixmine

i guess there has no use for 'list.clear()', because 'list' is
fixed to hold 4 strings in it.

after you invoke 'list.clear()', the size of 'list' change to 0,
then 'list[index]' would generate an error(index out of range).


Since 'WORDS_PER_LINE' is a const, maybe C arrays can be used.
std::string args[WORDS_PER_LINE];
 
V

Victor Bazarov

fudmore said:
sorry for not mentioning that but list is

vector <string>list(WORDS_PER_LINE);

So, did you get your answer? When you define your "list" like this,
it contains 'WORDS_PER_LINE' elements to begin with. As soon as you
clear it, there are no more elements, and you cannot refer to any of
them using operator[] any longer until you resize the vector.

So, the solution is not to clear it. Simple.

Victor
Victor said:
fudmore said:
I have a Segmentation fault problem. The code section at the bottom
keeps throwing a Segmentation fault when it enters the IF block for the
second time.

const int WORDS_PER_LINE = 4;

when counter == 7 is when the string Concatenation fails within the IF
block.

BUT, the strange part is if the don't CLEAR the list ( // list.clear())
it works... but i'm afraid that I'm not freeing up memory when i
reassign different strings to the vector.


What's "list" in your program?

Code Snippet
--------------------------------------------------------------------------

-------------

while(str != NULL)
{
index = counter % WORDS_PER_LINE;
list[index] = *str;

cout << "i:" << index << " c:" << counter << " word : " << *str << endl;

if(index == (WORDS_PER_LINE-1))
{
string ref =

string("INSERT INTO BANKING(DATE,TRANS_TYPE,AMOUNT,BALANCE)
VALUES(TO_DATE('") +
list[0] +
string("','MM/DD/YYYY'),'") +
list[1] +
string("',") +
list[2] +
string(",") +
list[3]+
string(");");

cout << "processing .." << ref << endl;

list.clear();


Why do you want to clear it here? What's the purpose of clearing?

}

counter++;
delete (str);
str = parser.readField();
}
 
F

fudmore

Hmm, then i have another question. If I do this.


vector <string> list(10);

string *ptr = getFirstName();
list[1] = *ptr;


ptr = getLastname();
list[1] = *ptr;


will list[1] free up the memory used by firstname and then replace it with last name ?
If the answer is yes then it's ok otherwise that is a memory Leak.

I did all my programming in Java and I never realized memory allocation and de-allocation was such a
complicated issue. How do you know who is resposible for de-allocating memory?




Victor said:
fudmore said:
sorry for not mentioning that but list is

vector <string>list(WORDS_PER_LINE);


So, did you get your answer? When you define your "list" like this,
it contains 'WORDS_PER_LINE' elements to begin with. As soon as you
clear it, there are no more elements, and you cannot refer to any of
them using operator[] any longer until you resize the vector.

So, the solution is not to clear it. Simple.

Victor

Victor said:
I have a Segmentation fault problem. The code section at the bottom
keeps throwing a Segmentation fault when it enters the IF block for the
second time.

const int WORDS_PER_LINE = 4;

when counter == 7 is when the string Concatenation fails within the IF
block.

BUT, the strange part is if the don't CLEAR the list ( // list.clear())
it works... but i'm afraid that I'm not freeing up memory when i
reassign different strings to the vector.


What's "list" in your program?




Code Snippet
--------------------------------------------------------------------------

-------------


while(str != NULL)
{
index = counter % WORDS_PER_LINE;
list[index] = *str;

cout << "i:" << index << " c:" << counter << " word : " << *str << endl;

if(index == (WORDS_PER_LINE-1))
{
string ref =

string("INSERT INTO BANKING(DATE,TRANS_TYPE,AMOUNT,BALANCE)
VALUES(TO_DATE('") +
list[0] +
string("','MM/DD/YYYY'),'") +
list[1] +
string("',") +
list[2] +
string(",") +
list[3]+
string(");");

cout << "processing .." << ref << endl;

list.clear();


Why do you want to clear it here? What's the purpose of clearing?



}

counter++;
delete (str);
str = parser.readField();
}
 
V

Victor Bazarov

fudmore said:
Hmm, then i have another question. If I do this.


vector <string> list(10);

A vector of ten strings. The vector maintains the memory, it will release
the memory when it goes out of scope.
string *ptr = getFirstName();

You're getting a pointer. Are you sure it's a pointer to a valid string?
Where is the object located? Dynamic memory? Who's to release it? It
may not matter in this example, but you should be able to answer those
questions.
list[1] = *ptr;

So, the actual string is "extracted" and _copied_ by assignment to the
element number one in the vector. Fine.
ptr = getLastname();

Again, you should know where the actual object and why you're getting
only the pointer to it, and, which is probably more important, who's
controlling the lifetime of the object. As before, it doesn't really
matter in this example, though.
list[1] = *ptr;


will list[1] free up the memory used by firstname and then replace it with
last name ?

Yes, it will. It should be taken care of by the 'string's assignment
operator.
If the answer is yes then it's ok otherwise that is a memory Leak.

Well, if you trust the maker of the standard library implementation you
are using, then you should have no problem.
I did all my programming in Java and I never realized memory allocation and de-allocation was such a
complicated issue. How do you know who is resposible for de-allocating
memory?

You simply learn. If you need to know how 'std::string' manages its memory,
you open the source code for the library you have and read. Have you never
heard the expression "Use the Source, Luke"?
Victor said:
fudmore said:
sorry for not mentioning that but list is

vector <string>list(WORDS_PER_LINE);


So, did you get your answer? When you define your "list" like this,
it contains 'WORDS_PER_LINE' elements to begin with. As soon as you
clear it, there are no more elements, and you cannot refer to any of
them using operator[] any longer until you resize the vector.

So, the solution is not to clear it. Simple.

Victor

Victor Bazarov wrote:



I have a Segmentation fault problem. The code section at the bottom
keeps throwing a Segmentation fault when it enters the IF block for the
second time.

const int WORDS_PER_LINE = 4;

when counter == 7 is when the string Concatenation fails within the IF
block.

BUT, the strange part is if the don't CLEAR the list ( // list.clear())
it works... but i'm afraid that I'm not freeing up memory when i
reassign different strings to the vector.


What's "list" in your program?




Code Snippet

------------------------------------------------------------------------- -

-------------


while(str != NULL)
{
index = counter % WORDS_PER_LINE;
list[index] = *str;

cout << "i:" << index << " c:" << counter << " word : " << *str << endl;

if(index == (WORDS_PER_LINE-1))
{
string ref =

string("INSERT INTO BANKING(DATE,TRANS_TYPE,AMOUNT,BALANCE)
VALUES(TO_DATE('") +
list[0] +
string("','MM/DD/YYYY'),'") +
list[1] +
string("',") +
list[2] +
string(",") +
list[3]+
string(");");

cout << "processing .." << ref << endl;

list.clear();


Why do you want to clear it here? What's the purpose of clearing?



}

counter++;
delete (str);
str = parser.readField();
}
 
F

fudmore

Cool. I trust the people that implemented the STL for now. Thanks for all your help victor.


Victor said:
fudmore said:
Hmm, then i have another question. If I do this.


vector <string> list(10);


A vector of ten strings. The vector maintains the memory, it will release
the memory when it goes out of scope.

string *ptr = getFirstName();


You're getting a pointer. Are you sure it's a pointer to a valid string?
Where is the object located? Dynamic memory? Who's to release it? It
may not matter in this example, but you should be able to answer those
questions.

list[1] = *ptr;


So, the actual string is "extracted" and _copied_ by assignment to the
element number one in the vector. Fine.

ptr = getLastname();


Again, you should know where the actual object and why you're getting
only the pointer to it, and, which is probably more important, who's
controlling the lifetime of the object. As before, it doesn't really
matter in this example, though.

list[1] = *ptr;


will list[1] free up the memory used by firstname and then replace it with

last name ?

Yes, it will. It should be taken care of by the 'string's assignment
operator.

If the answer is yes then it's ok otherwise that is a memory Leak.


Well, if you trust the maker of the standard library implementation you
are using, then you should have no problem.

I did all my programming in Java and I never realized memory allocation

and de-allocation was such a
complicated issue. How do you know who is resposible for de-allocating

memory?

You simply learn. If you need to know how 'std::string' manages its memory,
you open the source code for the library you have and read. Have you never
heard the expression "Use the Source, Luke"?

Victor said:
sorry for not mentioning that but list is

vector <string>list(WORDS_PER_LINE);


So, did you get your answer? When you define your "list" like this,
it contains 'WORDS_PER_LINE' elements to begin with. As soon as you
clear it, there are no more elements, and you cannot refer to any of
them using operator[] any longer until you resize the vector.

So, the solution is not to clear it. Simple.

Victor



Victor Bazarov wrote:





I have a Segmentation fault problem. The code section at the bottom
keeps throwing a Segmentation fault when it enters the IF block for
the
second time.

const int WORDS_PER_LINE = 4;

when counter == 7 is when the string Concatenation fails within the IF
block.

BUT, the strange part is if the don't CLEAR the list ( //
list.clear())
it works... but i'm afraid that I'm not freeing up memory when i
reassign different strings to the vector.


What's "list" in your program?




Code Snippet

-------------------------------------------------------------------------
-
-------------



while(str != NULL)
{
index = counter % WORDS_PER_LINE;
list[index] = *str;

cout << "i:" << index << " c:" << counter << " word : " << *str <<
endl;
if(index == (WORDS_PER_LINE-1))
{
string ref =

string("INSERT INTO BANKING(DATE,TRANS_TYPE,AMOUNT,BALANCE)
VALUES(TO_DATE('") +
list[0] +
string("','MM/DD/YYYY'),'") +
list[1] +
string("',") +
list[2] +
string(",") +
list[3]+
string(");");

cout << "processing .." << ref << endl;

list.clear();


Why do you want to clear it here? What's the purpose of clearing?




}

counter++;
delete (str);
str = parser.readField();
}
 
J

Jonathan Turkanis

Victor Bazarov said:
allocation
and de-allocation was such a de-allocating
memory?

You simply learn. If you need to know how 'std::string' manages its memory,
you open the source code for the library you have and read. Have you never
heard the expression "Use the Source, Luke"?

I think this is bad advice for a beginner. The standard, not a
particular implementation, determines what a user needs to know about
how a string manages memory. So I would start a good C++ introduction
which covers the standard library. It is nice to know various
implementation strategies for std::basic_string, but it is rarely
necessary to consider how your particular implementation works (unless
it is broken).

In certain rare cases a particular standard library implementation may
be unsuitable for a given purpose for performance reasons, and it may
be necessary to switch to a different standard library, but these
considerations are not relevant to the OP's problem.

Jonathan
 
V

Victor Bazarov

Jonathan Turkanis said:
I think this is bad advice for a beginner.

I totally agree. But once somebody begins being interested how memory
is managed, they are not beginners any longer. And reading others' code
is one of the best ways to learn (regardless of whether the code is good
or bad).
 
J

Jonathan Turkanis

Victor Bazarov said:
I totally agree. But once somebody begins being interested how memory
is managed, they are not beginners any longer. And reading others' code
is one of the best ways to learn (regardless of whether the code is good
or bad).

Some of the questions the OP was asking were pretty basic; that's why
I called him a beginner. I don't mean it in a derogatory sense -- we
were all beginners once, and I not so long ago.

Reading others' code is a good way to learn, certainly, but standard
library code is a bad place to start. It's full of identifiers
beginning with leading underscores, macros for compatibility with
multiple compilers/OSs, typically devoid of useful comments and often
broken up into files with funny non-standard names.

For someone currious about implementing strings, I'd recommend
Stroutrup's The C++ Programming Language, 3rd ed., Scott Meyers's More
Effective C++, or Herb Sutter's More Exceptional C++. The last two are
certainly not beginners books, though, and the first requires a
certain amount of sophistication, although no pre-existing knowledge
of C++.

Jonathan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top