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

  • Home
  • SEARCH
  • 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 6795021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:17:33+00:00 2026-05-26T18:17:33+00:00

consider the code below: #include list.h struct List { int size; int* data; };

  • 0

consider the code below:

#include "list.h"
struct List
{
   int size;
   int* data;
};
List *list_create()
{
   List *list;
   printf("%d %d",sizeof(list),sizeof(List));
   list = malloc(sizeof(list));
   assert(list != NULL);
   if (list != NULL) {
      list->size = 0;
   }
   return list;
}

The number printed out is “4 8”, i assume this is the 4 bytes taken by “int size” in List object?and the size of “int* data” is 0 cause nothing has assigned to data?
the size of int pointer is also 4 bytes so the type List take 8 bytes in total? or there are some thing else going on? Can some one help me understand all this in detail?

then the malloc() get 4 bytes from the heap and assign the address to the pointer list? later in main if i do “list->data[i]=1;” this will give me a run time error why? Is it because I cant change contents in the heap? but if i do “list->size++” this would work,
isn’t the whole list object is in the heap?

really need some help here

Thanks in advance.

  • 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-26T18:17:34+00:00Added an answer on May 26, 2026 at 6:17 pm

    sizeof(List*) is the size of a pointer to a List struct.

    sizeof(list) in your case, since variable list is of type List* is the same as sizeof(List*).

    sizeof(List) instead is the size of the struct List, it contains two 32 bit variables (I assume you are using a 32 bit compiler obviously), an integer and a pointer and your compiler decided that the right size for your struct is 8 bytes.

    Pointers to types are usually 4 byte in 32 bit compilers and 8 bytes in 64 bit compilers.

    As a side note, reading your code however i read you never initialize list->data, you should initialize it to something somewhere i guess.

    This is C++ however, you should write

    typedef struct { ... } List; // This is C.
    

    Sizeof operator is evaluated at compile time, not at runtime, it gives only information of the size of a type.
    You cannot, for example, know how much elements are in a dynamic array with sizeof, if you were trying to accomplish this, sizeof(pointer) will give you the size in byte of the pointer type.

    As something to read about what is a pointer and what is an array i would suggest you to read http://www.lysator.liu.se/c/c-faq/c-2.html or http://pw1.netcom.com/~tjensen/ptr/pointers.htm

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

Sidebar

Related Questions

My questions are divided into three parts Question 1 Consider the below code, #include
Consider a code excerpt below: typedef struct tagTHREADNAME_INFO { DWORD dwType; LPCTSTR szName; DWORD
Consider following codes: #include <stdio.h> #include <malloc.h> void allocateMatrix(int **m, int l, int c)
Consider the code below: ScriptRuntimeSetup setup = Python.CreateRuntimeSetup(null); ScriptRuntime runtime = new ScriptRuntime(setup); ScriptEngine
Consider the code below: this.usedIds = 0; this.SendData = function(data) { var id =
Consider the following sample code below: #include <iostream> using namespace std; class base {
Consider the JavaScript code below, inspired from the YUI documentation on YAHOO.lang.extend . In
Consider the Java code below, what would happen if there were no paintComponent method
Consider the code: PreparedStatement ps = null; ResultSet rs = null; try { ps
Consider this code (Java, specifically): public int doSomething() { doA(); try { doB(); }

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.