How to read stream from STDIN or Unix pipe in C - c

Forgive my ignorance, I'm brand new to C. As a learning experience, I'm trying to read in an image that's been cat'ed and piped to a simple C script. I'd like to iterate over the octets, and at least for now, just print them to STDOUT.
Example: cat some-image.png | ./my-c-program
Code:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
FILE *input;
input = fopen(argv[1], "r");
int c;
long retvalue = 0;
while (EOF != (c = fgetc(input))) {
retvalue++;
printf("%d", c);
}
printf("length: %ld", retvalue);
return 0;
}
Currently, I'm getting a lot of SegFault 11
I'm not too sure how to make sense of this, but Valgrind tells me:
==90834== Memcheck, a memory error detector
==90834== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==90834== Using Valgrind-3.11.0.SVN and LibVEX; rerun with -h for copyright info
==90834== Command: ./a.out
==90834==
--90834-- ./a.out:
--90834-- dSYM directory is missing; consider using --dsymutil=yes
==90834== Syscall param open(filename) points to unaddressable byte(s)
==90834== at 0x1002FA012: open$NOCANCEL (in /usr/lib/system/libsystem_kernel.dylib)
==90834== by 0x1001EE4B6: fopen (in /usr/lib/system/libsystem_c.dylib)
==90834== by 0x100000EDC: main (in ./a.out)
==90834== Address 0x0 is not stack'd, malloc'd or (recently) free'd
==90834==
==90834== Invalid read of size 8
==90834== at 0x1001ECE07: flockfile (in /usr/lib/system/libsystem_c.dylib)
==90834== by 0x1001ED52E: fgetc (in /usr/lib/system/libsystem_c.dylib)
==90834== by 0x100000EF1: main (in ./a.out)
==90834== Address 0x68 is not stack'd, malloc'd or (recently) free'd
==90834==
==90834==
==90834== Process terminating with default action of signal 11 (SIGSEGV)
==90834== Access not within mapped region at address 0x68
==90834== at 0x1001ECE07: flockfile (in /usr/lib/system/libsystem_c.dylib)
==90834== by 0x1001ED52E: fgetc (in /usr/lib/system/libsystem_c.dylib)
==90834== by 0x100000EF1: main (in ./a.out)
==90834== If you believe this happened as a result of a stack
==90834== overflow in your program's main thread (unlikely but
==90834== possible), you can try to increase the size of the
==90834== main thread stack using the --main-stacksize= flag.
==90834== The main thread stack size used in this run was 8388608.
==90834==
==90834== HEAP SUMMARY:
==90834== in use at exit: 34,916 bytes in 425 blocks
==90834== total heap usage: 505 allocs, 80 frees, 41,044 bytes allocated
==90834==
==90834== LEAK SUMMARY:
==90834== definitely lost: 0 bytes in 0 blocks
==90834== indirectly lost: 0 bytes in 0 blocks
==90834== possibly lost: 0 bytes in 0 blocks
==90834== still reachable: 0 bytes in 0 blocks
==90834== suppressed: 34,916 bytes in 425 blocks
==90834==
==90834== For counts of detected and suppressed errors, rerun with: -v
==90834== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
Segmentation fault: 11
Any help or guidance is appreciated.

If you don't provide an argument to ./my-c-program then argv[1] is NULL and you get a segfault.
To read from standard input using the Standard C I/O functions, use one or more of
getchar(), fgetc(stdin), scanf(), fread(..., stdin), fgets(..., stdin)
You do not need to open stdin as the C runtime startup code does that for you. If you need to read from files supplied by arguments, you need to use fopen() first to create a new input stream.

Related

Experiencing segmentation fault when creating functions to deal with initialization and cleaning of mpz_t array

I using GNU MP to great an array of multi-precision integers. If I initialize, use and clean, all the main function, it works perfect, with no error. I've tried to create auxiliary functions to manage repetitive jobs: initialize an array and clean an array. Unfortunately it did not end up well, I'm getting a memory leak. I made a small example bellow reproducing the error behaviour:
#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>
mpz_t *start(int n)
{
mpz_t *c = malloc(n*sizeof(mpz_t));
for (int i=0; i<n; i++) mpz_init (c[i]);
for (int i=0; i<n; i++) mpz_set_ui (c[i], 1);
}
void finish(mpz_t *x, int n)
{
for (int i=0; i<n; i++) mpz_clear (x[i]);
free(x);
}
int main(int argc, char *argv[])
{
int n = 10;
mpz_t *c = start(n);
finish(c, n);
return 0;
}
The result of running the program is:
Segmentation fault (core dumped)
Bellow follows Valgrind analysis log:
==18807== Memcheck, a memory error detector
==18807== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==18807== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==18807== Command: ./test
==18807==
==18807== Invalid read of size 4
==18807== at 0x4887C60: __gmpz_clear (in /usr/lib/x86_64-linux-gnu/libgmp.so.10.3.2)
==18807== by 0x10928B: finish (test.c:14)
==18807== by 0x1092DF: main (test.c:22)
==18807== Address 0xa is not stack'd, malloc'd or (recently) free'd
==18807==
==18807==
==18807== Process terminating with default action of signal 11 (SIGSEGV)
==18807== Access not within mapped region at address 0xA
==18807== at 0x4887C60: __gmpz_clear (in /usr/lib/x86_64-linux-gnu/libgmp.so.10.3.2)
==18807== by 0x10928B: finish (test.c:14)
==18807== by 0x1092DF: main (test.c:22)
==18807== If you believe this happened as a result of a stack
==18807== overflow in your program's main thread (unlikely but
==18807== possible), you can try to increase the size of the
==18807== main thread stack using the --main-stacksize= flag.
==18807== The main thread stack size used in this run was 8388608.
==18807==
==18807== HEAP SUMMARY:
==18807== in use at exit: 240 bytes in 11 blocks
==18807== total heap usage: 11 allocs, 0 frees, 240 bytes allocated
==18807==
==18807== 240 (160 direct, 80 indirect) bytes in 1 blocks are definitely lost in loss record 2 of 2
==18807== at 0x483A7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==18807== by 0x1091E8: start (test.c:7)
==18807== by 0x1092CA: main (test.c:21)
==18807==
==18807== LEAK SUMMARY:
==18807== definitely lost: 160 bytes in 1 blocks
==18807== indirectly lost: 80 bytes in 10 blocks
==18807== possibly lost: 0 bytes in 0 blocks
==18807== still reachable: 0 bytes in 0 blocks
==18807== suppressed: 0 bytes in 0 blocks
==18807==
==18807== For lists of detected and suppressed errors, rerun with: -s
==18807== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
Segmentation fault (core dumped)
What is wrong? How to fix it?
Flowing off the end of a value-returning function (except main) without a return statement is undefined behavior.
In your example, mpz_t *start(int n) doesn't actually return anything, which leads to UB.

Valgrind is telling me I have error and possible memory leak in this simple C program [duplicate]

This question already has answers here:
Valgrind reports errors for a very simple C program
(3 answers)
Closed 7 years ago.
The challenge is to clear all Valgrind errors before going on to the next lesson, but I don't really know what's wrong with this program. I want to know what's wrong before going on. Thanks, appreciate the help.
#include <stdio.h>
int main()
{
int age = 10;
int height = 72;
printf("I am %d years old.\n", age);
printf("I am %d inches tall.\n", height);
return 0;
}
Here's the valgrind report:
==41714== Memcheck, a memory error detector
==41714== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==41714== Using Valgrind-3.11.0.SVN and LibVEX; rerun with -h for copyright info
==41714== Command: ./ex3
==41714==
==41714== Conditional jump or move depends on uninitialised value(s)
==41714== at 0x1003FBC3F: _platform_memchr$VARIANT$Haswell (in /usr/lib/system/libsystem_platform.dylib)
==41714== by 0x1001EFBB6: __sfvwrite (in /usr/lib/system/libsystem_c.dylib)
==41714== by 0x1001FA005: __vfprintf (in /usr/lib/system/libsystem_c.dylib)
==41714== by 0x10021F9CE: __v2printf (in /usr/lib/system/libsystem_c.dylib)
==41714== by 0x10021FCA0: __xvprintf (in /usr/lib/system/libsystem_c.dylib)
==41714== by 0x1001F5B91: vfprintf_l (in /usr/lib/system/libsystem_c.dylib)
==41714== by 0x1001F39F7: printf (in /usr/lib/system/libsystem_c.dylib)
==41714== by 0x100000F4D: main (ex3.c:8)
==41714==
I am 10 years old.
I am 72 inches tall.
==41714==
==41714== HEAP SUMMARY:
==41714== in use at exit: 38,816 bytes in 426 blocks
==41714== total heap usage: 507 allocs, 81 frees, 44,960 bytes allocated
==41714==
==41714== LEAK SUMMARY:
==41714== definitely lost: 0 bytes in 0 blocks
==41714== indirectly lost: 0 bytes in 0 blocks
==41714== possibly lost: 0 bytes in 0 blocks
==41714== still reachable: 4,096 bytes in 1 blocks
==41714== suppressed: 34,720 bytes in 425 blocks
==41714== Rerun with --leak-check=full to see details of leaked memory
==41714==
==41714== For counts of detected and suppressed errors, rerun with: -v
==41714== Use --track-origins=yes to see where uninitialised values come from
==41714== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
There are very clearly no memory issues. You never even malloc or free anything, so at this point all memory is handled without you being involved. This must be an issue with the Valgrind. If you are using OSX perhaps try on a machine with linux if you have one available.

memory errors while reading a large file in C

I am very novice in working with malloc/realloc in C and so need some help. The following is short snippet of a big program where I intend to read a big (fasta) file almost 80000 line numbers and store the first line marked with > (name) and following line (sequence) in two separate arrays- fasta_name and fasta_seq. I have used a standard library which gives the output as a structure but I need to change it into arrays since later on the program I need to check input provided by the user with each of the fasta entries in the file..
The inputfile format is:
P21306\n
MSAWRKAGISYAAYLNVAAQAIRSSLKTELQTASVLNRSQTDAFYTQYKNGTAASEPTPITK\n
P38077\n
MLSRIVSNNATRSVMCHQAQVGILYKTNPVRTYATLKEVEMRLKSIKNIEKITKTMKIVASTRLSKAEKA\n
=======================
The code is:
KSEQ_INIT(gzFile, gzread) ///EXTERNAL LIBRARY FOR BIOLOGISTS READING FASTA FORMAT FILE///
int main(int argc,char *argv[])
{
char **fasta_name=(char **)malloc(sizeof(char *)*80000);
for(i=0;i<size;i++)
{
fasta_name[i]=(char*)malloc(sizeof(char)*50);
}
char **fasta_seq=(char**)malloc(sizeof(char *)*80000);
for(i=0;i<size;i++)
{
fasta_seq[i]=(char*)malloc(sizeof(char)*5000);
}
fpf = gzopen("fasta_seq_nr_uniprot.txt", "r");
seq = kseq_init(fpf);
while((l = kseq_read(seq)) >= 0)
{
strcpy(fasta_name[index1],seq->name.s);
strcpy(fasta_seq[index1],seq->seq.s);
index1++;
}
kseq_destroy(seq);
gzclose(fpf);
for(i=0;i<size;i++)
{
free(fasta_name[i]);
}
for(i=0;i<size;i++)
{
free(fasta_seq[i]);
}
free(fasta_name);
free(fasta_seq);
The program shows no compilation errors but the following memory errors and segmentation fault using Valgrind.
$ valgrind --track-origins=yes --leak-check=full ./Gwidd_uniprot_map2 xaa==3511== Memcheck, a memory error detector
==3511== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==3511== Using Valgrind-3.6.1 and LibVEX; rerun with -h for copyright info
==3511== Command: ./map2 xaa
==3511==
--3511-- ./map2:
--3511-- dSYM directory has wrong UUID; consider using --dsymutil=yes
Opening file xaa
==3511== Use of uninitialised value of size 8
==3511== at 0x100012C43: __strcpy_chk (mc_replace_strmem.c:893)
==3511== by 0x100001A78: __inline_strcpy_chk (in ./map2)
==3511== by 0x10000183E: main (in ./map2)
==3511== Uninitialised value was created by a heap allocation
==3511== at 0x100011345: malloc (vg_replace_malloc.c:236)
==3511== by 0x10000170C: main (in ./map2)
==3511==
==3511== Invalid write of size 1
==3511== at 0x100012C43: __strcpy_chk (mc_replace_strmem.c:893)
==3511== by 0x100001A78: __inline_strcpy_chk (in ./map2)
==3511== by 0x10000183E: main (in ./map2)
==3511== Address 0x0 is not stack'd, malloc'd or (recently) free'd
==3511==
==3511==
==3511== Process terminating with default action of signal 11 (SIGSEGV)
==3511== Access not within mapped region at address 0x0
==3511== at 0x100012C43: __strcpy_chk (mc_replace_strmem.c:893)
==3511== by 0x100001A78: __inline_strcpy_chk (in ./map2)
==3511== by 0x10000183E: main (in ./map2)
==3511== If you believe this happened as a result of a stack
==3511== overflow in your program's main thread (unlikely but
==3511== possible), you can try to increase the size of the
==3511== main thread stack using the --main-stacksize= flag.
==3511== The main thread stack size used in this run was 8388608.
==3511==
==3511== HEAP SUMMARY:
==3511== in use at exit: 6,674,813 bytes in 3,664 blocks
==3511== total heap usage: 3,807 allocs, 143 frees, 6,698,108 bytes allocated
==3511==
==3511== LEAK SUMMARY:
==3511== definitely lost: 0 bytes in 0 blocks
==3511== indirectly lost: 0 bytes in 0 blocks
==3511== possibly lost: 0 bytes in 0 blocks
==3511== still reachable: 6,674,813 bytes in 3,664 blocks
==3511== suppressed: 0 bytes in 0 blocks
==3511== Reachable blocks (those to which a pointer was found) are not shown.
==3511== To see them, rerun with: --leak-check=full --show-reachable=yes
==3511==
==3511== For counts of detected and suppressed errors, rerun with: -v
==3511== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
Segmentation fault
CODE EDITED AS:
char **fasta_name=(char **)malloc(sizeof(char *)*80000);
if(fasta_name == NULL)
{
printf("Unable to allocate memory.\n");
exit(EXIT_FAILURE);
}
for(i=0;i<size;i++)
{
fasta_name[i]=(char*)malloc(sizeof(char)*50);
if(fasta_name[i] == NULL)
{
printf("Unable to allocate memory.\n");
exit(EXIT_FAILURE);
}
}
char **fasta_seq=(char**)malloc(sizeof(char *)*80000);
if(fasta_seq == NULL)
{
printf("Unable to allocate memory.\n");
exit(EXIT_FAILURE);
}
for(i=0;i<size;i++)
{
fasta_seq[i]=(char*)malloc(sizeof(char)*5000);
if(fasta_seq[i] == NULL)
{
printf("Unable to allocate memory.\n");
exit(EXIT_FAILURE);
}
}
fpf = gzopen("fasta_seq_nr_uniprot.txt", "r");
seq = kseq_init(fpf);
while((l = kseq_read(seq)) >= 0)
{
strcpy(fasta_name[index1],seq->name.s);
strcpy(fasta_seq[index1],seq->seq.s);
index1++;
}
kseq_destroy(seq);
gzclose(fpf);
Based on the error Access not within mapped region at address, I would say that you are accessing memory that's out of bounds, causing your segmentation fault. Check that malloc() doesn't return NULL to you after calling it, otherwise you risk accessing memory you don't actually own.
Do this after every single malloc() call. For example:
int *i = (int *) malloc(sizeof(int));
if(!i){
fprintf(stderr, "Something went wrong with malloc(), exiting...\n");
exit(1);
}
Another thing to check is to see if index1 is out of bounds of your allocations. I don't see where it's declared or initialized. Is it possible that your allocation for fasta_name and others is too small, given that you read information using a while loop that keeps incrementing the index?

Perhaps a memory issue when I reinitialize an array? [duplicate]

I am running breadth first search and bellman ford algorithms on large sets of graph data in the format (u, v, weight) from an input file.
I initialize in the breadth first search, that all vertex's should be marked 0 for unvisited.
Later in the program, since I am calling BFS after every time I add an edge, rather than at the end of the program all together (it's part of a research project on bellman ford and BFS even though it doesn't quite make sense) I reinitialize the vertex array to unvisited.
However, I am getting a segmentation fault when I run larger sets it seems when I reinitialize the vertex array. I make the assumption about larger sets because I have a few smaller sets of test data going from 8 vertices, to 10, then at 100 and larger it fails.
Here is how I initialize at the very beginning of the program:
for(i=0;i<numberVertices;i++)
{
vertexArray[i].v = i+1;
vertexArray[i].adj = NULL;
vertexArray[i].marked = 0;
if(i==0)
{
vertexArray[i].weight = 0;
}
else{
vertexArray[i].weight = 10000;
}
}
And here is how I reinitialize at the end of a while loop set to break when it reaches the end of the file directly after BFS finishes:
BFS(q, u)
for(i=0;i<numberVertices;i++)
{
vertexArray[i].marked = 0;
}
Like I said it seems to work for smaller data sets, but I do not understand why when I reinitialize it seems to seg fault.
Please let me know your thoughts and suggestions!
Valgrind Output On An Example Case:
==6634== Memcheck, a memory error detector
==6634== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==6634== Using Valgrind-3.6.0 and LibVEX; rerun with -h for copyright info
==6634== Command: ./a.out 100S.txt
==6634==
==6634== Conditional jump or move depends on uninitialised value(s)
==6634== at 0x400C13: checkonqueue (bfs.c:160)
==6634== by 0x400B7F: BFS (bfs.c:142)
==6634== by 0x40094A: main (bfs.c:103)
==6634==
==6634== Invalid write of size 4
==6634== at 0x40096E: main (bfs.c:107)
==6634== Address 0x4d00000041 is not stack'd, malloc'd or (recently) free'd
==6634==
==6634==
==6634== Process terminating with default action of signal 11 (SIGSEGV)
==6634== Access not within mapped region at address 0x4D00000041
==6634== at 0x40096E: main (bfs.c:107)
==6634== If you believe this happened as a result of a stack
==6634== overflow in your program's main thread (unlikely but
==6634== possible), you can try to increase the size of the
==6634== main thread stack using the --main-stacksize= flag.
==6634== The main thread stack size used in this run was 10485760.
==6634==
==6634== HEAP SUMMARY:
==6634== in use at exit: 3,448 bytes in 181 blocks
==6634== total heap usage: 181 allocs, 0 frees, 3,448 bytes allocated
==6634==
==6634== LEAK SUMMARY:
==6634== definitely lost: 0 bytes in 0 blocks
==6634== indirectly lost: 0 bytes in 0 blocks
==6634== possibly lost: 0 bytes in 0 blocks
==6634== still reachable: 3,448 bytes in 181 blocks
==6634== suppressed: 0 bytes in 0 blocks
==6634== Reachable blocks (those to which a pointer was found) are not shown.
==6634== To see them, rerun with: --leak-check=full --show-reachable=yes
==6634==
==6634== For counts of detected and suppressed errors, rerun with: -v
==6634== Use --track-origins=yes to see where uninitialised values come from
==6634== ERROR SUMMARY: 16120 errors from 2 contexts (suppressed: 6 from 6)
Segmentation fault (core dumped)
check on queue function:
int checkonqueue(int * q, int value)
{
int i = 0;
for(i=0;i < max;i++)
{
if(q[i] == value)
{
return 1;
}
}
return 0;
}
line 160 is the line of the if condition

Reinitializing an Array is creating a segfault?

I am running breadth first search and bellman ford algorithms on large sets of graph data in the format (u, v, weight) from an input file.
I initialize in the breadth first search, that all vertex's should be marked 0 for unvisited.
Later in the program, since I am calling BFS after every time I add an edge, rather than at the end of the program all together (it's part of a research project on bellman ford and BFS even though it doesn't quite make sense) I reinitialize the vertex array to unvisited.
However, I am getting a segmentation fault when I run larger sets it seems when I reinitialize the vertex array. I make the assumption about larger sets because I have a few smaller sets of test data going from 8 vertices, to 10, then at 100 and larger it fails.
Here is how I initialize at the very beginning of the program:
for(i=0;i<numberVertices;i++)
{
vertexArray[i].v = i+1;
vertexArray[i].adj = NULL;
vertexArray[i].marked = 0;
if(i==0)
{
vertexArray[i].weight = 0;
}
else{
vertexArray[i].weight = 10000;
}
}
And here is how I reinitialize at the end of a while loop set to break when it reaches the end of the file directly after BFS finishes:
BFS(q, u)
for(i=0;i<numberVertices;i++)
{
vertexArray[i].marked = 0;
}
Like I said it seems to work for smaller data sets, but I do not understand why when I reinitialize it seems to seg fault.
Please let me know your thoughts and suggestions!
Valgrind Output On An Example Case:
==6634== Memcheck, a memory error detector
==6634== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==6634== Using Valgrind-3.6.0 and LibVEX; rerun with -h for copyright info
==6634== Command: ./a.out 100S.txt
==6634==
==6634== Conditional jump or move depends on uninitialised value(s)
==6634== at 0x400C13: checkonqueue (bfs.c:160)
==6634== by 0x400B7F: BFS (bfs.c:142)
==6634== by 0x40094A: main (bfs.c:103)
==6634==
==6634== Invalid write of size 4
==6634== at 0x40096E: main (bfs.c:107)
==6634== Address 0x4d00000041 is not stack'd, malloc'd or (recently) free'd
==6634==
==6634==
==6634== Process terminating with default action of signal 11 (SIGSEGV)
==6634== Access not within mapped region at address 0x4D00000041
==6634== at 0x40096E: main (bfs.c:107)
==6634== If you believe this happened as a result of a stack
==6634== overflow in your program's main thread (unlikely but
==6634== possible), you can try to increase the size of the
==6634== main thread stack using the --main-stacksize= flag.
==6634== The main thread stack size used in this run was 10485760.
==6634==
==6634== HEAP SUMMARY:
==6634== in use at exit: 3,448 bytes in 181 blocks
==6634== total heap usage: 181 allocs, 0 frees, 3,448 bytes allocated
==6634==
==6634== LEAK SUMMARY:
==6634== definitely lost: 0 bytes in 0 blocks
==6634== indirectly lost: 0 bytes in 0 blocks
==6634== possibly lost: 0 bytes in 0 blocks
==6634== still reachable: 3,448 bytes in 181 blocks
==6634== suppressed: 0 bytes in 0 blocks
==6634== Reachable blocks (those to which a pointer was found) are not shown.
==6634== To see them, rerun with: --leak-check=full --show-reachable=yes
==6634==
==6634== For counts of detected and suppressed errors, rerun with: -v
==6634== Use --track-origins=yes to see where uninitialised values come from
==6634== ERROR SUMMARY: 16120 errors from 2 contexts (suppressed: 6 from 6)
Segmentation fault (core dumped)
check on queue function:
int checkonqueue(int * q, int value)
{
int i = 0;
for(i=0;i < max;i++)
{
if(q[i] == value)
{
return 1;
}
}
return 0;
}
line 160 is the line of the if condition

Resources