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 Cpiral (talk | contribs) at 21:31, 1 October 2011 (Structural padding: reword, and assert removing the material). 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]

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

The lead section is misleading where it talks about what appears to be the (interesting) subject of the internals of structs. It reads as if struct members are unique in that that they alone are padded. But to the common reader, just starting out programming, or merely skimming the subject of data structures themselves, the only internal of a struct that might be interesting (a diversion in their case) is the conceptual size of a struct as a value type, or the method of chaining the members to one another. The mention of padding refers to something deeper—compiler design, specifically data structure alignment:

A memory pointer that refers to primitive data that is n bytes long is said to be aligned if it is only allowed to contain addresses that are n-byte aligned, otherwise it is said to be unaligned. A memory pointer that refers to a data aggregate (a data structure or array) is aligned if (and only if) each primitive datum in the aggregate is aligned.

Thus byte padding is not really implemented upon structures, or between structures. They are just input and output, and words themselves are the padding that separate bytes. If the compiler aligns the data, both the primitives alone and the primitives inside the composites, then the concept of padding reduces to the concept of words: the first bytes are aligned on a sort of word-grid. Padding is then just a side-effect of the subject of alignment, not of structures themselves. It is not unlike bits in a byte. Padding is not spoken of in that context, nor should it be in our context.

I move we place "If the C compiler aligns the members of a struct, then there is these neat trick that telecom programmers use..." deeper in the article, if it is not removed entirely. This concept is not unique to the C struct. There is an analogous dynamic that also "works as expected" concerning subclassing in C++. — CpiralCpiral 21:31, 1 October 2011 (UTC)[reply]