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 9309079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T00:46:50+00:00 2026-06-19T00:46:50+00:00

When valgrind runs this program (implementing a simple type of data compression), it reports

  • 0

When valgrind runs this program (implementing a simple type of data compression), it reports “Conditional jump depends on uninitialized value.” It also occasionally segfaults. Problem is, I can’t seem to pinpoint which variable is not initialized. My CS professor mentioned that “your code doesn’t make sure that the strings are properly null-terminated” but I can’t see where.

https://gist.github.com/anonymous/9f6c87fb33985606a297

(edit – added the actual code here:)


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define WORDLIM 128
#define REPEAT for(;;)
struct word {
    char *cont;
    char *wsp;  
    int ctr;
}; 

char peekchar(void)
{
    char c;
    c = getchar();
    if(c != EOF) 
        ungetc(c, stdin);

    /* puts it back */    
    return c;
} 

struct word *getword()
{
    char cont[WORDLIM];
    char wsp[WORDLIM];
    cont[0] = '\0';
    wsp[0] = '\0';
    if (peekchar() == EOF) 
    {
        return NULL;
    }

    REPEAT{
        char c = getchar();
        char buf[2];
        buf[0]=c;
        buf[1]='\0';
        if (c == '\n' || c == ' ' || c == EOF)
        {
            if (c != EOF)
                strcat(wsp, buf);
            if (peekchar() != '\n' && peekchar() != ' ')
            {
                struct word *toret;
                toret = malloc(sizeof(struct word));
                toret->cont = malloc(strlen(cont) + 1);
                strcpy(toret->cont, cont);
                toret->wsp = malloc(strlen(wsp) + 1);
                strcpy(toret->wsp, wsp);
                toret->ctr = -1;
                return toret;
            }
            continue;
        }
        else {
            strcat(cont, buf);
            continue;
        }
    }
    printf("PANIC PANIC PANIC THIS IS GOING WROOOOONG!!!!!\n");
}

void numbrify(struct word **wordlist)
{
    int oc = 0;
    int roc = oc;
    struct word *w;
    while ((w = wordlist[oc]) != NULL){
        int ic;
        if (w->ctr == -1){
            for (ic = oc + 1; wordlist[ic] != NULL; ic++){
                if (!strcmp(wordlist[ic]->cont, w->cont)){
                    //printf("**found match between %s and %s**\n", wordlist[ic]->cont, w->cont);
                    wordlist[ic]->ctr = roc;
                }
            }
            if (w->cont[0]!='\0')
        roc++;
        }
        oc++;
    }
} 

int main(void){
    struct word *wlist[4096];
    int i = 0;
    struct word *w;
    for (i = 0; (w = getword()) != NULL; i++){
        wlist[i]=w;
    }

    wlist[i+1] = NULL;
    numbrify(wlist);
    i = 0;
    for (i = 0; wlist[i]!=NULL; i++){
        if (wlist[i]->ctr == -1) 
            printf("%s%s", wlist[i]->cont, wlist[i]->wsp);
        else 
            printf("%d%s", wlist[i]->ctr, wlist[i]->wsp);
            //printf("'%s'\t'%s'\t%d\n", wlist[i]->cont, wlist[i]->wsp, wlist[i]->ctr);
    }

    return 0;
}
  • 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-06-19T00:46:51+00:00Added an answer on June 19, 2026 at 12:46 am

    At line 85 of your program:

    wlist[i+1] = NULL;
    

    i already points to an unused entry in the array, so assigning NULL to wlist[i+1] leaves an undefined value before the end.

    I determined this by running the Clang static analyzer*, which identified this issue in the first two diagnostics (as well as an unrelated, harmless error as the third one):

    14968829.c:62:12: warning: Assigned value is garbage or undefined
            while ((w = wordlist[oc]) != NULL){
                      ^ ~~~~~~~~~~~~
    14968829.c:65:35: warning: The left operand of '!=' is a garbage value
                            for (ic = oc + 1; wordlist[ic] != NULL; ic++){
                                              ~~~~~~~~~~~~ ^
    14968829.c:87:2: warning: Value stored to 'i' is never read
            i = 0;
            ^   ~
    

    Correcting the issue (by changing wlist[i+1] to wlist[i]) caused the two Clang diagnostics to disappear, as well as the Valgrind errors.

    *: Command line: clang --analyze -Wall 14968829.c -o 14968829

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

Sidebar

Related Questions

I wrote this code. It runs OK, but when I check it under Valgrind
valgrind is reporting uninitialized memory errors from code like this: unsigned char buf[100]; struct
Valgrind reports a memory leak when assigning a value to a string. I used
Valgrind reports a SQLite error: ==11614== Jump to the invalid address stated on the
I am working on this old c++ program that runs in Linux. It is
If you've used Memcheck (from Valgrind) you'll probably be familiar with this message... Conditional
I am using valgrind on a program which runs an infinite loop. As memcheck
Valgrind is complaining about some of my code, but this code is pretty much
I'm running Valgrind and I'm getting the following error (this is not the only
I get my final done message with valgrind, and get this exit report: ==3434==

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.