Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

segmentation fault while writing user program in linux

Status
Not open for further replies.

garimella

Full Member level 5
Joined
Aug 25, 2011
Messages
260
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,296
Activity points
3,276
I am trying to write a simple device driver and application program to access the driver.

Driver write system call looks like this


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
static ssize_t dev_write(struct file *filp, const char *buff, size_t len, loff_t *off)
{
short c=0;
short ind =0;
short count=0;
memset(msg,0,100);
readPos=0;
while(len>0)
{
 msg[count++]=buff[ind++];
 len--;
}


and application program which works looks as follows


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int main()
{
char buf[100];
char i = 0;
char tag=0;
 
int fp;
char *val[10];
char *rval[10];
unsigned int a =18440;
unsigned int d =1;
memset(buf, 0, 10);
tag=1;
fp = open("/dev/dd",O_RDWR);
if (fp<0)
{ 
  printf("file failed to open\n");
}  
sprintf(val[1],"%d%x%x",tag,a,d);
write(fp,val[1],strlen(val[1]));
sprintf(rval[1],"%d%x",tag,a);// statement that causes segmentation fault
return 0;
}



Now if I add one more sprintf function as shown above , it leads to segmentation fault.I do not know why?. If I remove this line, the code works fine. What could be the problem? The same problem happens when I call sprintf and write as a subfunction call?
 
Last edited by a moderator:

I'm not at a compiler right now but at a glance, should there be a 'close' to match the 'open' statement?
Also (again this is from my memory only so treat as suspect!) should 'fp' be declared as a pointer?

Brian.
 

I am not going through your code line by line but have a couple of suggestions:

1. Check the plocks; just see that the { and } are nicely matched.

2. Please try with printf first (whether it works) and then replace that with sprintf. Then check the syntax. What is there in val[] and rval[]??
 

Looks like a trivial C programming issue.

You are declaring arrays of pointers char *val[10] and char *rval[10] but never assigning an actual storage location to it, e.g. val[1] = buf.
 

val[1] gets assigned to the concatenated values of tag,a and d. This works fine for me. Now by adding only rval[1] statement leads to fault. Do not understand correlation. rval[1] holds tag and a in string format. Also checked the syntax and everything works without the second sprintf statement as shown in my code.
 

Presuming you didn't omit part of the code, please show me which string storage is addressed by pointers val[1] and rval[1].
Using uninitialized pointers isn't a syntax rather than a sematic error. Some compilers might flag a warning.
 

which string storage is addressed by pointers val[1] and rval[1]...

Correct; segmentation faults are caused by reference to memory outside the allowed range...

I do not see any proper reference for rval (which is a pointer).

I request the original poster to study pointers in a bit more detail; they can be messy and I myself get confused so often...
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top