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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:27:43+00:00 2026-05-25T16:27:43+00:00

Maybe a bad topic, but given the following code, do i need to free(player->name)

  • 0

Maybe a bad topic, but given the following code, do i need to free(player->name) too?

#include <stdio.h>

struct Player
{
       char *name;
       int level;       
};

int main()
{
    struct Player *player;
    player->name = malloc(sizeof(player->name)*256);
    player->name = "John";

    printf(player->name);

    free(player);

    printf("\n\n\n");
    system("PAUSE");
    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-05-25T16:27:44+00:00Added an answer on May 25, 2026 at 4:27 pm

    Oh boy, where to start? You really need a good book. Sigh. Let’s start at the top of main():


    This

    struct Player *player;
    

    defines a pointer to a struct Player, but it doesn’t initialize it. It has thus a more or less random value, pointing somewhere into memory. This

    player->name = malloc(sizeof(player->name)*256);
    

    now writes into parts of that random location the address of a piece of memory obtained by malloc(). Writing to memory through an uninitialized pointer invokes Undefined Behavior. After that, all bets are off. No need to look further down your program. You are unlucky that, by accident, you write to a piece of memory that is owned by your process, so it doesn’t crash immediately, making you aware of the problem.

    There’s two ways for you to improve that. Either stick to the pointer and have it point to a piece of memory allocated for a Player object. You could obtain it by calling malloc(sizeof(Player).
    Or just use a local, automatic (aka stack-based) object:

    struct Player player;
    

    The compiler will generate the code to allocate memory on the stack for it and will release it automatically. This is the easiest, and should certainly be your default.


    However, your code has more problems than that.

    This

    player->name = malloc(sizeof(player->name)*256);
    

    allocates consecutive memory on the heap to store 256 pointers to characters, and assigns the address of the first pointer (the address of a char*, thus a char**) to player->name (a char*). Frankly, I’m surprised that even compiles, but then I’m more used to C++’ stricter type enforcement. Anyway, what you probably want instead instead is to allocate memory for 256 characters:

    player->name = malloc(sizeof(char)*256);
    

    (Since sizeof(char) is, by definition, 1, you will often see this as malloc(256).)
    However, there more to this: Why 256? What if I pass a string 1000 chars long? No, simply allocating space for a longer string is not the way to deal with this, because I could pass it a string longer still. So either 1) fix the maximum string length (just declare Player::name to be a char array of that length, instead of a char*) and enforce this limit in your code, or 2) find out the length needed dynamically, at run-time, and allocate exactly the memory needed (string length plus 1, for the terminating '\0' char).

    But it gets worse. This

    player->name = "John";
    

    then assigns the address of a string literal to player->name, overriding the address of the memory allocated by malloc() in the only variable you store it in, making you lose and leak the memory.
    But strings are no first-class citizens in C, so you cannot assign them. They are arrays of characters, by convention terminated with a '\0'. To copy them, you have to copy them character by character, either in a loop or, preferably, by calling strcpy().

    To add insult to injury, you later attempt to free the memory a string literal lives in

    free(player);
    

    thereby very likely seriously scrambling the heap manager’s data structures. Again, you seem to be unlucky for that to not causing an immediate crash, but the code seemingly working as intended is one of the worst possibilities of Undefined Behavior to manifest itself. If it weren’t for all bets being off before, they now thoroughly would be.


    I’m sorry if this sounds condemning, it really wasn’t meant that way, but you certainly and seriously fucked up this one. To wrap this up:

    1. You need a good C++ book. Right now. Here is a list of good books assembled by C programmers on Stack Overflow. (I’m a C++ programmer by heart, so I won’t comment on their judgment, but K&R is certainly a good choice.)

    2. You should initialize all pointers immediately, either with the address of an existing valid object, or with the address of a piece of memory allocated to hold an object of the right type, or with NULL (which you can easily check for later). In particular, you must not attempt to read from or write to a piece of memory that has not been allocated (dynamically on the heap or automatically on the stack) to you.

    3. You need to free() all memory that was obtained by calling malloc() exactly once.

    4. You must not attempt to free() any other memory.


    I’m sure there is more to that code, but I’ll stop here. And did I mention you need a good C book? Because you do.

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

Sidebar

Related Questions

Maybe I'm having a bad day, but I can't seem to combine these 2
Maybe I'm having a really bad day, but could someone possibly help me to
Maybe the need to do this is a 'design smell' but thinking about another
Maybe I'm just thinking about this too hard, but I'm having a problem figuring
So, maybe this is a bad design; I don't know. But say I have
Maybe it is a bad habit which I still have since Flash Professional, but
This is maybe a stupid question, but I want to know if my code
sorry for bad Subject of topic but i couldnt find out what to write
I need to build a proxy (maybe a bad description) that receives an XML
Maybe I just don't know .NET well enough yet, but I have yet to

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.