Jump to content

Talk:Struct (C programming language)

Page contents not supported in other languages.
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by BinaryDigit~enwiki (talk | contribs) at 17:00, 14 March 2012 (Declare/define: new section). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
WikiProject iconC/C++ Start‑class Mid‑importance
WikiProject iconThis article is within the scope of WikiProject C/C++, a collaborative effort to improve the coverage of C and C++ topics on Wikipedia. If you would like to participate, please visit the project page, where you can join the discussion and see a list of open tasks.
StartThis article has been rated as Start-class on Wikipedia's content assessment scale.
MidThis article has been rated as Mid-importance on the importance scale.

Article or redirect?

From history, this page has been in three rather different states:

Each is reasonable (although the last perhaps less so), but there seems to have been no discussion of the matter. Worse, the Structs page had gotten set as redirecting to Object composition, making the singular and plural of "struct" refer to very different things.

The evolution seems to be that what started out as being about C (programming language) specifically got geared towards C++, and then towards abstract object orientation. Later someone reacted to this and changed the redirect to the more common (but still abstract) interpretation of Record (computer science). 90.230.192.94 (talk) 17:17, 8 May 2009 (UTC)[reply]

This article should observe that struct names are a namespace separate from the main namespace of types and variables, so that one can declare

typedef struct ListNode { int val; struct ListNode *next; } ListNode;

That is, it is not necessary to use different names (ListNode_ and ListNode) for the two C views of wha is essentially the same data type.
All the best, --Jorge Stolfi (talk) 21:57, 15 May 2009 (UTC)[reply]

Type punning

"For example, common Internet protocols rely on the fact that C compilers insert padding between struct fields in predictable ways [...]"

Is this an example of type punning? --Abdull (talk) 22:52, 2 January 2010 (UTC)[reply]

I don't think so. I think it was intended for uses such as those shown in the example, and not, as Type punning characterized it—a subversion. See #Stuctural padding below. — CpiralCpiral 23:16, 1 October 2011 (UTC)[reply]

Call by reference or call by value?

In C, when calling a subroutine or receiving a return value, is a struct passed/returned by value or by reference? Does the ISO/IEC C standard discuss this topic explicitly? Thanks, --Abdull (talk) 09:18, 2 May 2010 (UTC)[reply]

I haven't looked at the standard you mention, but it has always been part of C that a struct is passed and returned by value. If a struct contains data occupying 1000 bytes, the compiler pushes 1000 bytes onto the stack. C programmers often pass a pointer to a struct, which is what a "pass by reference" language would do. I suppose this could be mentioned somewhere (it is implied at C (programming language)#Characteristics), but it's probably excessive detail for an article. Johnuniq (talk) 02:03, 3 May 2010 (UTC)[reply]

Structural padding

Assuming "padding" is strictly related to data structure alignment, I move we place the information about padding deeper into the article, and change the concept from "padding" to alignment. e.g. "The C compiler aligns the members of a struct..." A lead section might better focus on the unique aspects. There are two things that are not unique about the coded padding example.

  • Padding is not unique to structs if alignment is used throughout the executable. In case the compiler does align data types, member data types, and instructions and cooked data for the executable, then the compiler routine for byte padding a struct need not be called (assuming there is not some other addressing algorithm, unrelated to word size).
  • The internal alignment of data members is not unique to the C struct. There is an analogous dynamic that also "works as expected" concerning subclassing in C++. That said it is very interesting that even if a C compiler is run "unaligned" it will always align (don't say "pad"?) struct members inside the struct, guaranteeing this manipulation by a sub-struct of quantity N-last elements manipulating its containing struct of N elements.

Salient internals for our lead section are the size (as mentioned), and the method of chaining the members of a C struct to one another. — CpiralCpiral 21:31, 1 October 2011 (UTC)[reply]

Declare/define

"The memory is already given and zeroed by just declaring a variable of that type regardless of member initialization" is nonsense. Declarations do not allocate memory. Object definitions allocate memory. Furthermore, that memory is not "zeroed" by a definition (let alone a declaration!). It is necessary to initialize at least one member of the struct if one wishes the default static initializer rule to cut in. If the initializer is another struct, a bitwise copy is made, and thus any members (note: members, not fields!) that were 0 in the initializing struct will be 0 in the initialized struct.

I didn't bother correcting the article, because it's a waste of time. There always seems to be someone ready and waiting to uncorrect it again.

BinaryDigit (talk) 17:00, 14 March 2012 (UTC)[reply]