473,378 Members | 1,321 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,378 software developers and data experts.

[C++] Segmentation Fault {C++ Novice Programmer}

Hello guys.

I have a function, cleanSpace [which takes in a string such as
"this is\ta\ta a test". It cleans it up by removing new
lines/tabs and multiple/trailing and inbetween whitespace. I am
recieving a segmentation fault on one line when I apply it to a second
program [my second school assignment]. I was told from another source
that the problem is, is that the function is acting on constant string
literal. To fix this I was told to take the incoming string, copy it to
a buffer, work on the buffer and copy the buffer back to the original.
I tried but no sucess.

The seg fault is shown after the code snippet below.
int Account::cleanSpace(char c[])
{
int i=0,
j=0,
k=0,
m=0;

/* Ultimately this loop will scan for new lines and tabs and replace
them
with spaces. */
for(i=0; c[i]; i++)
{
if(c[i] == '\n' || c[i] == '\t')
c[i] = ' ';
}

/* For loop finds character starting point. */
for(i=0; c[i] == ' '; i++)
{
c[m] = c[i+1];
}

/* For loop moves all characters next to the first found character.
*/
for(i++; c[i]; i++)
{
c[++m] = c[i];
}

/* For loop removes trailing spaces. */
for(i = strlen(c) - 1; c[i] == ' '; i--)
{
c[i] = '\0';
}

/*For loop removes excess spaces. */
for(i = 0; c[i]; i++)
{
if(c[i] == ' ' && c[i+1] == ' ')
{
j = i;

while(c[j] == ' ')
{
j++;
}

for(k = i + 1; c[k]; k++, j++)
{
c[k] = c[j];
}
j=0;
}
}
return strlen(c);
}
Here is the function that calls it
int Account::changeCustomerInfo(char c[])
{
int result = 0;
char temp[251];
result = strlen(c) - 250;

cleanSpace(c); //Calls the cleanSpace function

if(strlen(c) <= 250){
strcpy(this->customer, c);
}
else{
for(int i = 0; i < 250; i++){
temp[i] = c[i];
}
strcpy(this->customer, temp);}

if(strlen(c) > 250){
return result;}

else return 0;
}
I recieve the seg fault on this code snippet below [which is found in
the cleanSpace function]

/* For loop moves all characters next to the first found character.
*/
for(i++; c[i]; i++)
{
c[++m] = c[i];
}

So by working with
int Account::cleanSpace(char c[]) function, how can I make it work not
on a string literal.

Thanks in advance. Appreciate any help whatsoever.

Jul 23 '05 #1
6 2550

"AMT2K5" <Aa*********@gmail.com> wrote in message
news:11********************@g49g2000cwa.googlegrou ps.com...
Hello guys.

I have a function, cleanSpace [which takes in a string such as
"this is\ta\ta a test". It cleans it up by removing new
lines/tabs and multiple/trailing and inbetween whitespace. I am
recieving a segmentation fault on one line when I apply it to a second
program [my second school assignment]. I was told from another source
that the problem is, is that the function is acting on constant string
literal. To fix this I was told to take the incoming string, copy it to
a buffer, work on the buffer and copy the buffer back to the original.
I tried but no sucess.

The seg fault is shown after the code snippet below.
int Account::cleanSpace(char c[])
{
int i=0,
j=0,
k=0,
m=0;

/* Ultimately this loop will scan for new lines and tabs and replace
them
with spaces. */
for(i=0; c[i]; i++)
{
if(c[i] == '\n' || c[i] == '\t')
c[i] = ' ';
}

/* For loop finds character starting point. */
for(i=0; c[i] == ' '; i++)
{
c[m] = c[i+1];
}

/* For loop moves all characters next to the first found character.
*/
for(i++; c[i]; i++)
{
c[++m] = c[i];
}

/* For loop removes trailing spaces. */
for(i = strlen(c) - 1; c[i] == ' '; i--)
{
c[i] = '\0';
}

/*For loop removes excess spaces. */
for(i = 0; c[i]; i++)
{
if(c[i] == ' ' && c[i+1] == ' ')
{
j = i;

while(c[j] == ' ')
{
j++;
}

for(k = i + 1; c[k]; k++, j++)
{
c[k] = c[j];
}
j=0;
}
}
return strlen(c);
}
Here is the function that calls it
int Account::changeCustomerInfo(char c[])
{
int result = 0;
char temp[251];
result = strlen(c) - 250;

cleanSpace(c); //Calls the cleanSpace function

if(strlen(c) <= 250){
strcpy(this->customer, c);
}
else{
for(int i = 0; i < 250; i++){
temp[i] = c[i];
}
strcpy(this->customer, temp);}

if(strlen(c) > 250){
return result;}

else return 0;
}
I recieve the seg fault on this code snippet below [which is found in
the cleanSpace function]

/* For loop moves all characters next to the first found character.
*/
for(i++; c[i]; i++)
{
c[++m] = c[i];
}

So by working with
int Account::cleanSpace(char c[]) function, how can I make it work not
on a string literal.

Thanks in advance. Appreciate any help whatsoever.


I didn't throughly analyze all conditions, but on the strings "XXX\t\n" and
" X ", it worked fine for me. (I substituted the member customer for a
local char[500] array, and made the functions global, but that shouldn't
affect things unless you've got other problems in the class that end up
causing this crash sometime later.) You should try debugging the app and
seeing what the values of c,i and m are the time it crashes. Perhaps
there's some condition you're not accounting for properly.

-Howard

Jul 23 '05 #2

"AMT2K5" <Aa*********@gmail.com> wrote in message
news:11********************@g49g2000cwa.googlegrou ps.com...
Hello guys.

I have a function, cleanSpace [which takes in a string such as
"this is\ta\ta a test". It cleans it up by removing new
lines/tabs and multiple/trailing and inbetween whitespace. I am
recieving a segmentation fault on one line when I apply it to a second
program [my second school assignment]. I was told from another source
that the problem is, is that the function is acting on constant string
literal. To fix this I was told to take the incoming string, copy it to
a buffer, work on the buffer and copy the buffer back to the original.
I tried but no sucess.

The seg fault is shown after the code snippet below.
int Account::cleanSpace(char c[])
{
int i=0,
j=0,
k=0,
m=0;

/* Ultimately this loop will scan for new lines and tabs and replace
them
with spaces. */
for(i=0; c[i]; i++)
{
if(c[i] == '\n' || c[i] == '\t')
c[i] = ' ';
}

/* For loop finds character starting point. */
for(i=0; c[i] == ' '; i++)
{
c[m] = c[i+1];
}

/* For loop moves all characters next to the first found character.
*/
for(i++; c[i]; i++)
{
c[++m] = c[i];
}

/* For loop removes trailing spaces. */
for(i = strlen(c) - 1; c[i] == ' '; i--)
{
c[i] = '\0';
}

/*For loop removes excess spaces. */
for(i = 0; c[i]; i++)
{
if(c[i] == ' ' && c[i+1] == ' ')
{
j = i;

while(c[j] == ' ')
{
j++;
}

for(k = i + 1; c[k]; k++, j++)
{
c[k] = c[j];
}
j=0;
}
}
return strlen(c);
}
Here is the function that calls it
int Account::changeCustomerInfo(char c[])
{
int result = 0;
char temp[251];
result = strlen(c) - 250;

cleanSpace(c); //Calls the cleanSpace function

if(strlen(c) <= 250){
strcpy(this->customer, c);
}
else{
for(int i = 0; i < 250; i++){
temp[i] = c[i];
}
strcpy(this->customer, temp);}

if(strlen(c) > 250){
return result;}

else return 0;
}
I recieve the seg fault on this code snippet below [which is found in
the cleanSpace function]

/* For loop moves all characters next to the first found character.
*/
for(i++; c[i]; i++)
{
c[++m] = c[i];
}

So by working with
int Account::cleanSpace(char c[]) function, how can I make it work not
on a string literal.

Thanks in advance. Appreciate any help whatsoever.


I'm taking C and C++ classes right now. The cpp file I attached contains a
program I wrote using C to take user input from the keyboard (whether it's
got letters, numbers, spaces, or other characters), strips all
non-alphabetic characters, removes all spaces, and puts it back into a
sentence. Of course the requirement for this particular program was to
translate English into Pig Latin, so it does some things you don't want.

I make no guarantees about the code (I'm still learning, too). You'll
notice it's in a cpp file and not a c file because I kept getting memory
errors from the executable when using Dev-C++; those errors went away when I
compiled it in an empty VisualC++ project. Maybe somebody else could point
out why.

Dave


Jul 23 '05 #3
>>I was told from another source
that the problem is, is that the function is acting on constant string
literal. To fix this I was told to take the incoming string, copy it to
a buffer, work on the buffer and copy the buffer back to the original.

I think you didn't fix problem right.

You are still working on const string.

Fix:

You are actually making copy of string into "temp". But you are not
passing temp to "changeCustomerInfo()". You are still passing "c" to
"changeCustomerInfo()". "c" is const string, that is why the crash.

Instead you should pass "temp"

-Nagarajan Makala

Howard wrote: "AMT2K5" <Aa*********@gmail.com> wrote in message
news:11********************@g49g2000cwa.googlegrou ps.com...
Hello guys.

I have a function, cleanSpace [which takes in a string such as
"this is\ta\ta a test". It cleans it up by removing new
lines/tabs and multiple/trailing and inbetween whitespace. I am
recieving a segmentation fault on one line when I apply it to a second
program [my second school assignment]. I was told from another source
that the problem is, is that the function is acting on constant string
literal. To fix this I was told to take the incoming string, copy it to
a buffer, work on the buffer and copy the buffer back to the original.
I tried but no sucess.

The seg fault is shown after the code snippet below.
int Account::cleanSpace(char c[])
{
int i=0,
j=0,
k=0,
m=0;

/* Ultimately this loop will scan for new lines and tabs and replace
them
with spaces. */
for(i=0; c[i]; i++)
{
if(c[i] == '\n' || c[i] == '\t')
c[i] = ' ';
}

/* For loop finds character starting point. */
for(i=0; c[i] == ' '; i++)
{
c[m] = c[i+1];
}

/* For loop moves all characters next to the first found character.
*/
for(i++; c[i]; i++)
{
c[++m] = c[i];
}

/* For loop removes trailing spaces. */
for(i = strlen(c) - 1; c[i] == ' '; i--)
{
c[i] = '\0';
}

/*For loop removes excess spaces. */
for(i = 0; c[i]; i++)
{
if(c[i] == ' ' && c[i+1] == ' ')
{
j = i;

while(c[j] == ' ')
{
j++;
}

for(k = i + 1; c[k]; k++, j++)
{
c[k] = c[j];
}
j=0;
}
}
return strlen(c);
}
Here is the function that calls it
int Account::changeCustomerInfo(char c[])
{
int result = 0;
char temp[251];
result = strlen(c) - 250;

cleanSpace(c); //Calls the cleanSpace function

if(strlen(c) <= 250){
strcpy(this->customer, c);
}
else{
for(int i = 0; i < 250; i++){
temp[i] = c[i];
}
strcpy(this->customer, temp);}

if(strlen(c) > 250){
return result;}

else return 0;
}
I recieve the seg fault on this code snippet below [which is found in
the cleanSpace function]

/* For loop moves all characters next to the first found character.
*/
for(i++; c[i]; i++)
{
c[++m] = c[i];
}

So by working with
int Account::cleanSpace(char c[]) function, how can I make it work not
on a string literal.

Thanks in advance. Appreciate any help whatsoever.


I didn't throughly analyze all conditions, but on the strings "XXX\t\n" and
" X ", it worked fine for me. (I substituted the member customer for a
local char[500] array, and made the functions global, but that shouldn't
affect things unless you've got other problems in the class that end up
causing this crash sometime later.) You should try debugging the app and
seeing what the values of c,i and m are the time it crashes. Perhaps
there's some condition you're not accounting for properly.

-Howard


Jul 23 '05 #4
That makes sense, thanks. Although I am unsure where I would declare
and state what temp is.

Original call
void Account::init(char c[], char num[], int b)
{
changeCustomerInfo(c);
changeAccountNumber(num);
changeBalance(b);
}

I cant do the following though, because temp would just be empty.

void Account::init(char c[], char num[], int b)
{
char temp[300];

changeCustomerInfo(temp);
changeAccountNumber(num);
changeBalance(b);
}
What am I overlooking here?

Nagarajan Makala wrote:
I was told from another source
that the problem is, is that the function is acting on constant string
literal. To fix this I was told to take the incoming string, copy it to
a buffer, work on the buffer and copy the buffer back to the original.


I think you didn't fix problem right.

You are still working on const string.

Fix:

You are actually making copy of string into "temp". But you are not
passing temp to "changeCustomerInfo()". You are still passing "c" to
"changeCustomerInfo()". "c" is const string, that is why the crash.

Instead you should pass "temp"

-Nagarajan Makala

Howard wrote:
"AMT2K5" <Aa*********@gmail.com> wrote in message
news:11********************@g49g2000cwa.googlegrou ps.com...
Hello guys.

I have a function, cleanSpace [which takes in a string such as
"this is\ta\ta a test". It cleans it up by removing new
lines/tabs and multiple/trailing and inbetween whitespace. I am
recieving a segmentation fault on one line when I apply it to a second
program [my second school assignment]. I was told from another source
that the problem is, is that the function is acting on constant string
literal. To fix this I was told to take the incoming string, copy it to
a buffer, work on the buffer and copy the buffer back to the original.
I tried but no sucess.

The seg fault is shown after the code snippet below.
int Account::cleanSpace(char c[])
{
int i=0,
j=0,
k=0,
m=0;

/* Ultimately this loop will scan for new lines and tabs and replace
them
with spaces. */
for(i=0; c[i]; i++)
{
if(c[i] == '\n' || c[i] == '\t')
c[i] = ' ';
}

/* For loop finds character starting point. */
for(i=0; c[i] == ' '; i++)
{
c[m] = c[i+1];
}

/* For loop moves all characters next to the first found character.
*/
for(i++; c[i]; i++)
{
c[++m] = c[i];
}

/* For loop removes trailing spaces. */
for(i = strlen(c) - 1; c[i] == ' '; i--)
{
c[i] = '\0';
}

/*For loop removes excess spaces. */
for(i = 0; c[i]; i++)
{
if(c[i] == ' ' && c[i+1] == ' ')
{
j = i;

while(c[j] == ' ')
{
j++;
}

for(k = i + 1; c[k]; k++, j++)
{
c[k] = c[j];
}
j=0;
}
}
return strlen(c);
}
Here is the function that calls it
int Account::changeCustomerInfo(char c[])
{
int result = 0;
char temp[251];
result = strlen(c) - 250;

cleanSpace(c); //Calls the cleanSpace function

if(strlen(c) <= 250){
strcpy(this->customer, c);
}
else{
for(int i = 0; i < 250; i++){
temp[i] = c[i];
}
strcpy(this->customer, temp);}

if(strlen(c) > 250){
return result;}

else return 0;
}
I recieve the seg fault on this code snippet below [which is found in
the cleanSpace function]

/* For loop moves all characters next to the first found character.
*/
for(i++; c[i]; i++)
{
c[++m] = c[i];
}

So by working with
int Account::cleanSpace(char c[]) function, how can I make it work not
on a string literal.

Thanks in advance. Appreciate any help whatsoever.


I didn't throughly analyze all conditions, but on the strings "XXX\t\n" and
" X ", it worked fine for me. (I substituted the member customer for a
local char[500] array, and made the functions global, but that shouldn't
affect things unless you've got other problems in the class that end up
causing this crash sometime later.) You should try debugging the app and
seeing what the values of c,i and m are the time it crashes. Perhaps
there's some condition you're not accounting for properly.

-Howard


Jul 23 '05 #5
ah forgot strcpy(temp, c);

Jul 23 '05 #6
AMT2K5 wrote:
Hello guys.

I have a function, cleanSpace [which takes in a string such as
"this is\ta\ta a test". It cleans it up by removing new
lines/tabs and multiple/trailing and inbetween whitespace. I am
recieving a segmentation fault on one line when I apply it to a second
program [my second school assignment]. I was told from another source
that the problem is, is that the function is acting on constant string
literal. To fix this I was told to take the incoming string, copy it to
a buffer, work on the buffer and copy the buffer back to the original.
I tried but no sucess.

[snip]
Thanks in advance. Appreciate any help whatsoever.


Here's some alternatives. Compile and run this program
to see how it works.

// cleanSpace.cpp
// trims leading and trailing whitespace from a string and
// replaces multiple sequential whitespace chars within
// the string with a single space.

// for cin and cout:
#include <iostream>

// for std::string:
#include <string>

// for std::istringstream and std::ostringstream
#include <sstream>

// for strcpy() and strdup()
#include <cstring>

// a version using a ref to a std::string as input.
// the input string ('str') is modified.
int cleanSpace(std::string& str)
{
if (0 == str.length())
return 0;

// no words read from the input yet
int wordCount = 0;

// make a string input stream from 'str'
std::istringstream wordsIn(str);

// make a string output stream to hold the result
std::ostringstream wordsOut;

// we'll read each word from the input string into 'word'
std::string word;

// while there is data in the input string stream
while (wordsIn)
{
// skip any leading whitespace and read the next word
// into 'word'
wordsIn >> word;

// if we actually read a word (i.e. we havn't reached
// the end of the input data
if (wordsIn)
{
// if we have already put at least one word
// to the output string, append a blank before
// we write the next word to the output string
if (wordCount)
wordsOut << ' ';

// append the word just read from the input string
// to the output string
wordsOut << word;

// erase the content of 'word' so it's ready to
// receive the next word read from the input string
word.erase();

// increment the count of words read from the input
// string
wordCount++;
}
}

// replace the original contents of 'str' with
// the content of the output string stream
str = wordsOut.str();

// return the final number of chars in 'str'
return str.length();
}
// a version using a char array as input.
// the input array ('c') is modified.
int cleanSpace(char * c)
{
if (NULL == c)
return 0;

int wordCount = 0;

// make an input string stream from the contents of 'c'.
std::istringstream wordsIn(c);

std::ostringstream wordsOut;
std::string word;

while (wordsIn)
{
wordsIn >> word;

if (wordsIn)
{
if (wordCount)
wordsOut << ' ';

wordsOut << word;
word.erase();
wordCount++;
}
}

// copy the contents of the output string stream
// to 'c' - replacing the original contents of 'c'
strcpy(c, wordsOut.str().c_str());

return strlen(c);
}

#if 0
// an alternate approach for using a char array.
// makes a string from the array, calls cleanSpace(string),
// then copies the result to the original input array ('c')
int cleanSpace(char * c)
{
if (NULL == c)
return 0;

int len;
std::string str(c);

len = cleanSpace(str);
strcpy(c, str.c_str());

return len;
}
#endif

int main()
{
int len;
char * c;
std::string s;

std::cout << "enter a string: ";
std::getline(std::cin, s);

// make a copy of the input string as a char array
c = strdup(s.c_str());

// call the version of cleanSpace() that takes a string
len = cleanSpace(s);
std::cout << "string len " << len << ": '" << s
<< "'" << std::endl;

// call the version of cleanSpace that takes a char array
len = cleanSpace(c);
std::cout << "c[] len " << len << ": '" << c
<< "'" << std::endl;

return 0;
}

Regards,
Larry
Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: AMT2K5 | last post by:
Hello folks, I seem to have recieved a segfault within my function but am unsure on how to resolve it. I understand that it means that somewhere something is trying to access memory that is not...
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...
15
by: Matthew Jakeman | last post by:
can anyone tell me why the following piece of code causes a segmentation fault please ?? char *getFNFromIden(char *ident) { int i = 0 ; printf("identifier %s\n", allMenus.identifier) ;...
7
by: Alexandre | last post by:
Hello, Maybe it's a little OT, but the fact is that I don't necessarly want to know "how to correct?", but "why it happens?" I have a program who "segment fault" (ok, that's "normal"... ;-)...
3
by: Zheng Da | last post by:
Program received signal SIGSEGV, Segmentation fault. 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 (gdb) bt #0 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 #1 0x40094c54 in malloc...
5
by: Fra-it | last post by:
Hi everybody, I'm trying to make the following code running properly, but I can't get rid of the "SEGMENTATION FAULT" error message when executing. Reading some messages posted earlier, I...
27
by: Paminu | last post by:
I have a wierd problem. In my main function I print "test" as the first thing. But if I run the call to node_alloc AFTER the printf call I get a segmentation fault and test is not printed! ...
7
by: Kies | last post by:
Hi everyone, these days I find a problem.Look this source code: 1 #include <stdio.h> 2 3 int 4 main(void) 5 { 6 char a = "hello"; 7 a = 'X';...
3
by: madunix | last post by:
My Server is suffering bad lag (High Utlization) I am running on that server Oracle10g with apache_1.3.35/ php-4.4.2 Web visitors retrieve data from the web by php calls through oci cobnnection...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.