Segmentation Fault - Need assistance resolving {Novice C++ Programmer}

A

AMT2K5

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 prohibited
or not available.

The following function is an overloaded += operator that reads in
strings in a record format seperated by comma delimiters except for the
last data which terminates with a semicolon. I am adding these records
to an array of objects which stores the record. I hope the folowing
code is enough to identify the problem. The static array's are defined
as told in my assignment.

const char* Bank::eek:perator+=(const char a[])
{

char stringTemp[316];
char accountTemp[16];
int balanceTemp;
const char *ptr = a;

sscanf(ptr, "%15[^,],%d,%315[^;];", accountTemp, &balanceTemp,
stringTemp);


savings[size].changeAccountNumber(accountTemp);
savings[size].changeBalance(balanceTemp);
strcat(stringTemp,";");
savings[size].changeCustomerInfo(stringTemp);
size++; //Core dump here

return a;

}
 
V

Victor Bazarov

AMT2K5 said:
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 prohibited
or not available.

The following function is an overloaded += operator that reads in
strings in a record format seperated by comma delimiters except for the
last data which terminates with a semicolon. I am adding these records
to an array of objects which stores the record. I hope the folowing
code is enough to identify the problem. The static array's are defined
as told in my assignment.

const char* Bank::eek:perator+=(const char a[])
{

char stringTemp[316];

Make sure you initiliase it using

char stringTemp[316] = {0};
char accountTemp[16];

Same here.
int balanceTemp;

This one is left uninitialised as well
const char *ptr = a;

There is no need in this, is there? Why don't you just pass 'a' to the
'scanf' below?
sscanf(ptr, "%15[^,],%d,%315[^;];", accountTemp, &balanceTemp,
stringTemp);


savings[size].changeAccountNumber(accountTemp);
savings[size].changeBalance(balanceTemp);
strcat(stringTemp,";");

Since 'stringTemp' wasn't initialised to 0 it probably concatenates the
semicolon to some unsuspecting part of memory -- undefined behaviour.

BTW, are you sure there is enough room in 'stringTemp' to append the
semicolon?
savings[size].changeCustomerInfo(stringTemp);
size++; //Core dump here

return a;

}

V
 
A

AMT2K5

Thanks for the tips. I tried removing the entire function code and
leaving size++; but I still have a seg fault, so I'm not sure what the
problem may be now.

const char* Bank::eek:perator+=(const char a[])
{
size++;
}
 
V

Victor Bazarov

AMT2K5 said:
Thanks for the tips. I tried removing the entire function code and
leaving size++; but I still have a seg fault, so I'm not sure what the
problem may be now.

const char* Bank::eek:perator+=(const char a[])
{
size++;

This function has the return value type of 'const char*'. I don't see any
'return' statement...

That may or may not be the cause of your segmentation fault. Impossible
to tell without seeing how the function is used.

V
 
V

Victor Bazarov

AMT2K5 said:

I am guessing the function _now_ looks like this

const char* Bank::eek:perator+=(const char a[])
{
size++;
return a;
}

Does it still segfault? If it does, then the problem is elsewhere.

It is _very_likely_ though, that you're calling this member function
through an invalid pointer or an invalid reference, which means that
the error lies _outside_ this function.

V
 
D

Default User

AMT2K5 said:
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 prohibited
or not available.


Learn this mantra:

Post a complete, minimal, compliable, program that demonstrates the
problem. You haven't done that here. We don't know what savings or size
are, nor do we see the input data you are running this against.

You are almost assuredly overrunning a buffer, that stringTemp stuff
looks suspicious.



Brian
 
M

Maxim Yegorushkin

On Fri, 15 Jul 2005 20:25:46 +0400, Victor Bazarov

[]
const char* Bank::eek:perator+=(const char a[])
{
char stringTemp[316];

Make sure you initiliase it using

char stringTemp[316] = {0};
char accountTemp[16];

Same here.
int balanceTemp;

This one is left uninitialised as well

There is no need to initialize them provided he checks the return value of
the sscanf call.
 
V

Victor Bazarov

Maxim said:
On Fri, 15 Jul 2005 20:25:46 +0400, Victor Bazarov

[]
const char* Bank::eek:perator+=(const char a[])
{
char stringTemp[316];


Make sure you initiliase it using

char stringTemp[316] = {0};
char accountTemp[16];


Same here.
int balanceTemp;


This one is left uninitialised as well


There is no need to initialize them provided he checks the return value
of the sscanf call.

How is checking of sscanf return value going to help? Does sscanf put the
null terminator into the array when converting "%15[^,]"?

V
 
A

AMT2K5

I can post the whole code but it was be maybe 2-3 pages or so. I dont
know if that would be breaking any rules or not.
 
V

Victor Bazarov

AMT2K5 said:
I can post the whole code but it was be maybe 2-3 pages or so. I dont
know if that would be breaking any rules or not.

You know, before you summon the help of the entire planet for mere 2-3
pages of code, maybe you just need to spend a bit more time in a debugger
stepping through your code and examining the values of your objects? What
is your ultimate goal, to get this 2-3 pages of code to run or to become
a good C++ programmer? Think about it.
 
D

Default User

AMT2K5 said:
I can post the whole code but it was be maybe 2-3 pages or so. I dont
know if that would be breaking any rules or not.

You don't need to. What you need to do is reduce down the program to a
managable size first. Often in doing so you will discover the problem.
As it was, most of the objects in your code were completely undefined
to us, as was the data set.

Yes it will be extra work for you, but you're the one that want's help.



Brian
 
A

AMT2K5

Ok guys, I found the source of the problem - I have commented the
function but would appreciate if you have any tips on fixing it. It
seems to be a pointer problem I believe.

It was not size++ or anything in that function I posted above. I ran a
debugger as mentioned (had to download one as I was using bcc32 in a
command prompt before).

The following function cleans up a string, I have commented important
lines

int cleanSpace(char* ptr)
{
char temp[300];
strcpy(temp, ptr);

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; temp; i++)
{
if(temp == '\n' || temp == '\t')
temp = ' ';
}

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

// For loop moves all characters next to the first found character.
for(i++; temp; i++)
{
temp[++m] = temp;
}
temp[m+1] = '\0';

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

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

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

for(k = i + 1; temp[k]; k++, j++)
{
temp[k] = temp[j];
}
j=0;
}
}
strcpy(ptr,temp); // Copy temp to ptr // Seg fault (Access
violation) happens on this line
return strlen(ptr); // Return the length
}

The segfault occurs on the strcpy(ptr,temp) right above.
 
V

Victor Bazarov

AMT2K5 said:
[...]
The following function cleans up a string, I have commented important
lines

int cleanSpace(char* ptr)
{
char temp[300];
strcpy(temp, ptr);
[...changing 'temp' is snipped...]
strcpy(ptr,temp); // Copy temp to ptr // Seg fault (Access
violation) happens on this line

What's "ptr"? How do you call this function? Are you sure you're allowed
to write as many characters as 'temp' has back into the location where 'ptr'
points?
return strlen(ptr); // Return the length
}

The segfault occurs on the strcpy(ptr,temp) right above.

V
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top