Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3444728
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:57:08+00:00 2026-05-18T08:57:08+00:00

This is more focused than my previous Valgrind question; I am trying to narrow

  • 0

This is more focused than my previous Valgrind question; I am trying to narrow down write and read errors when parsing command-line options:

#include <stdio.h>                                                                                                                                                                                                                    
#include <stdlib.h>                                                                                                                                                                                                                   
#include <unistd.h>                                                                                                                                                                                                                   
#include <sys/utsname.h>                                                                                                                                                                                                              
#include <sys/stat.h>                                                                                                                                                                                                                 
#include <string.h>                                                                                                                                                                                                                   
#include <locale.h>                                                                                                                                                                                                                   
#include <bzlib.h>                                                                                                                                                                                                                    
#include <zlib.h>                                                                                                                                                                                                                     
#include "starch.h"

#define BUFMAXLEN 1024

int main(int argc, char **argv) { 
    if (parseCommandLineInputs( &argc, &argv ) != 0)
        exit(EXIT_FAILURE);

    return 0;
} 

int parseCommandLineInputs(int *argc, char ***argv) {

    pid_t pid;                                                                                                                                                                                                                        
    struct utsname uts;
    char uniqTag[BUFMAXLEN];

    if ((*argc == 1) || (*argc > 4)) {                                                                                                                                                                                                
        printUsage();
        return -1;                                                                                                                                                                                                           
    }                                                                                                                                                                                                                                  

    if ((pid = getpid()) < 0) {                                                                                                                                                                                                       
        fprintf(stderr, "\n\t[starch] - Error: Could not obtain process ID\n\n");                                                                                                                                                     
        return -1;                                                                                                                                                                                                           
    }                                                                                                                                                                                                                                 
    uname( &uts );                                                                                                                                                                                                                    
    sprintf(uniqTag, "pid%d.%s", pid, uts.nodename);                                                                                                                                                                                  

    switch (*argc) {                                                                                                                                                                                                                  
        case 2: {                                                                                                                                                                                                                     
            if (strcmp(*argv[1], "-") != 0) {                                                                                                                                                                                         
                if (fileExists(*argv[1]) != 0) { /* standard input */                                                                                                                                                              
                    ...
    }

    return 0;
}


int fileExists(char *fn) {
    struct stat buf;
    int i = stat (fn, &buf);
    if (i == 0)
        return 0;
    return -1;
}

void printUsage() {
    fprintf(stderr, "my usage statement\n\n");
}

My makefile is as follows:

CC      = gcc                                                                                                                                                                                                                         
CFLAGS  = -D_FILE_OFFSET_BITS=64 -D_LARGEFILE64_SOURCE=1 -DUSE_ZLIB -O3 -Wformat -Wall -pedantic -std=gnu99 -g                                                                                                                        
BIN     = ../bin                                                                                                                                                                                                                      

all: starch                                                                                                                                                                                                                           
    rm -rf *~                                                                                                                                                                                                                         

starch: starch.o                                                                                                                                                                                                                      
    mkdir -p $(BIN) && $(CC) ${CFLAGS} starch.o -lbz2 -lz -o ${BIN}/starch                                                                                                                                                            
    rm -rf *~                                                                                                                                                                                                                         

clean:
    rm -rf *.o *~ ${BIN}/starch

I get the following errors when I run with valgrind:

$ valgrind --tool=memcheck --leak-check=yes --show-reachable=yes --num-callers=20 --track-fds=yes -v ../bin/starch

...

==2675== 1 errors in context 1 of 2:                                                                                                                                                                                                  
==2675== Invalid read of size 8                                                                                                                                                                                                       
==2675==    at 0x3AB4262243: fwrite (in /lib64/libc-2.5.so)                                                                                                                                                                           
==2675==  Address 0x7fedffd68 is on thread 1's stack                                                                                                                                                                                  
==2675==                                                                                                                                                                                                                              
==2675==                                                                                                                                                                                                                              
==2675== 1 errors in context 2 of 2:                                                                                                                                                                                                  
==2675== Invalid write of size 8                                                                                                                                                                                                      
==2675==    at 0x401AA6: parseCommandLineInputs (starch.c:217)                                                                                                                                                                        
==2675==    by 0x7FF0000AF: ???                                                                                                                                                                                                       
==2675==    by 0x401DFA: main (starch.c:46)                                                                                                                                                                                           
==2675==  Address 0x7fedffd68 is on thread 1's stack 

The first error is not telling me anything I can use, since I am not using fwrite() anywhere.

The second error is tripped up on the fprintf statement in printUsage().

Line 46 is the following line:

 if (parseCommandLineInputs( &argc, &argv ) != 0)

Line 217 is the following line:

 fprintf(stderr, "my usage statement\n\n");

What is wrong with my application that explains why these errors appear?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T08:57:08+00:00Added an answer on May 18, 2026 at 8:57 am

    Two things that pop up to me right away:

    • *argv[1] is NOT the same as (*argv)[1] which is what you probably mean. Array subscripting has precedence over pointer dereferencing. This results in an invalid pointer. As many experienced programmers will tell you: “Don’t try to remember the precedence of operators – if in doubt use parentheses, if not just use them anyway”.

    • -O3 in the compiler flags is a big fat NO-NO when debugging. The compiler will mangle your code so much that it can make your life impossible. Variables can disappear completely and functions can go mysteriously away as they get inlined. If your code compiles, links and runs with -O0 (IIRC some code containing inline assembly needs -O1 with some (all?) GCC versions) use it, otherwise use -O1 at most.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.