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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:49:34+00:00 2026-05-18T06:49:34+00:00

I’ve always programmed in Java, which is probably why I’m so confused about this:

  • 0

I’ve always programmed in Java, which is probably why I’m so confused about this:

In Java I declare a pointer:

int[] array

and initialize it or assign it some memory:

int[] array = {0,1,0}
int[] array = new int[3]

Now, in C, it’s all so confusing. At first I thought it was as easy as declaring it:

int array[]

and initializing it or assigning it some memory:

int array[] = {0,1,0}
int array[] = malloc(3*sizeof(int))
int array[] = calloc(3,sizeof(int))

Unless I’m wrong, all of the above is equivalent Java-C, right?

Then, today I met a code in which I found the following:

pthread_t tid[MAX_OPS];

and some lines below, without any kind of initialization…

pthread_create(&tid[0],NULL,mou_usuari,(void *) 0);

Surprisingly (at least to me), the code works! At least in Java, that would return a nice “NullPointerException”!

So, in order:

  1. Am I correct with all of the Java-C “translations”?

  2. Why does that code work?

  3. Is there any difference between using malloc(n*sizeof(int)) and calloc(n,sizeof(int))?

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-18T06:49:35+00:00Added an answer on May 18, 2026 at 6:49 am

    You can’t assign memory to an array. An array has a fixed size, for the whole of its lifespan. An array can never be null. An array is not a pointer.

    malloc returns the address to a memory block that is reserved for the program. You can’t “assign” that (being the memory block) to an array, but you can store the address of this memory block in a pointer: luckily, array subscription is defined through pointers – so you can “use pointers like arrays”, e.g.

    int *ptr = malloc(5 * sizeof *ptr);
    ptr[2] = 5; // access the third element "of ptr"
    free(ptr); // always free at the end
    

    When you declare an array without a size (i.e. array[]), it simply means the size of the array is determined from the initializer list. That is

    int array[] = {1, 2, 3, 4, 5}; // is equal to
    int array[5] = {1, 2, 3, 4, 5};
    

    Trying to declare an array without a size and without an initializer is an error.


    The code pthread_t tid[MAX_OPS]; declares an array named tid of type pthread_t and of size MAX_OPS.

    If the array has automatic storage (i.e. declaration is inside a function and not static, not global), then each of the arrays elements has indeterminate value (and it would cause undefined behavior trying to read such value). Luckily, all that the function call does is that it takes the address of the first element of the array as the first parameter, and probably initializes it (the element) inside the function.


    The difference of calloc and malloc is that the memory block that calloc returns is initialized to zero. That is;

    int *ptr = calloc(5, sizeof *ptr);
    // is somewhat equal to
    int *ptr = malloc(5 * sizeof *ptr);
    memset(ptr, 0, 5 * sizeof *ptr);
    

    The difference between

    int *ptr = malloc(5 * sizeof *ptr);
    // and
    int array[5];
    

    is that array has automatic storage, (is stored on stack), and is “released” after it goes out of scope. ptr, however, (is stored on heap), is dynamically allocated and must be freed by the programmer.

    • 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
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I have some data like this: 1 2 3 4 5 9 2 6
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

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.