473,378 Members | 1,479 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.

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

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::operator+=(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;

}

Jul 23 '05 #1
15 1863
AMT2K5 wrote:
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::operator+=(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
Jul 23 '05 #2
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::operator+=(const char a[])
{
size++;
}

Jul 23 '05 #3
AMT2K5 wrote:
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::operator+=(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
Jul 23 '05 #4
Return a

Jul 23 '05 #5
AMT2K5 wrote:
Return a


I am guessing the function _now_ looks like this

const char* Bank::operator+=(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
Jul 23 '05 #6
AMT2K5 wrote:
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
Jul 23 '05 #7
On Fri, 15 Jul 2005 20:25:46 +0400, Victor Bazarov
<v.********@comAcast.net> wrote:

[]
const char* Bank::operator+=(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.

--
Maxim Yegorushkin
<fi****************@gmail.com>
Jul 23 '05 #8
Maxim Yegorushkin wrote:
On Fri, 15 Jul 2005 20:25:46 +0400, Victor Bazarov
<v.********@comAcast.net> wrote:

[]
const char* Bank::operator+=(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
Jul 23 '05 #9
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.

Jul 23 '05 #10
AMT2K5 wrote:
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.
Jul 23 '05 #11
AMT2K5 wrote:
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
Jul 23 '05 #12
On Fri, 15 Jul 2005 22:53:40 +0400, Victor Bazarov
<v.********@comAcast.net> wrote:

[]
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[^,]"?


Sure it does.
http://www.opengroup.org/onlinepubs/...ons/scanf.html

--
Maxim Yegorushkin
<fi****************@gmail.com>
Jul 23 '05 #13
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]; i++)
{
if(temp[i] == '\n' || temp[i] == '\t')
temp[i] = ' ';
}

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

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

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

// For loop removes excess spaces.
for(i = 0; temp[i]; i++)
{
if(temp[i] == ' ' && 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.

Jul 23 '05 #14
it is called via cleanSpace(c);

when it is passed a string

Jul 23 '05 #15
AMT2K5 wrote:
[...]
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
Jul 23 '05 #16

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

Similar topics

6
by: AMT2K5 | last post by:
Hello guys. I have a function, cleanSpace . 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...
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...
1
by: matthew | last post by:
Hi, I have Python 2-4-2, Numpy 24.2, and PyOpenGL-2.0.2.01 install, along with glut 3.7. The 1st 'funny' thing is that I always seem to get two pythons in /usr/local/bin, ie python and...
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...
14
by: ptq2238 | last post by:
Hi, I'm getting confused with arrays and hope someone can shed light on my code. I wrote this to try and learn about files and arrays as I thought if I could grab each element and place them into...
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
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...

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.