I am getting a segmentation fault in a function in a C++ program. "fields" is a two dimensional array that was passed to the function. Each "row" in fields is a 32 character array, and the total number of "rows" is numfields, also passed to the function. I have double checked the number and it's correct.
The segmentation fault is occurring at the strncpy. I have also tried strcpy and the commented out code. - for(int x = 0; x < numfields; x++) {
-
int mismatch = 1;
-
int y = 0;
-
-
while( y < numselect && mismatch != 0 ) {
-
char fieldname[11];
-
cout << "before for loop" << endl;
-
cout.flush();
-
-
// for(int z = 0; z < 10; z++)
-
// fieldname[z] = fields[x][z];
-
// fieldname[10] = '\0';
-
strncpy(fieldname, fields[x], 10);
-
-
mismatch = strcmp(fieldname, selFields[y]);
-
if(mismatch == 0)
-
select[x] = true;
-
y++;
-
}
-
}
Please let me know if you can see something that I don't, or if I haven't given enough info.
The segmentation fault occurs on the first iteration of the loops when x and y are both 0.
Thanks.
6 2032
I am getting a segmentation fault in a function in a C++ program. "fields" is a two dimensional array that was passed to the function. Each "row" in fields is a 32 character array, and the total number of "rows" is numfields, also passed to the function. I have double checked the number and it's correct.
The segmentation fault is occurring at the strncpy. I have also tried strcpy and the commented out code. - for(int x = 0; x < numfields; x++) {
-
int mismatch = 1;
-
int y = 0;
-
-
while( y < numselect && mismatch != 0 ) {
-
char fieldname[11];
-
cout << "before for loop" << endl;
-
cout.flush();
-
-
// for(int z = 0; z < 10; z++)
-
// fieldname[z] = fields[x][z];
-
// fieldname[10] = '\0';
-
strncpy(fieldname, fields[x], 10);
-
-
mismatch = strcmp(fieldname, selFields[y]);
-
if(mismatch == 0)
-
select[x] = true;
-
y++;
-
}
-
}
Please let me know if you can see something that I don't, or if I haven't given enough info.
The segmentation fault occurs on the first iteration of the loops when x and y are both 0.
Thanks.
Hi,
Can you post the calling function which is calling this.
I am asking for a asample code which is calling this in which the seg error is happening,.
Raghuram
Hi,
Can you post the calling function which is calling this.
I am asking for a asample code which is calling this in which the seg error is happening,.
Raghuram
I ended up going with a new algorithm to avoid the segmentation fault. Thanks for the reply, though.
Passing 2-D array to a function. I think you need to check it out yourself.
I don't think you need to specify the 1st dimension in the calling function, which u mentioned u passed in to the function. The compiler would just ignore it , the important one is the 2nd dimension. Segmentation fault might be encountered due to the improper passing of argument to your function which then access those memory location that you are not allow to.
i am not sure this would help you, just try to give some opinion ^.^
Well I came across the problem again...
Here is what is relevant in main: - #include <iostream>
-
#include <fstream>
-
#include <ctime>
-
using namespace std;
-
-
int getInput(char*, char*, char*, char**);
-
void getFieldInfo(char**, fstream &, int, bool*, char**, int);
-
-
int main() {
-
char** selectfields;
-
char** fieldnames;
-
bool* select;
-
int numselect;
-
-
numselect = getInput(tablename, wherefield, wherevalue, selectfields);
-
-
// some code that determines headersize, not really important
-
int numfields = ((headersize-1)/32)-1;
-
-
getFieldInfo(fieldnames, file, numfields, select, selectfields, numselect);
-
-
return 0;
-
}
Now in getInput (selectNum is getting the correct value, I have checked that): - int getInput(char* tablename, char* wherefield, char* wherevalue, char** selectfields) {
-
char command[81];
-
-
cout << "Command >> ";
-
cin.getline(command,80);
-
-
int numSelect = 1;
-
int x = 0;
-
-
while( x < 81 && command[x] != '\0' ) {
-
if( command[x] == ',' )
-
numSelect++;
-
x++;
-
}
-
-
selectfields = new char*[numSelect];
-
for( x = 0; x < numSelect; x++ ) {
-
selectfields[x] = new char[20];
-
}
-
-
int position = 7;
-
int fieldpos = 0;
-
x = 0;
-
while( x < numSelect ) {
-
if(command[position] == ' ' || command[position] == ',') {
-
selectfields[x][fieldpos] = '\0';
-
position++;
-
x++;
-
fieldpos = 0;
-
while(x < numSelect && (command[position] == ' ' || command[position] == ','))
-
position++;
-
}
-
else {
-
selectfields[x][fieldpos] = command[position];
-
fieldpos++;
-
position++;
-
}
-
}
-
-
return numSelect;
-
}
The command I am entering now only has 1 select field, and when I cout selectfields[0] in the previous function it works.
Now for whatever reason, when I go to the next function that's called in main, selectfields[0] causes a segmentation fault. - void getFieldInfo(char** fieldnames, fstream &file, int numfields, bool* select, char** selFields, int numselect) {
-
-
cout << "0: " << selFields[0] << endl;
-
cout.flush();
-
-
// some commented out stuff
-
-
}
A char** is not a 2D array. A char*[] is, and you do still have to supply a size here, I believe. There's a very good article on this in the Howtos section that you probably ought to read.
A char** is not a 2D array. A char*[] is, and you do still have to supply a size here, I believe. There's a very good article on this in the Howtos section that you probably ought to read.
Thanks, I was under the impression that they were the same thing. I have fixed it now.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: sivignon |
last post by:
Hi,
I'm writing a php script which deals with 3 ORACLE databases.
This script is launch by a script shell on an linux machine like this :
/../php/bin/php ./MySript.php (PHP 4.3.3)
My script...
|
by: diyanat |
last post by:
i am writing a cgi script in C using the CGIC library, the script
fails to run, i am using apache on linux
error report from apache : internal server error
Premature end of script headers:...
|
by: laberth |
last post by:
I've got a segmentation fault on a calloc and I don'tunderstand why?
Here is what I use :
typedef struct noeud {
int val;
struct noeud *fgauche;
struct noeud *fdroit;
} *arbre; //for those...
|
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...
|
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...
|
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)?...
|
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!
...
|
by: pycraze |
last post by:
I would like to ask a question. How do one handle the exception due to
Segmentation fault due to Python ? Our bit operations and arithmetic
manipulations are written in C and to some of our...
|
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...
|
by: DanielJohnson |
last post by:
int main()
{
printf("\n Hello World");
main;
return 0;
}
This program terminate just after one loop while the second program
goes on infinitely untill segmentation fault (core dumped) on...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |