pointer initialization of string constant
Dear All,
Plz see the following code. It gives segmentation fault. ofcourse, it's right as char *ptr = "abc" sets pointer to the address of the "abc" string (which may be stored in read-only memory and thus unchangeable). My doubt : when i run gdb and execute p ptr either after or before printf, pbc has been displayed. I'm getting segmentation fault only at the end, it seems, because when i run ./a.out "pbc is not displayed" but prompted segmentation fault error. why is it so? How does segmentation fault raises, though pointer-pointing contents has been changed & even displayed using gdb. Will U plz explain in detail (internals).
main()
{
char *ptr = "abc";
ptr[0] = 'p';
printf("%s\n",ptr);
}Thanx in advance.
Krishna Mohan
Quoted 5 lines · Krishna Mohan
main(){ char *ptr = "abc"; ptr[0] = 'p';
Since ptr points to read-only mem, changing ptr[0] does not seem to be a very
wise idea.
Dear progga
U are right. But my doubt is : when i run gdb and execute p ptr either just after or before printf, pbc has been displayed. I'm getting segmentation fault only at the end, it seems, because when i run ./a.out "pbc is not displayed" but prompted segmentation fault error. why is it so? How does segmentation fault raises, though pointer-pointing contents has been changed & even displayed using gdb. i am interested to know the internals.
Thanq for ur interest.
Regards
Krishna Mohan
On Sun, 2005-01-23 at 18:15, Progga wrote:
On Sun, Jan 23, 2005 at 11:56:11AM +0530, Krishna Mohan wrote:
>
> main()
> {
> char *ptr = "abc";
> ptr[0] = 'p';
Since ptr points to read-only mem, changing ptr[0] does not seem to be a very
wise idea.how to implement tail -n
Hi,
I would like to know how to implement "tail -n" in C.
The "-n" option will be used to print the last n lines of a file - rather than the default option of printing the last 10 lines of a file.
I wish to do this in a single pass. I can use lseek() to go to the end of the file. How to traverse backwards from there ? Is there any function that does this ?
thanks,
venkatesh
Re: how to implement tail -n
Quoted 10 lines · you
Hi,
I would like to know how to implement "tail -n" in C.
The "-n" option will be used to print the last n lines of a file - rather than the default option of printing the last 10 lines of a file.
I wish to do this in a single pass. I can use
lseek()to go to the end of the file. How to traverse backwards from there ? Is there any function that does this ?
Not that I know of. There is no GOOD way to do this in a single pass since the lines can be of varying length. If by single pass you meant you want performace, then dont read a char at a time, rather just seek to the end of the file, read in about 4K( a reasonable buffer) or so and see if you can find (n+1) newlines. If you dont, read in 4 more k and keep going backwards till you find them.
P.S. Please dont hi-jack subjects. Type in a new message instead of replying to something and deleting the original message; it screws with threading.
Quoted 6 lines
thanks,
venkatesh
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Signature · 2 lines
---------------------------------------- --EB
Quoted 4 lines
All is fine except that I can reliably "oops" it simply by trying to read from
/proc/apm(e.g. cat/proc/apm).
oops output and ksymoops-2.3.4 output is attached.
Is there anything else I can contribute?
The latitude and longtitude of the bios writers current position, and a ballistic missile.
--Alan Cox LKML-December 08,2000
----------------------------------------
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: how to implement tail -n
In reply to Venkatesh JoshiQuoted 1 line · Venkatesh Joshi
I would like to know how to implement "tail -n" in C.
Use a circular buffer which contains the last N lines read. Each time you read a line, remove the first line and add the new line to the end of the buffer. When you reach the end, display the contents of the buffer.
E.g.
void tail(FILE *fp, int num_lines)
{
char **buffer = calloc(num_lines, sizeof(char *));
int index = 0;
int i;
for (;;)
{
char *line = NULL;
int len = 0;
if (getline(&line, &len, fp) < 0)
break;
if (buffer[index])
free(buffer[index]);
buffer[index] = line;
index++;
index %= num_lines;
}
for (i = 0; i < num_lines; i++)
puts(buffer[(index + i) % num_lines]);
}In practice, you would probably want to do your own memory allocation rather than using getline(). You also need to allow for files with less than num_lines lines..
Quoted 6 lines
The "-n" option will be used to print the last n lines of a file - rather than the default option of printing the last 10 lines of a file.
I wish to do this in a single pass. I can use
lseek()to go to the end of the file. How to traverse backwards from there ? Is there any function that does this ?
Use lseek(SEEK_CUR) with a negative offset to seek backwards. You would have to use trial and error to determine how much you need to read.
Also, lseek() will only work on regular files. It won't work on pipes, etc. The "tail" command works on pipes.
Signature · 1 line
Glynn Clements <[email protected]>
Re: how to implement tail -n
In reply to Eric BambachQuoted 16 lines · Eric Bambach
Quoted 12 lines · you
Hi,
I would like to know how to implement "tail -n" in C.
The "-n" option will be used to print the last n lines of a file - rather than the default option of printing the last 10 lines of a file.
I wish to do this in a single pass. I can use
lseek()to go to the end of the file. How to traverse backwards from there ? Is there any function that does this ?
Quoted 18 lines
Not that I know of. There is no GOOD way to do this in a single pass since the lines can be of varying length. If by single pass you meant you want performace, then dont read a char at a time, rather just seek to the end of the file, read in about 4K( a reasonable buffer) or so and see if you can find (n+1) newlines. If you dont, read in 4 more k and keep going backwards till you find them.
P.S. Please dont hi-jack subjects. Type in a new message instead of replying to something and deleting the original message; it screws with threading.Quoted 8 lines
thanks,
venkatesh
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
What Eric describes is exactly how it IS implemented in GNU tail. To see this implementation you can look at the source which can be found in this tarball -- http://ftp.gnu.org/gnu/coreutils/coreutils-5.2.0.tar.gz (src/tail.c)
Sometimes looking at source is enlightening, sometimes it is not. In this particular case the code is extremely well commented and clear, so you should be able to get some good info fast. The function you're interested in is 'file_lines(...'
Robert