Connecting Tech Pros Worldwide Forums | Help | Site Map

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

fudmore
Guest
 
Posts: n/a
#1: Jul 22 '05

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();
}



Victor Bazarov
Guest
 
Posts: n/a
#2: Jul 22 '05

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


"fudmore" <fudmore@mindspring.com> wrote...[color=blue]
> 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.[/color]

What's "list" in your program?
[color=blue]
>
>
>
> Code Snippet
> --------------------------------------------------------------------------[/color]
-------------[color=blue]
>
> 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();[/color]

Why do you want to clear it here? What's the purpose of clearing?
[color=blue]
>
> }
>
> counter++;
> delete (str);
> str = parser.readField();
> }
>
>[/color]


ixmine
Guest
 
Posts: n/a
#3: Jul 22 '05

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


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];
Victor Bazarov
Guest
 
Posts: n/a
#4: Jul 22 '05

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


"fudmore" <fudmore@mindspring.com> wrote...[color=blue]
> sorry for not mentioning that but list is
>
> vector <string>list(WORDS_PER_LINE);[/color]

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
[color=blue]
>
> Victor Bazarov wrote:[color=green]
> > "fudmore" <fudmore@mindspring.com> wrote...
> >[color=darkred]
> >>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.[/color]
> >
> >
> > What's "list" in your program?
> >
> >[color=darkred]
> >>
> >>
> >>Code Snippet[/color][/color]
>[color=green]
>>--------------------------------------------------------------------------
> >
> > -------------
> >[color=darkred]
> >>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();[/color]
> >
> >
> > Why do you want to clear it here? What's the purpose of clearing?
> >
> >[color=darkred]
> >>}
> >>
> >>counter++;
> >>delete (str);
> >>str = parser.readField();
> >>}
> >>
> >>[/color]
> >
> >
> >[/color]
>[/color]


fudmore
Guest
 
Posts: n/a
#5: Jul 22 '05

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



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:[color=blue]
> "fudmore" <fudmore@mindspring.com> wrote...
>[color=green]
>>sorry for not mentioning that but list is
>>
>>vector <string>list(WORDS_PER_LINE);[/color]
>
>
> 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
>
>[color=green]
>>Victor Bazarov wrote:
>>[color=darkred]
>>>"fudmore" <fudmore@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[/color]
>>[color=darkred]
>>>--------------------------------------------------------------------------
>>>
>>>-------------
>>>
>>>
>>>>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();
>>>>}
>>>>
>>>>
>>>
>>>
>>>[/color][/color]
>
>[/color]

Victor Bazarov
Guest
 
Posts: n/a
#6: Jul 22 '05

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


"fudmore" <fudmore@mindspring.com> wrote...[color=blue]
>
> Hmm, then i have another question. If I do this.
>
>
> vector <string> list(10);[/color]

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

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.
[color=blue]
> list[1] = *ptr;[/color]

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

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.
[color=blue]
> list[1] = *ptr;
>
>
> will list[1] free up the memory used by firstname and then replace it with[/color]
last name ?

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

Well, if you trust the maker of the standard library implementation you
are using, then you should have no problem.
[color=blue]
>
> I did all my programming in Java and I never realized memory allocation[/color]
and de-allocation was such a[color=blue]
> complicated issue. How do you know who is resposible for de-allocating[/color]
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"?
[color=blue]
>
>
>
>
> Victor Bazarov wrote:[color=green]
> > "fudmore" <fudmore@mindspring.com> wrote...
> >[color=darkred]
> >>sorry for not mentioning that but list is
> >>
> >>vector <string>list(WORDS_PER_LINE);[/color]
> >
> >
> > 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
> >
> >[color=darkred]
> >>Victor Bazarov wrote:
> >>
> >>>"fudmore" <fudmore@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[/color][/color][/color]
the[color=blue][color=green][color=darkred]
> >>>>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 ( //[/color][/color][/color]
list.clear())[color=blue][color=green][color=darkred]
> >>>>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
> >>[/color][/color]
>[color=green][color=darkred]
>>>-------------------------------------------------------------------------[/color][/color][/color]
-[color=blue][color=green][color=darkred]
> >>>
> >>>-------------
> >>>
> >>>
> >>>>while(str != NULL)
> >>>>{
> >>>>index = counter % WORDS_PER_LINE;
> >>>>list[index] = *str;
> >>>>
> >>>>cout << "i:" << index << " c:" << counter << " word : " << *str <<[/color][/color][/color]
endl;[color=blue][color=green][color=darkred]
> >>>>
> >>>>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();
> >>>>}
> >>>>
> >>>>
> >>>
> >>>
> >>>[/color]
> >
> >[/color]
>[/color]


fudmore
Guest
 
Posts: n/a
#7: Jul 22 '05

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


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


Victor Bazarov wrote:[color=blue]
> "fudmore" <fudmore@mindspring.com> wrote...
>[color=green]
>>Hmm, then i have another question. If I do this.
>>
>>
>>vector <string> list(10);[/color]
>
>
> A vector of ten strings. The vector maintains the memory, it will release
> the memory when it goes out of scope.
>
>[color=green]
>>string *ptr = getFirstName();[/color]
>
>
> 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.
>
>[color=green]
>>list[1] = *ptr;[/color]
>
>
> So, the actual string is "extracted" and _copied_ by assignment to the
> element number one in the vector. Fine.
>
>[color=green]
>>
>>ptr = getLastname();[/color]
>
>
> 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.
>
>[color=green]
>>list[1] = *ptr;
>>
>>
>>will list[1] free up the memory used by firstname and then replace it with[/color]
>
> last name ?
>
> Yes, it will. It should be taken care of by the 'string's assignment
> operator.
>
>[color=green]
>>If the answer is yes then it's ok otherwise that is a memory Leak.[/color]
>
>
> Well, if you trust the maker of the standard library implementation you
> are using, then you should have no problem.
>
>[color=green]
>>I did all my programming in Java and I never realized memory allocation[/color]
>
> and de-allocation was such a
>[color=green]
>>complicated issue. How do you know who is resposible for de-allocating[/color]
>
> 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"?
>
>[color=green]
>>
>>
>>
>>Victor Bazarov wrote:
>>[color=darkred]
>>>"fudmore" <fudmore@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" <fudmore@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[/color][/color]
>
> the
>[color=green][color=darkred]
>>>>>>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 ( //[/color][/color]
>
> list.clear())
>[color=green][color=darkred]
>>>>>>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
>>>>
>>>>-------------------------------------------------------------------------[/color][/color]
>
> -
>[color=green][color=darkred]
>>>>>-------------
>>>>>
>>>>>
>>>>>
>>>>>>while(str != NULL)
>>>>>>{
>>>>>>index = counter % WORDS_PER_LINE;
>>>>>>list[index] = *str;
>>>>>>
>>>>>>cout << "i:" << index << " c:" << counter << " word : " << *str <<[/color][/color]
>
> endl;
>[color=green][color=darkred]
>>>>>>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();
>>>>>>}
>>>>>>
>>>>>>
>>>>>
>>>>>
>>>>>
>>>[/color][/color]
>
>[/color]

Jonathan Turkanis
Guest
 
Posts: n/a
#8: Jul 22 '05

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


"Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
news:4%IOb.83302$5V2.109578@attbi_s53...[color=blue]
> "fudmore" <fudmore@mindspring.com> wrote...[/color]
[color=blue][color=green]
> >
> > I did all my programming in Java and I never realized memory[/color][/color]
allocation[color=blue]
> and de-allocation was such a[color=green]
> > complicated issue. How do you know who is resposible for[/color][/color]
de-allocating[color=blue]
> memory?
>
> You simply learn. If you need to know how 'std::string' manages its[/color]
memory,[color=blue]
> you open the source code for the library you have and read. Have[/color]
you never[color=blue]
> heard the expression "Use the Source, Luke"?
>[/color]

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


Victor Bazarov
Guest
 
Posts: n/a
#9: Jul 22 '05

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


"Jonathan Turkanis" <technews@kangaroologic.com> wrote...[color=blue]
> "Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
> news:4%IOb.83302$5V2.109578@attbi_s53...[color=green]
> > "fudmore" <fudmore@mindspring.com> wrote...[/color]
>[color=green][color=darkred]
> > >
> > > I did all my programming in Java and I never realized memory[/color][/color]
> allocation[color=green]
> > and de-allocation was such a[color=darkred]
> > > complicated issue. How do you know who is resposible for[/color][/color]
> de-allocating[color=green]
> > memory?
> >
> > You simply learn. If you need to know how 'std::string' manages its[/color]
> memory,[color=green]
> > you open the source code for the library you have and read. Have[/color]
> you never[color=green]
> > heard the expression "Use the Source, Luke"?
> >[/color]
>
> I think this is bad advice for a beginner.[/color]

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).
[color=blue]
> 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
>
>[/color]


Jonathan Turkanis
Guest
 
Posts: n/a
#10: Jul 22 '05

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


"Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
news:NG%Ob.85910$Rc4.334063@attbi_s54...[color=blue]
> "Jonathan Turkanis" <technews@kangaroologic.com> wrote...[color=green]
> > "Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
> > news:4%IOb.83302$5V2.109578@attbi_s53...[color=darkred]
> > > "fudmore" <fudmore@mindspring.com> wrote...[/color][/color][/color]
[color=blue][color=green][color=darkred]
> > > you open the source code for the library you have and read.[/color][/color][/color]
Have[color=blue][color=green]
> > you never[color=darkred]
> > > heard the expression "Use the Source, Luke"?
> > >[/color]
> >
> > I think this is bad advice for a beginner.[/color]
>
> I totally agree. But once somebody begins being interested how[/color]
memory[color=blue]
> is managed, they are not beginners any longer. And reading others'[/color]
code[color=blue]
> is one of the best ways to learn (regardless of whether the code is[/color]
good[color=blue]
> or bad).
>[/color]

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



Closed Thread


Similar C / C++ bytes