473,396 Members | 2,085 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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


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
9 3144
"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
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
"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

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
"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
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
"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
"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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: mblome | last post by:
Hi everybody! I came across a very strange problem with stl vectors during developement of a mesh creation program. As the program is quite large I can only post small parts of it. Basically I...
1
by: sandwich_eater | last post by:
I get a segmentation fault in my program when calling a function "TestFn" that has been passed as a pointer into another function. The following excerpt should give enough information as to what I...
1
by: Michael Sgier | last post by:
Hello i get the error: Segmentation fault. The debugger stopped at the last of the following lines: // This stores the texture array for each of the textures assigned to this model unsigned...
18
by: Digital Puer | last post by:
Hi, I'm coming over from Java to C++, so please bear with me. In C++, is there a way for me to use exceptions to catch segmentation faults (e.g. when I access a location off the end of an array)?...
1
by: samuel.y.l.cheung | last post by:
Hi, I wrote a template to use copy() algorithm, called copyAll: template<class T> void copyAll(const T& src , T& dest ) { copy (src.begin(), src.end(), back_inserter(dest)); } but when I...
5
by: silverburgh.meryl | last post by:
Hi, I have a segmentation fault in line 66 of GroupResult.h and I can't figure out why that causes any problem, I appreciate if anyone can help. line 66 of Result.h: 66 size_t size()...
8
by: Bryan | last post by:
Hello all. I'm fairly new to c++. I've written several programs using std::vectors, and they've always worked just fine. Until today. The following is a snippet of my code (sorry, can't...
2
by: Steve | last post by:
I have segmentation fault when calling resize on an stl vector. I'm unsure if I'm doing something horribly wrong or if the stl is being dumb. The code breaks down like this: ...
3
by: flyvin | last post by:
I have a vector full of vectors, and whenever I try to access them I get a segmentation fault. It compiled fine, but then when I ran it I got a error. So I ran a debuger and it said a had a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.