in c what is the difference in making an array like : int nums[2]; vs int *nums = malloc(sizeof(int) * 2);
What is the difference if it were char?
Is the difference that the first one is in the stack and the second one is in the heap?
atrt7 wrote:
in c what is the difference in making an array like : int nums[2]; vs int *nums = malloc(sizeof(int) * 2);

The first is a static (non-resizable) allocation, and the second is dynamic (resizable) and must be freed after use to prevent memory leaks.

atrt7 wrote:
What is the difference if it were char?

Basically just the size, it's still all just setting aside RAM.

atrt7 wrote:
Is the difference that the first one is in the stack and the second one is in the heap?

Umm . . . yes

EDIT: Also, this is one of those questions that you should probably just Google, as it has definitely been asked many times before.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement