kernelnewbies.kernelnewbies.org archive mirror
 help / color / mirror / Atom feed
* How to tell if a field in c struct is initialized?
@ 2017-12-08 12:47 Shiyao MA
  2017-12-08 13:11 ` Greg KH
  0 siblings, 1 reply; 3+ messages in thread
From: Shiyao MA @ 2017-12-08 12:47 UTC (permalink / raw)
  To: kernelnewbies

Hi,

I come across this code:
http://elixir.free-electrons.com/linux/latest/source/drivers/scsi/scsi_netlink.c#L128

other fields such as .cbmutex, .bind, etc are not initialized.

Since the object =cfg= is of automatic storage, I wonder how the
kernel tells if a field is initialized or its value is just some
random bits on the stack?

-- 
Best,
Shiyao

^ permalink raw reply	[flat|nested] 3+ messages in thread

* How to tell if a field in c struct is initialized?
  2017-12-08 12:47 How to tell if a field in c struct is initialized? Shiyao MA
@ 2017-12-08 13:11 ` Greg KH
  2017-12-08 13:23   ` Shiyao MA
  0 siblings, 1 reply; 3+ messages in thread
From: Greg KH @ 2017-12-08 13:11 UTC (permalink / raw)
  To: kernelnewbies

On Fri, Dec 08, 2017 at 08:47:42PM +0800, Shiyao MA wrote:
> Hi,
> 
> I come across this code:
> http://elixir.free-electrons.com/linux/latest/source/drivers/scsi/scsi_netlink.c#L128
> 
> other fields such as .cbmutex, .bind, etc are not initialized.
> 
> Since the object =cfg= is of automatic storage, I wonder how the
> kernel tells if a field is initialized or its value is just some
> random bits on the stack?

The fields are initialized to zero.  It's a C specification requirement.

Hope this helps,

greg k-h

^ permalink raw reply	[flat|nested] 3+ messages in thread

* How to tell if a field in c struct is initialized?
  2017-12-08 13:11 ` Greg KH
@ 2017-12-08 13:23   ` Shiyao MA
  0 siblings, 0 replies; 3+ messages in thread
From: Shiyao MA @ 2017-12-08 13:23 UTC (permalink / raw)
  To: kernelnewbies

Thanks for your reply.

Best,

On Fri, Dec 8, 2017 at 9:11 PM, Greg KH <greg@kroah.com> wrote:
> On Fri, Dec 08, 2017 at 08:47:42PM +0800, Shiyao MA wrote:
>> Hi,
>>
>> I come across this code:
>> http://elixir.free-electrons.com/linux/latest/source/drivers/scsi/scsi_netlink.c#L128
>>
>> other fields such as .cbmutex, .bind, etc are not initialized.
>>
>> Since the object =cfg= is of automatic storage, I wonder how the
>> kernel tells if a field is initialized or its value is just some
>> random bits on the stack?
>
> The fields are initialized to zero.  It's a C specification requirement.
>
> Hope this helps,
>
> greg k-h



-- 
Best,
Shiyao

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2017-12-08 13:23 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-12-08 12:47 How to tell if a field in c struct is initialized? Shiyao MA
2017-12-08 13:11 ` Greg KH
2017-12-08 13:23   ` Shiyao MA

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).