LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-13-2005, 03:32 PM   #1
nibbler
Member
 
Registered: Oct 2003
Location: Croatia
Distribution: Fedora 4
Posts: 46

Rep: Reputation: 15
Question Is array allocated on heap set to zero?


Hello,

I've heard that in C an array allocated on heap is set to zero ( and it worked so far ), although I know that it is generally not true for stack allocation. If it is true, why is this so? Is it true for linux, or is it defined in the ANSI C? I haven't found the answer on google, so any link would be great.

Thanks in advance
 
Old 12-13-2005, 03:43 PM   #2
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
It is the same reason that a pointer could point to some random value, but primitives are initialized to something predictable.
 
Old 12-14-2005, 02:50 AM   #3
nibbler
Member
 
Registered: Oct 2003
Location: Croatia
Distribution: Fedora 4
Posts: 46

Original Poster
Rep: Reputation: 15
I'm not sure I understand. If by primitives you think basic data types such as int or double, those aren't set to zero in C ( when declared on stack, and I'm wondering is it true for heap ).
 
Old 12-14-2005, 02:55 AM   #4
IBall
Senior Member
 
Registered: Nov 2003
Location: Perth, Western Australia
Distribution: Ubuntu, Debian, Various using VMWare
Posts: 2,088

Rep: Reputation: 62
I would say never assume. The gcc compiler might, the Borland C compiler might not. If you always initialise all your variables properly, then you always know that they have been initialised.

--Ian
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
In C, using qsort for dynamically allocated array ntmsz Programming 7 08-23-2005 10:33 AM
Mount a raid array with SGID bit set, howto? anand_kt Linux - General 4 04-01-2005 01:30 AM
Find out heap size for a PID luexiong Linux - General 1 03-29-2005 09:40 AM
increasing heap size podollb Linux - Software 3 03-06-2005 11:42 PM
Modifying the heap available to a process exitsfunnel Linux - Software 0 12-10-2003 09:20 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:53 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration