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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:29:14+00:00 2026-06-04T09:29:14+00:00

I’ve been reading Learn C the Hard Way and I ran across an interesting

  • 0

I’ve been reading “Learn C the Hard Way” and I ran across an interesting problem that I was hoping someone could explain in detail. The basic exercise was to use a struct involving an example Person struct. In the first case, I had a constructor-ish method (I don’t exactly get why the tutorial did it this way if someone wouldn’t mind explaining on the side :D) along with the struct like this:

struct Person {
  char *name;
  int age;
  int height;
  int weight; 
};

struct Person *Person_create(char *name, int age, int height, int weight) 
{
  struct Person *who = malloc(sizeof(struct Person)); 
  assert(who != NULL);

  who->name = strdup(name);
  who->age = age; 
  who->height = height;
  who->weight = weight;
}

Along with rest of the code, this runs just fine, but when I print the memory location of the pointers to these structs like:

int main(int argc, char* argv[]) {
     struct Person *joe = Person_create("joe alex", 12, 80, 100);
     struct Person *joe = Person_create("frank blank", 20, 72, 140);
}

The difference in their memory values are always exactly 40. In comparison, an implementation of the struct constructor like:

 struct Person{//is the same as above};

 struct Person Person_create (char* name, int age, int height, int weight) {
  struct Person newPerson;
  newPerson.name = name; //and so on

  return newPerson
 }

When I print out the memory locations of joe and frank, initialized with the same values as above, with the above implementation, the difference in their memory location appears to be always 20.

I studied a bit of assembly and I am aware that the compiler assigns blocks of memory according to the data types of the struct so I was thinking.. in either implementations, a char array has so many chars so n characters * 1 = char space, and then I have 3 ints so 3*4 = 12; 12 + 9 (Joe Alex\0 right?) = 21.. I probably got the \0 wrong so it’s equal to 20 or something, but regardless of the specific memory size of my structs, I’m more interested in why the two different implementations result in a different size in memory (to my understanding, DOUBLE the memory cost is quite a significant amount).

  • 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-04T09:29:15+00:00Added an answer on June 4, 2026 at 9:29 am

    In the first case 4 dynamic allocations are made: two name strings with strdup, and 2 Person structs with malloc. The difference between the struct addresses tells you roughly how much memory was allocated including the first struct, and before the second one[*]

    In the second case, no dynamic allocations are made, but I imagine you’ve created two Person objects on the stack:

    struct Person joe = Person_create("joe alex", 12, 80, 100);
    struct Person frank = Person_create("frank blank", 20, 72, 140);
    

    The difference between their addresses tells you roughly how much memory the first one occupies[*].

    Since you haven’t copied the strings, you’re pretty much required to either use a string literal as the name, or else to manage its memory separately from the Person struct. The string data is not part of the struct Person — never will be. newPerson.name = name just stores the pointer, not the string data.

    This becomes a nuisance as soon as you start reading the names from a file or the terminal: you’ll have to dynamically allocate them anyway, so you won’t save any memory, but you’ll have to write more code. The exercise probably includes a Person_Destroy function that frees the name, so that the user of struct Person doesn’t have to worry about that separately.

    [*] Except that the implementation isn’t required to allocate these things one after the other, so your method doesn’t work in general. It just happens to be giving results on this occasion consistent with the theory that the allocations are placed in the order you made them.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I am doing a simple coin flipping experiment for class that involves flipping a
I have a French site that I want to parse, but am running into

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.