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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:39:37+00:00 2026-05-29T07:39:37+00:00

I’m experimenting with C structs and I’ve come up with a invalid write of

  • 0

I’m experimenting with C structs and I’ve come up with a invalid write of size 8 followed by invalid read of size 8 messages from valgrind.

My code is only looping through arguments (if argc > 1) and for each filename, it scans for a string and unsigned int indicating name and age(struct player).

This is all the code I’ve got so far:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct player {
  char name[20];
  unsigned int age;
};

struct player *player_new_from_stream(FILE * stream){
  struct player *new_player = (struct player*) malloc(sizeof(struct player));
  char *p_name = malloc(20);
  char *p_age = malloc(20);

  if (stream != stdin){
    if (fgets(p_name, 20, stream) != NULL){
      char *p = strrchr(p_name, '\n');
      if (p)
        *p = '\0';
      strcpy(new_player->name, p_name);
    }
    if (fgets(p_age, 20, stream) != NULL)
      new_player->age = atoi(p_age);
  }
  else {
    printf("enter name and age for a player\n");
    gets(p_name);
    gets(p_age);
    strcpy(new_player->name, p_name);
    new_player->age = atoi(p_age);
  }

  free(p_name);
  free(p_age);
  return new_player;
}

void player_inspect(struct player plyr, char* prefix){
  printf("[%s] name: %s\n", prefix, plyr.name);
  printf("[%s] age : %d\n", prefix, plyr.age);
}

int main(int argc, char* argv[]){
  FILE * stream;
  char* argument;
  // below: trying to allocate (argc - 1) pointers
  // valgrind's --show-origins=yes points here for both errors
  struct player **players = malloc(sizeof(int) * (argc - 1)); 
  int i = 1;
  for (; i < argc; i++){
    argument = argv[i];
    if (strcmp("-", argument) != 0){
      if ((stream = fopen(argument, "r")) == NULL) perror("Error opening file");
      else {
        // the next line emits Invalid write of size 8 in valgrind
        players[i-1] = player_new_from_stream(stream);
        fclose(stream);
      }
    } else {
      players[i-1] = player_new_from_stream(stdin);
    }
  }

  i = 0;
  char buffer[15];
  for (; i < argc - 1; i++){
    sprintf(buffer, "%d", i);
    // the next line emits Invalid read of size 8
    player_inspect(*(players[i]), buffer);
    free(players[i]);
  }

  free(players);
  return 0;
}

What is wrong here? I want to return a pointer to struct player from player_new_from_stream and pack this pointer to array players in main().

  • 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-29T07:39:38+00:00Added an answer on May 29, 2026 at 7:39 am

    This is wrong:

    struct player **players = malloc(sizeof(int) * (argc - 1));
    

    Use this instead:

    struct player **players = malloc(sizeof(*players) * (argc - 1));
    

    Note that on your system, sizeof(int) == 4 while sizeof(struct player *) == 8.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.