By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,127 Members | 1,343 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,127 IT Pros & Developers. It's quick & easy.

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

P: n/a

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();
}
Jul 22 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
"fudmore" <fu*****@mindspring.com> 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();
}

Jul 22 '05 #2

P: n/a
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];
Jul 22 '05 #3

P: n/a
"fudmore" <fu*****@mindspring.com> wrote...
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:
"fudmore" <fu*****@mindspring.com> 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();
}


Jul 22 '05 #4

P: n/a

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 Bazarov wrote:
"fudmore" <fu*****@mindspring.com> wrote...
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:
"fudmore" <fu*****@mindspring.com> 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();
}




Jul 22 '05 #5

P: n/a
"fudmore" <fu*****@mindspring.com> wrote...

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 Bazarov wrote:
"fudmore" <fu*****@mindspring.com> wrote...
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:

"fudmore" <fu*****@mindspring.com> 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();
>}
>
>


Jul 22 '05 #6

P: n/a
Cool. I trust the people that implemented the STL for now. Thanks for all your help victor.
Victor Bazarov wrote:
"fudmore" <fu*****@mindspring.com> wrote...
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 Bazarov wrote:
"fudmore" <fu*****@mindspring.com> wrote...
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:
>"fudmore" <fu*****@mindspring.com> 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();
>>}
>>
>>
>
>
>



Jul 22 '05 #7

P: n/a
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:4%IOb.83302$5V2.109578@attbi_s53...
"fudmore" <fu*****@mindspring.com> wrote...

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"?


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
Jul 22 '05 #8

P: n/a
"Jonathan Turkanis" <te******@kangaroologic.com> wrote...
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:4%IOb.83302$5V2.109578@attbi_s53...
"fudmore" <fu*****@mindspring.com> wrote...

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"?


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).
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

Jul 22 '05 #9

P: n/a
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:NG%Ob.85910$Rc4.334063@attbi_s54...
"Jonathan Turkanis" <te******@kangaroologic.com> wrote...
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:4%IOb.83302$5V2.109578@attbi_s53...
"fudmore" <fu*****@mindspring.com> wrote... 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.


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

Jul 22 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.