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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:49:27+00:00 2026-06-11T15:49:27+00:00

I am trying to figure out the difference between structs on the heap and

  • 0

I am trying to figure out the difference between structs on the heap and on the stack. I have the following structs, for Book, Author and Shelf. Book has multiple authors and Shelf has multiple books.

struct Author {
    char name[NAME_LENGTH];
};

struct Book {
    char title[TITLE_LENGTH];
    struct Author authors[AUTHORS_PER_BOOK];
    int author_count;
};

struct Shelf {
    struct Book books[BOOKS_PER_SHELF];
    int book_count;
};

I have a bunch of functions of creating the Author and Book structs, with specified name and title. And a function for adding authors to book. I understand that C is strictly pass by value, so I used a pointer to the book in add_authors_to_book function. These functions are created locally, I am assuming they are on the stack(?).

struct Author new_author(char name[]) {
    struct Author author;
    strcpy(author.name, name);

    return author;
}

struct Book new_book(char title[]) {
    struct Book book;
    strcpy(book.title, title);

    book.author_count = 0;

    return book;
}

void print_book(struct Book book) {
    printf("%s\nby", book.title);
    int i, n;
    for (i = 0, n = book.author_count; i < n; i++) {
        printf(" %s,", book.authors[i].name);
    }

    printf("\b.\n");
}

void add_author_to_book(struct Book *book, struct Author author) {
    book->authors[book->author_count] = author;
    book->author_count++;
}

I want the shelf to be on the heap. I am allocating memory for it and freeing it below.

struct Shelf *new_shelf() {
    struct Shelf *shelf = malloc(sizeof(struct Shelf));
    shelf->book_count = 0;

    return shelf;
}

void delete_shelf(struct Shelf *shelf) {
    free(shelf);
}

void print_shelf(struct Shelf *shelf) {
    printf("Shelf has the %d book(s),\n", shelf->book_count);
    int i, n;
    for (i = 0, n = shelf->book_count; i < n; i++) {
        print_book(shelf->books[i]);
    }
}

My question is regarding adding the books to the shelf. Below I am assigning the book struct to the shelf’s book at it’s last index. Where does this struct Book reside, on the Heap or on the Stack? Will C create clone of the entire Book structure including the authors automatically when passing it to the add_book_to_shelf? Does pass by value work with such nested structs?

Also how do you go about freeing up the memory used by books and it’s authors?

void add_book_to_shelf(struct Shelf *shelf, struct Book book) {
    shelf->books[shelf->book_count] = book;
    shelf->book_count++;
}

Here’s my test code,

int main(int argc, char *argv[]) {
    struct Shelf *shelf = new_shelf();

    struct Book book = new_book("Freakonomics");
    add_author_to_book(&book, new_author("Stephen Dubner"));
    add_author_to_book(&book, new_author("Steven Levitt"));

    add_book_to_shelf(shelf, book);

    print_shelf(shelf);
    delete_shelf(shelf);
    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-11T15:49:28+00:00Added an answer on June 11, 2026 at 3:49 pm

    You’re copying the book in add_book_to_shelf, which means it will be on the heap.

    The book that you created in main will be on the stack, (since it is an automatic variable,) but in add_book_to_shelf the book is copied into the books array which is allocated as part of the shelf on the heap.

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

Sidebar

Related Questions

I am trying to figure out the difference between Memcached and JPA, this is
I am trying to figure out the difference between Google's GData API ( http://code.google.com/p/gdata-python-client/
I'm trying to figure out the difference between Where(Expression) and Single(Expression). Is the Expression
I am trying to figure out the difference between $a=&$b and $a=$b . I
I'm trying to figure out the difference between histograms made on Matlab (using the
I am trying to figure out what the difference is between onKeyPress and onKeyUp.
I am trying to figure out what exactly is the difference between debug configuration
I'm trying to figure out the difference between ASP.NET and ASP.NET MVC. Mode-View-Controller called
I am trying to figure out the difference between Add Variable and the Add
I am trying to figure out what the difference is between ORM and ODM,

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.