#include%26lt;stdio.h%26gt;
#include%26lt;string.h%26gt;
int main()
{
char *p="Hello";
printf("%s\n",p);
printf("%c\n",*p);
*(p)='A'; /*Replace the first letter with 'A'*/
printf("%s\n",p);
return 0;
}
I am using Linux operating system. In windows probably it will not give a segmentation fault.
Why does the following C code give segmentation fault?
Please see http://c-faq.com/decl/strlitinit.htmll .
You're creating a constant string literal, and then trying to modify it. Effectively, you're trying to write over read-only memory, and this is why a segfault occurs.
You could use Windows if you suffer from the "blame Linux" mentality. You'll get a segfault anyway, though.
Reply:To be able to manipulate bytes using a pointer, try using malloc to allocate a block of memory.
char *p = malloc(6);
strcpy(p, "Hello"); // copy string with trailing \0
*p = 'A';
The code you are using points p to a string in a constant table, and the program cannot write to the address.
Reply:As another person explained, you're replacing the
contents of the literal string "Hello", which the
compiler implementation is allowed (but not required)
to place into a read-only memory segment.
For example, on Solaris using the Sun compiler, this
does not cause a segmentation violation by default.
However, if you compile with the "-xstrconst" option
(which puts literal constants in read-only memory),
then of course, it does. The point is that it will
vary from platform to platform.
Reply:Don't use char *p="Hello";
Use
unsigned char p[] = "Hello";
#include%26lt;stdio.h%26gt;
#include%26lt;string.h%26gt;
int main()
{
unsigned char p[]="Hello";
printf("%s\n",p);
printf("%c\n",p[0]);
p[0]='A'; /*Replace the first letter with 'A'*/
printf("%s\n",p);
return 0;
}
The static allocations by the complier are just that "static". For even more fun, compile your "broken" program with full optimization. You'll get no errors and of course no "Aello". The compiler optimizes out the write to read-only segments.
Monday, May 24, 2010
Need help with a c program?
I'm programming a bot to connect to IRC. I'm fairly new at C, and I need to know efficient ways to keep the program from exiting at the end of the main function. As of now, the bot connects and the programme ends, how do I keep it running so that the bot will stay there and I can move on to my next challenge of responding to PINGs
this is what I have
(very dry because I'm trying to make it work before I try new stuff)
if (connect(sockdesc, (struct sockaddr *)%26amp;their_addr,
sizeof their_addr) == -1) {
perror("connect");
exit(1);
}
char *connectreg="NICK test\r\nUSER testbot test test :My bot\r\n";
len = strlen(connectreg);
send(sockdesc, connectreg, len, 0);
return 0;
}
I need it to keep running so that I can start responding to PINGs after the send call.
My main goal as of now is to make it idle on the server, then I'm going to clean up the code a bit.
PS: I'm compiling under a linux system.
Need help with a c program?
You should put it in a while loop, i'm not familiar with C socket but Java socket. And then put a read from the socket stream there, the read will basically wait until there is an input from the remote side.
This is my pseudo code for this
send start signal to server
while(true)
{
readSignal from Server
process Signal
switch (signal)
case signal1 : handle and send proper signal back to server
case signal2 : handle and send proper signal back to server
..etc
}
sweet pea
this is what I have
(very dry because I'm trying to make it work before I try new stuff)
if (connect(sockdesc, (struct sockaddr *)%26amp;their_addr,
sizeof their_addr) == -1) {
perror("connect");
exit(1);
}
char *connectreg="NICK test\r\nUSER testbot test test :My bot\r\n";
len = strlen(connectreg);
send(sockdesc, connectreg, len, 0);
return 0;
}
I need it to keep running so that I can start responding to PINGs after the send call.
My main goal as of now is to make it idle on the server, then I'm going to clean up the code a bit.
PS: I'm compiling under a linux system.
Need help with a c program?
You should put it in a while loop, i'm not familiar with C socket but Java socket. And then put a read from the socket stream there, the read will basically wait until there is an input from the remote side.
This is my pseudo code for this
send start signal to server
while(true)
{
readSignal from Server
process Signal
switch (signal)
case signal1 : handle and send proper signal back to server
case signal2 : handle and send proper signal back to server
..etc
}
sweet pea
Is fwrite in C blocking call?
Is fwrite in C blocking call from the app point of view? For example, if NFS is used as i/o, will the fwrite to a file residing on NFS mounted to the Linux be blocking?
Is fwrite in C blocking call?
yes, fwrite() is a blocking call - your program will not return from it while it is being processed.
Non-blocking calls only exist in programming environments with native threads, such as Windows. They do not really exist in UNIX. On Unix, non-blocking I/O within the same process is achieved using other tricks, such as select ( http://www.scit.wlv.ac.uk/cgi-bin/mansec... ).
Non-blocking equivalent of fwrite() on Windows is WriteFileEx():
http://msdn2.microsoft.com/en-us/library...
Is fwrite in C blocking call?
yes, fwrite() is a blocking call - your program will not return from it while it is being processed.
Non-blocking calls only exist in programming environments with native threads, such as Windows. They do not really exist in UNIX. On Unix, non-blocking I/O within the same process is achieved using other tricks, such as select ( http://www.scit.wlv.ac.uk/cgi-bin/mansec... ).
Non-blocking equivalent of fwrite() on Windows is WriteFileEx():
http://msdn2.microsoft.com/en-us/library...
Which programming language (other than C++ and Visual Basic) is suitable to develop CD/DVD burning software?
In the future, after graduated, I plan to develop an open source CD/DVD burning software which have all the following features:
1) Burn: Data CD/DVD, Video CD (VCD), DVD video, Audio CD. if possible, include Blu-ray disc.
2) Convert: all video files to VCD/DVD format, Audio files to MP3 format.
3) Backup: Any files, or entire computer.
4) Copy: disc to disc, disc to image (example: ISO), image to disc).
5) Edit: video and audio files
6) Supported operating system: Windows, Linux, Mac
7) Price: always free, including the source code.
I don't want to use C++ and Visual Basic
Which programming language (other than C++ and Visual Basic) is suitable to develop CD/DVD burning software?
Probably you would not write all this functionality but reuse libraries for the majority of it.
Due to this, you are more concentrating on the interface. You could look at c# but its mostly windows only (can work on Linux, but limited)
Java is portable and you can use c libraries, but the interfaces tend to suck a bit in my opinion on java apps.
You could use c/++ but I don't think you need to go to so much trouble when your main task is the interface.
Reply:There are a lot of languages that could do this but I think you are looking for a powerful language that easy and reduces the development cost. I think you should take a close look into C#.NET 2005 Express Edition. It's free and surprisingly powerful. You are working with audio and video, and this means you want your software to function at maximum speed. C# offers a number of unsafe execution techniques that could do the job. Beside that it is a .NET language, and this means easy development and debugging platform.
1) Burn: Data CD/DVD, Video CD (VCD), DVD video, Audio CD. if possible, include Blu-ray disc.
2) Convert: all video files to VCD/DVD format, Audio files to MP3 format.
3) Backup: Any files, or entire computer.
4) Copy: disc to disc, disc to image (example: ISO), image to disc).
5) Edit: video and audio files
6) Supported operating system: Windows, Linux, Mac
7) Price: always free, including the source code.
I don't want to use C++ and Visual Basic
Which programming language (other than C++ and Visual Basic) is suitable to develop CD/DVD burning software?
Probably you would not write all this functionality but reuse libraries for the majority of it.
Due to this, you are more concentrating on the interface. You could look at c# but its mostly windows only (can work on Linux, but limited)
Java is portable and you can use c libraries, but the interfaces tend to suck a bit in my opinion on java apps.
You could use c/++ but I don't think you need to go to so much trouble when your main task is the interface.
Reply:There are a lot of languages that could do this but I think you are looking for a powerful language that easy and reduces the development cost. I think you should take a close look into C#.NET 2005 Express Edition. It's free and surprisingly powerful. You are working with audio and video, and this means you want your software to function at maximum speed. C# offers a number of unsafe execution techniques that could do the job. Beside that it is a .NET language, and this means easy development and debugging platform.
My computer c drive missing....help?
i installed partition magic to seperate my two drive to install linux andwindows vista ultimate. i had already ulitmate. but after insatlling partion magic it says your disk drive is an error, so i fixed it and after that i tried to uninstall it, when i tried to uninstall from control panel, computer shut down. and then i restarted it says windows cannot be loaded.
i tried to install fresh copy ultimate but it says ur c drive is formatted, and that time i dont have any partition magic software to do it, then i tried to restore dell restore from d drive which is only available. but after few seconds of formatting it crashed as well. so now i cannot access my system.
so guys i m in big trouble. i want to restore all my data which software i should buy/download? or is there any option to get those data ? and if any company does it near north london?
thnak you
My computer c drive missing....help?
As far as i think -FORGET your data.Now what you can do is run fdisk and make new partitions and format them or install xp directly and make new partitions and reformat.
Reply:i think unistall data recovery software may work in this case
Reply:Use your Vista install disk and delete your partition.
Don't go any further or format if you are going to keep Windows Xp.
Put the Xp disk in and let install a fresh copy.
If you want Vista, go ahead and put up a new partition, a large one, 80% of your total disk size at least and let it do it's thing.
i tried to install fresh copy ultimate but it says ur c drive is formatted, and that time i dont have any partition magic software to do it, then i tried to restore dell restore from d drive which is only available. but after few seconds of formatting it crashed as well. so now i cannot access my system.
so guys i m in big trouble. i want to restore all my data which software i should buy/download? or is there any option to get those data ? and if any company does it near north london?
thnak you
My computer c drive missing....help?
As far as i think -FORGET your data.Now what you can do is run fdisk and make new partitions and format them or install xp directly and make new partitions and reformat.
Reply:i think unistall data recovery software may work in this case
Reply:Use your Vista install disk and delete your partition.
Don't go any further or format if you are going to keep Windows Xp.
Put the Xp disk in and let install a fresh copy.
If you want Vista, go ahead and put up a new partition, a large one, 80% of your total disk size at least and let it do it's thing.
"lex.yy.c"?
In my college days I have written programs which has "lex.yy.c" in it.
Now i want that language again to do some programming
what was that?
how can i get that compiler again?
is there a site to download that?
will that compiler run in windows?
do i have to install linux for this?
I googled to get : http://en.wikipedia.org/wiki/Lex_program...
Sounds very familiar.
P.S: I studied this on 6th sem of my engineering.
Computer science engineering Cochin University, Kerala.
Help!
"lex.yy.c"?
Yes, this is Lex and it is Wonderful, but in the beginning a little bit difficult.
Lex is a Lexical Analyzer, you can use it in several way, one that should be most used, is to convert a sequence of characters in your C source code into a sequence of tokens.
In this way you can, for example, crate your custom Makefile that compile on different platform and different operating systems.
To use it you need to read and read and read and try it.
This http://gnuwin32.sourceforge.net/packages... you can use on Windows, but I advice you to follow the links inside the wiki that you posted to understand clearly what are Lexical Analizer and how to use them
rose
Now i want that language again to do some programming
what was that?
how can i get that compiler again?
is there a site to download that?
will that compiler run in windows?
do i have to install linux for this?
I googled to get : http://en.wikipedia.org/wiki/Lex_program...
Sounds very familiar.
P.S: I studied this on 6th sem of my engineering.
Computer science engineering Cochin University, Kerala.
Help!
"lex.yy.c"?
Yes, this is Lex and it is Wonderful, but in the beginning a little bit difficult.
Lex is a Lexical Analyzer, you can use it in several way, one that should be most used, is to convert a sequence of characters in your C source code into a sequence of tokens.
In this way you can, for example, crate your custom Makefile that compile on different platform and different operating systems.
To use it you need to read and read and read and try it.
This http://gnuwin32.sourceforge.net/packages... you can use on Windows, but I advice you to follow the links inside the wiki that you posted to understand clearly what are Lexical Analizer and how to use them
rose
Anyone know of a good c++ compiler?
hey i was thinking of getting back into c++ and i was wondering what a good compiler would be? and i know about linux compilers but i kinda dont have enough space for 2 operating systems again i only have 20 gigs and i want to keep it simple use it on windows anyone know of a good one thanks in advance =)
Anyone know of a good c++ compiler?
windows have a good one. This is the link http://msdn2.microsoft.com/en-us/visualc...
Anyone know of a good c++ compiler?
windows have a good one. This is the link http://msdn2.microsoft.com/en-us/visualc...
Subscribe to:
Posts (Atom)