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

segmentation fault

satyanagendra
hi
I already write a database program in c and it is working fine. I change some modifications in the structure which holds the data in the database in that i got segmentation fault. the code is like this

struct student
{
int a;
char n[50];
student *nxt;
};
struct dummy
{
int a;
char n[50];
}

main()
{
student *head;
read(&head);
}

void read(student *head)
{
FILE *fp;
dummy s;
student *temp;
fp = fopen("trial.txt","rb");

while( fread(&s,sizeof(dummy),1,fp))
{
temp->a = s.a;
strcpy(temp->n,s.n);
temp->nxt = malloc(sizeof(student));
temp = temp->nxt;
}

fclose(fp);
}

the above code is working fine

but instead of int a in structure student if i place char p[20] reading from file gives segmentation fault. I write to file using fwrite function. But if i create pointer
instead of variable for struct dummy the problem solves i.e if write below code for reading it works.

void read(student *head)
{
FILE *fp;
dummy *s;
student *temp;
fp = fopen("trial.txt","rb");

while( fread(&s,sizeof(dummy),1,fp))
{
temp->a = s->a;
strcpy(temp->n,s->n);
temp->nxt = malloc(sizeof(student));
temp = temp->nxt;
}

fclose(fp);
}

If any body know the reason please tell me. If u dont understand my problem i will send total code to ur mail id if u give ur mail id .

thank u
regards
n s n reddy
Aug 23 '07 #1
11 1540
Meetee
931 Expert Mod 512MB
hi
I already write a database program in c and it is working fine. I change some modifications in the structure which holds the data in the database in that i got segmentation fault. the code is like this

struct student
{

}
Hi Satya,

Please give full code and give some details regarding problem.

Regards
Aug 23 '07 #2
Hi Satya,

Please give full code and give some details regarding problem.

Regards
please give me ur mail id because code is in my pc but i am contacting u from my office system.
Aug 23 '07 #3
Meetee
931 Expert Mod 512MB
please give me ur mail id because code is in my pc but i am contacting u from my office system.
I am sorry as I cannot give my mail id. It's also against forum rules being a member of TSDN. Your problem and solution can help many others on this forum. You better post your code snippet so others can also help you and refer your post.

Best regards,
Zodilla
Aug 23 '07 #4
primeSo
35
I am sorry as I cannot give my mail id. It's also against forum rules being a member of TSDN. Your problem and solution can help many others on this forum. You better post your code snippet so others can also help you and refer your post.

Best regards,
Zodilla
Ya, i would like to learn from this problem too.
Aug 23 '07 #5
I am sorry as I cannot give my mail id. It's also against forum rules being a member of TSDN. Your problem and solution can help many others on this forum. You better post your code snippet so others can also help you and refer your post.

Best regards,
Zodilla
Actually it is a project the code is in totally 4 files can i paste all four files code here.
Aug 23 '07 #6
xoinki
110 100+
hi,
I think you have not allocated for temp object. allocate using calloc or malloc.
if u declare a pointer you need to allocate memory before using it.

Thnks and Regards
xoinki
Aug 23 '07 #7
hi,
I think you have not allocated for temp object. allocate using calloc or malloc.
if u declare a pointer you need to allocate memory before using it.

Thnks and Regards
xoinki
After allocating the memory to the pointer the problem was solved but my problem is when i am using a variable then only i get the segmentation problem
Aug 23 '07 #8
xoinki
110 100+
hi,
Whether "p[20]" is enough? if u write more than 20 bytes.. obviously the program gives segmentation fault. I would suggest you to change all arrays to pointers then allocate, copy and finally cleanup module to free all loopin throgh the linked list.

Regards
Xoinki
Aug 23 '07 #9
hi,
Whether "p[20]" is enough? if u write more than 20 bytes.. obviously the program gives segmentation fault. I would suggest you to change all arrays to pointers then allocate, copy and finally cleanup module to free all loopin throgh the linked list.

Regards
Xoinki
In p[20] i am using only 10 bytes. and the problem is while i am writing these records into the file using fread or fwrite here the program terminates with segmentation fault
Aug 23 '07 #10
xoinki
110 100+
hi,
dont u think this ,
Expand|Select|Wrap|Line Numbers
  1. while( fread(&s,sizeof(dummy),1,fp))
  2.  
should be

Expand|Select|Wrap|Line Numbers
  1. while( fread(&s,sizeof(dummy),1,fp) != EOF)
  2.  
Regards,
Xoinki
Aug 23 '07 #11
hi,
dont u think this ,
Expand|Select|Wrap|Line Numbers
  1. while( fread(&s,sizeof(dummy),1,fp))
  2.  
should be

Expand|Select|Wrap|Line Numbers
  1. while( fread(&s,sizeof(dummy),1,fp) != EOF)
  2.  
Regards,
Xoinki
ThankQ i will try it
Aug 27 '07 #12

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...
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: 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
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: 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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.