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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T15:39:55+00:00 2026-05-21T15:39:55+00:00

Valgrind detect an invalid write of size 1 in this piece of code. In

  • 0

Valgrind detect an invalid write of size 1 in this piece of code.
In this, I read a file where the first line is something I don’t need, and the following lines define 3 strings and 1 int (total_space) that I need to put in this struct:

typedef struct 
{
    char username[40];   
    char password[40];   
    char token[40];     
    pid_t logged_pid;    
    int  total_space;    
    int  used_space;    
} User;

The file is this (each word on a new line, sorry but I still didn’t understand how to format text and code):

pass 
username1 
password1 
token1delczzzzozoc 
4500000 
username2 
pasword2222 
token2efwerfg 
trg 
1000000

Here is the code: valgrind yells only in the first 4 lines! And in the first one on the character “e”: what’s wrong with it?

User *user = NULL;
int n = 0;
int k = 0;
char input;
FILE *file;            

if(!(file = fopen(USERS, "r")))    logMmboxd("opening USERS failed\n", 1);  
else                               logMmboxd("opened USERS\n", 0);  

/* file pointer at the second line, since the first has nothing i need now */
while((input = fgetc(file)) != EOF && input != '\n')   {}

/* read 4 lines every loop from the second line to the EOF */
while((input = fgetc(file)) != EOF)   
{  
    /* rewind the pointer to the previous character (the one I read to see if the file ended) */
    if(fseek(file, -1, SEEK_CUR) == -1)   logMmboxd("failed seeking USERS\n", 1);

    /* expand the array of 1 user */
    users = realloc(users, n+1);
    n++;    

    for(k=0; (input=fgetc(file)) != '\n' && input != EOF; k++)   users[n-1].username[k] = input;
    users[n-1].username[k+1] = '\0';    

    for(k=0; (input=fgetc(file)) != '\n' && input != EOF; k++)   users[n-1].password[k] = input;
    users[n-1].password[k+1] = '\0';    

    for(k=0; (input=fgetc(file)) != '\n' && input != EOF; k++)   users[n-1].token[k] = input;
    users[n-1].token[k+1] = '\0';   

    users[n-1].logged_pid = 0;

    for(k=0; (input=fgetc(file)) != '\n' && input != EOF; k++)   line[k] = input;
    line[k+1] = '\0';   
    users[n-1].total_space = atoi(line);    

    users[n-1].used_space = usedSpace(users[n-1].username);
}
  • 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-21T15:39:56+00:00Added an answer on May 21, 2026 at 3:39 pm

    This code:

    /* expand the array of 1 user */
    users = realloc(users, n+1);
    

    Expands users by one byte, not one User.

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

Sidebar

Related Questions

Valgrind reports error Invalid read of size 8 in the following code. I have
valgrind report the invalid write of size 8 at 0x4007A0:ArrayCstringPush by 0x4008F8:main Address 0x4A0A450
For some reason valgrind keeps throwing the following error: ==6263== Invalid read of size
Valgrind detects an invalid read error I don't know how to fix or to
Valgrind is complaining about some of my code, but this code is pretty much
valgrind is reporting uninitialized memory errors from code like this: unsigned char buf[100]; struct
I'm trying to understand why Valgrind is spitting out : ==3409== Invalid read of
When I run valgrind on the following (example) code it reports an Invalid free()
On OSX Valgrind reports this memory leak, Where is it coming from? The code
Valgrind throws me out this error: ==11204== Syscall param write(buf) points to uninitialised byte(s)

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.