Memory Fault (core dumped) in ttpy program


 
Thread Tools Search this Thread
Top Forums Programming Memory Fault (core dumped) in ttpy program
# 1  
Old 10-19-2010
Memory Fault (core dumped) in ttpy program

I´m writing this program in QNX , I`m kinda new to UNIX and programing in general, and when I try to run it it gives me the Memory Fault error. Can anyone help?
Code:
#include <stdio.h>
#include <fcntl.h>
void main(void)
{int a[1],ter;
 char buf[12];
 printf("a=");
scanf("%d",a);
ter=open ("/dev/ttpy1",O_WRONLY);
sprintf(buf,"%s",a[1]);
write(ter,a,sizeof(a));
close(ter);
}

Its supposed to print the scanned string in to a second terminal window (ttpy1), so I open the two terminals before running the program but it gives me the Memory error.
# 2  
Old 10-19-2010
for starters:
Code:
sprintf(buf,"%s",a[1]);

should be "%d" not "%s". This causes the printf code to interpret the variable as a nul-terminated string. It keeps reading the "string" byte-by-byte until it finds ascii 0, nul.
If it reads past the end of the stack, boom.
This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 10-19-2010
Thanks it still doesn't print anything on ttpy1 but at least it doesn't crash anymore xD
# 4  
Old 10-20-2010
Check the return value of write(). Check the return value of everything, really.
# 5  
Old 10-20-2010
I don't want to correct everything....

main is NOT void, it is int. Return to int from main(). Always.

Turn on warnings and every other diagnostic known to the compiler.

Strings in C cannot be less than two characters, if they contain dat. a[1] will not fly generally. Why? they need a terminating nul characters \0 == ascii 0

you do not have all of the #include files required to compile the code.
Read the man page for read & open - it tells you at the top the name of the include file(s)required.

You cannot call a compile ok until:
1. there are zero warnings
2. there are zero errors

Period. Anything else that seems to work is a disaster waiting to happen.
# 6  
Old 10-20-2010
Quote:
Originally Posted by jim mcnamara
I don't want to correct everything....

main is NOT void, it is int. Return to int from main(). Always.

Turn on warnings and every other diagnostic known to the compiler.

Strings in C cannot be less than two characters, if they contain dat. a[1] will not fly generally. Why? they need a terminating nul characters \0 == ascii 0

you do not have all of the #include files required to compile the code.
Read the man page for read & open - it tells you at the top the name of the include file(s)required.

You cannot call a compile ok until:
1. there are zero warnings
2. there are zero errors

Period. Anything else that seems to work is a disaster waiting to happen.
hmm I can compile it with 0 warnings and 0 errors, with int instead of void and adding return 0 at the end and with %d instead of %s, the only problem I have now is that it doesn't print on the second terminal window ttyp1so i think my problem is this line :
ter=open ("/dev/ttpy1",O_WRONLY);

Professor numnuts at college told us that terminals are treated like files and are "mounted" in the /dev/ directory.

As for the missing .h as I said before a have 0 errors so i don`t think that`s the problem, just to be sure I added iostream and fstream .h, and i got invalid library errors on both when I tried to compile.

I am "programing" using a normal text editor and writing in the terminal

#cc program.c -o program

Last edited by GiganteAsesino; 10-20-2010 at 10:31 PM..
# 7  
Old 10-21-2010
You're doing sprintf to buf but writing the original variable "a" which contains the binary representation of the number.

Change
Code:
write(ter,a,sizeof(a));

to
Code:
write(ter,buf,strlen(buf));

Note to use strlen instead of sizeof so you don't write the unused portion of the array to the terminal.

Also, as Corona said, you should be checking return values from all of the library calls. For example, if, for some reason, you cannot open the tty, ter would contain -1. Do things like
Code:
if (ter < 0) { perror("open tty"); exit(errno); }

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

C: Memory Fault (core dumped)

When I run programm show this message: Memory Fault (core dumped) Does anyone can help me and tell me what is wrong? please #include <stdlib.h> #include <stdio.h> #include <process.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> int main(int argc, char *argv) { ... (3 Replies)
Discussion started by: ebasse2
3 Replies

2. UNIX for Advanced & Expert Users

Segmentation Fault/ core dumped in metadb

When I was trying to mirror in my v880 server after OS up gradation from 8 to 10 metadb -afc 3 /dev/dsk/c1t1d0s7 I got an error metadb: Segmentation Fault Segmentation Fault (core dumped) Then I logged a case to Oracle/sun team they suggest "Please could you try metadb -ac 3... (0 Replies)
Discussion started by: taherahmed
0 Replies

3. Programming

getting Segmentation Fault (core dumped) error but Program runs fine.

i am executing following program int main() { char str; FILE * fp; int i=0; ... (4 Replies)
Discussion started by: bhavesh.sapra
4 Replies

4. Red Hat

Segmentation fault (core dumped)

Hi All, I am getting "Segmentation fault (core dumped)" error in the runtime. I am new this please can you tell me why is that i am getting this error and I am not sure of my compilation : gcc -c avc_test.c gcc -c md5.c gcc avc_test.o md5.o -shared -Llibcoreavc_sdk.so -o proj ... (1 Reply)
Discussion started by: fido.genial
1 Replies

5. Solaris

segmentation fault core dumped

i am getting segmentation fault (core dumped) i tried following things but couldn't understand what is wrong with my prog and where the problem is.... i have only adb debugger available on solaris.... so plz help bash-3.00$ pstack core core 'core' of 765: ./mod_generalised_tapinread... (4 Replies)
Discussion started by: junaid.nehvi
4 Replies

6. Solaris

Segmentation Fault (core dumped)

i am getting Segmentation Fault (core dumped) on solaris, but when i run the same program with same input on linux it runs successfully. How can i trace the fault in program on solaris. (6 Replies)
Discussion started by: junaid.nehvi
6 Replies

7. Programming

Memory Fault,Core dumped

I have written a code in UNIX which is complied by using g++. Compling with turbo C didnt yield any errors, but with g++ I am getting Memory fault, core dumped. Could anyone help me out with this? Given below is the code: #include<stdio.h> #include<string.h> #include<stdlib.h>... (2 Replies)
Discussion started by: usshell
2 Replies

8. UNIX for Advanced & Expert Users

Memory Fault - Core Dumped

I use SCO UNIX 5.07 on a Compaq Proliant Machine. Each time I press the Escape or Delete key while running a program or issuing a FoxBase+ command from the dot prompt, I receive the error message: "Memory Fault - Core Dumped" and the screen locks up immediately. I would appreciate if necessary... (0 Replies)
Discussion started by: bayuz
0 Replies

9. Programming

Segmentation Fault (core dumped)

what r the situations to receive an error msg like the one below Segmentation Fault (core dumped) (2 Replies)
Discussion started by: bankpro
2 Replies

10. UNIX for Dummies Questions & Answers

Segmentation fault (core dumped)

Hello To All! Now anfd then I receive a message on my console: Segmentation fault (core dumped) What does it mean? Or more precisely what are the implications? :confused: (1 Reply)
Discussion started by: Ivo
1 Replies
Login or Register to Ask a Question