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

Segmentation Fault

drumgirl67
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.

Expand|Select|Wrap|Line Numbers
  1.     for(int x = 0; x < numfields; x++) {
  2.         int mismatch = 1;
  3.         int y = 0;
  4.  
  5.         while( y < numselect && mismatch != 0 ) {
  6.             char fieldname[11];
  7.             cout << "before for loop" << endl;
  8.             cout.flush();
  9.  
  10. //            for(int z = 0; z < 10; z++)
  11. //                fieldname[z] = fields[x][z];
  12. //            fieldname[10] = '\0';
  13.             strncpy(fieldname, fields[x], 10);
  14.  
  15.             mismatch = strcmp(fieldname, selFields[y]);
  16.             if(mismatch == 0)
  17.                 select[x] = true;
  18.             y++;
  19.         }
  20.     }
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.
Dec 13 '07 #1
6 2029
gpraghuram
1,275 Expert 1GB
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.

Expand|Select|Wrap|Line Numbers
  1.     for(int x = 0; x < numfields; x++) {
  2.         int mismatch = 1;
  3.         int y = 0;
  4.  
  5.         while( y < numselect && mismatch != 0 ) {
  6.             char fieldname[11];
  7.             cout << "before for loop" << endl;
  8.             cout.flush();
  9.  
  10. //            for(int z = 0; z < 10; z++)
  11. //                fieldname[z] = fields[x][z];
  12. //            fieldname[10] = '\0';
  13.             strncpy(fieldname, fields[x], 10);
  14.  
  15.             mismatch = strcmp(fieldname, selFields[y]);
  16.             if(mismatch == 0)
  17.                 select[x] = true;
  18.             y++;
  19.         }
  20.     }
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
Dec 13 '07 #2
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.
Dec 13 '07 #3
primeSo
35
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 ^.^
Dec 13 '07 #4
Well I came across the problem again...

Here is what is relevant in main:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <ctime>
  4. using namespace std;
  5.  
  6. int getInput(char*, char*, char*, char**);
  7. void getFieldInfo(char**, fstream &, int, bool*, char**, int);
  8.  
  9. int main() {
  10.     char** selectfields;
  11.     char** fieldnames;
  12.     bool* select;
  13.         int numselect;
  14.  
  15.     numselect = getInput(tablename, wherefield, wherevalue, selectfields);
  16.  
  17.         // some code that determines headersize, not really important
  18.     int numfields = ((headersize-1)/32)-1;
  19.  
  20.     getFieldInfo(fieldnames, file, numfields, select, selectfields, numselect);
  21.  
  22.     return 0;
  23. }
Now in getInput (selectNum is getting the correct value, I have checked that):
Expand|Select|Wrap|Line Numbers
  1. int getInput(char* tablename, char* wherefield, char* wherevalue, char** selectfields) {
  2.     char command[81];
  3.  
  4.     cout << "Command >> ";
  5.     cin.getline(command,80);
  6.  
  7.     int numSelect = 1;
  8.     int x = 0;
  9.  
  10.     while( x < 81 && command[x] != '\0' ) {
  11.         if( command[x] == ',' )
  12.             numSelect++;
  13.         x++;
  14.     }
  15.  
  16.     selectfields = new char*[numSelect];
  17.     for( x = 0; x < numSelect; x++ ) {
  18.         selectfields[x] = new char[20];
  19.     }
  20.  
  21.     int position = 7;
  22.     int fieldpos = 0;
  23.     x = 0;
  24.     while( x < numSelect ) {
  25.         if(command[position] == ' ' || command[position] == ',') {
  26.             selectfields[x][fieldpos] = '\0';
  27.             position++; 
  28.             x++; 
  29.             fieldpos = 0;
  30.             while(x < numSelect && (command[position] == ' ' || command[position] == ','))
  31.                 position++;
  32.         }
  33.         else {
  34.             selectfields[x][fieldpos] = command[position];
  35.             fieldpos++;
  36.             position++;
  37.         }
  38.     }
  39.  
  40.     return numSelect;
  41. }
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.

Expand|Select|Wrap|Line Numbers
  1. void getFieldInfo(char** fieldnames, fstream &file, int numfields, bool* select, char** selFields, int numselect) {
  2.  
  3.     cout << "0: " << selFields[0] << endl;
  4.     cout.flush();
  5.  
  6. // some commented out stuff
  7.  
  8. }
Dec 13 '07 #5
Laharl
849 Expert 512MB
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.
Dec 13 '07 #6
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.
Dec 13 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
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...
3
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:...
16
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...
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...
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)?...
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: 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...
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...
6
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
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...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
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...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
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...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
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...
1
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 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.