# Unity Why declare in reversed order for _WIN64?

This topic is 4616 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Good day people, I am seeing this in WinSock.h. But I'm not sure why do we have to "reverse" the declaration order if we're compiling it for _WIN64?   struct servent   {     #ifdef _WIN64       char    FAR * s_proto;       short   s_port;     #else       short   s_port;       char    FAR * s_proto;     #endif   }; Thanks in advance for any replies

##### Share on other sites
Structure alignment is usually done from highest size to lowest size in a descending order. This is done to attempt to keep structures aligned to whatever boundary the system desires ( 32bit 64bit, etc) and to reduce the amount of padding that would be added to a structure. Ptrs in Win64 are now going to be 8bytes, while the short will remain 2 bytes.

//Win32 structstruct fine{    int m_int;   (4bytes no padding)    long m_long;  (4bytes no padding)    int m_int2;   (4bytes no padding)};// but in win64struct bad{     int m_int;    (4bytes, 4bytes of padding (8 total bytes) )     long m_long;  (8bytes, 0 padding)     int m_int;    (4bytes, 4bytes of padding (8 total bytes) )};struct win64Good{    long m_long;   (8bytes no padding)    int m_int;   (m_int and m_int2 fill up 8 consecutive bytes..no padding)    int m_int2;};

[EDIT]
hmmm. Thats odd, now that I re-read your post, either way the char * would be bigger than the short.. There goes that theory =) Unless that is meant for backward compatibility for 16-bit systems too...
[EDIT]

##### Share on other sites
Different endianess?

##### Share on other sites
moeron is correct as to the desire to change. Now it breaks binary compatibility, but Win64 in native mode does anyway, which is why they can get away with it.
Don't see any difference byte order would make.

##### Share on other sites
Thanks guys,

This is the complete structure (I shouldn't have removed the first two
members). But even in this case padding of 6 bytes will still happen to "short
s_port" too, right? Unless it's better to have padding at the end of the
structure rather than somewhere in the middle.

struct servent
{
char    FAR * s_name;
char    FAR * FAR * s_aliases;
#ifdef _WIN64
char    FAR * s_proto;
short   s_port;
#else
short   s_port;
char    FAR * s_proto;
#endif
};

##### Share on other sites
Quote:
 Original post by Jan Wassenbergmoeron is correct as to the desire to change. Now it breaks binary compatibility, but Win64 in native mode does anyway, which is why they can get away with it.Don't see any difference byte order would make.
Yes, excellent answer. It's one of those things where someone didn't do it the best way to begin with. I get those a lot.

##### Share on other sites
Quote:
 But even in this case padding of 6 bytes will still happen to "short s_port" too, right?

Negative - padding is only strictly required before a data member. Sufficiently studly compilers could avoid adding padding to the end of a struct by checking alignment requirements of subsequent data.

iMalc:
Quote:
 Yes, excellent answer. It's one of those things where someone didn't do it the best way to begin with. I get those a lot.

:) Hindsight is 20/20; once released, it's reasonable to set APIs in stone.
How do you mean "I get those a lot"?

##### Share on other sites
Thanks Jan, if I get your point correctly, the "bad" structure Moeron shown may
or may not have padding at the final "int" then? If no padding at the end were
to happen, the whole structure will be of size 8 + 8 + 4 = 20 bytes. That doesn't look
like an "optimized" size to me... Just wondering.

{
int  m_int1;
long m_long;
};

##### Share on other sites
Exactly.
As to "optimized", it's a bit wasteful, yes. But the compiler cannot do anything about it; struct members must not be rearranged, since the coder may access them via pointer+direct offset.
This is why we must manually order from largest-smallest.

##### Share on other sites
Thanks Jan and everyone else, it's much clearer now. But I am still a bit
concerned about Microsoft doing that to their "globally" used header files. If
anyone of us is doing a direct offset into a structure, that code is going to
fail (though I know we should never do that).

##### Share on other sites
Again, MS can afford to do this because there are no existing apps for Win64; they all need to be recompiled anyway. The chance is now there; that they're using it and cleaning up stuff like this is a good thing :)

I agree that direct offsets are evil, but every so often one sees reasonable uses in low-level code (e.g. messing around with undocumented structures where you don't have proper member names). One further reason that the no-reorder guarantee is important: we'd otherwise have even more interop problems with file header structs etc.

##### Share on other sites
Thanks Jan, for the clear explanation.

• 10
• 11
• 12
• 10
• 12
• ### Similar Content

• So I am building a turn based rogue-like (think CDDA). The game is going to have a very large map (up to 1000's x 1000's) however to alleviate most of that I obviously can't render everything so there will just be render a certain radius around the player and just load in and out data as the player moves.
The next major system I am prototyping is making interactive tiles destructible and pretty much everything will be destructible besides basic landscape (cars, doors, windows, structures, etc. will be destructible)
While I am only rendering a certain amount of tiles around the player, I want to keep the amount of colliders active at one time to be as small as possible for performance and currently the tilemap tool I use automatically merges colliders together.
So instead of creating a separate colliders for each of these tiles and having the destructible behavior tied to that object (which my tilemap tool would allow me to do) I was thinking that I would store an array of all the X and Y locations for the interactive tilemap layer and let the tilemap manage the colliders.
Then when I hit a collider on the interactive tilemap layer, instead of of getting the behavior for how to deal with the destruction for that tile from that game object, I would pull it from the array I mentioned earlier based on the tile I attempt to interact with which I already have.
Does this sound like a good approach? Any other recommendations would be welcomed.

• Hey guys,
I have a really weird problem. I'm trying to get some data from a REST service. I'm using the following code:

private void GetTheScores() { UnityWebRequest GetCommand = UnityWebRequest.Get(url); UnityWebRequestAsyncOperation operation = GetCommand.SendWebRequest(); if (!operation.webRequest.isNetworkError) { ResultsContainer rez = JsonUtility.FromJson<ResultsContainer>(operation.webRequest.downloadHandler.text); Debug.Log("Text: " + operation.webRequest.downloadHandler.text); } } The problem is that when I'm in Unity's editor, the request doesn't return anything (operation.webRequest.downloadHandler.text is empty, the Debug.Log command just prints "Text: "), but when I enter the debug mode and insert a breakpoint on that line, then it returns the text properly. Does anyone have an idea why is this happening?
The real problem I'm trying to solve is that when I receive the text, I can't get the data from the JSON. The markup is really simple:
[{"id":1,"name":"Player1"},{"id":2,"name":"Player2"}] and I have an object that should accept that data:
[System.Serializable] public class ResultScript { public int id; public string name; } There is also a class that should accept the array of these objects (which the JSON is returning):
[System.Serializable] public class ResultsContainer { public ResultScript[] results; } But when I run the code (in the debug mode, to get any result) I get an error: ArgumentException: JSON must represent an object type. I've googled it but none of the proposed solutions work for me.
Also (regardless if I'm in the debug mode or not) when I try to do some string operations like removing or adding characters to the GET result, the functions return an empty string as a result
Can you help me with any of these problems?
Thank you
• By nihitori
The Emotional Music Vol. I pack focuses on beautiful and esoteric orchestral music, capable of creating truly emotive and intimate moods. It features detailed chamber strings, cello and piano as the main instruments, resulting in a subtle and elegant sound never before heard in video game royalty-free music assets.

The pack includes 5 original tracks, as well as a total of 47 loops based on these tracks (long loops for simple use and short loops for custom / complex music layering).

A 15 seconds preview of each main track is available on Soundcloud:

• Another one of our new UI for #screenshotsaturday. This is the inventory screen for showing what animal fossils you have collected so far. #gamedev #indiedev #sama

• We're looking for programmers for our project.
Our project is being made in Unity
Requirements:
-Skills in Unity
-C#
-Javascript
-Node.js
We're looking for programmers who can perform a variety of functions on our project.
Project is a top-down hack-and-slash pvp dungeon-crawler like game. Game is entirely multiplayer based, using randomized dungeons, and a unique combat system with emphasis on gameplay.
We have a GDD to work off of, and a Lead Programmer you would work under.
Assignments may include:
-Creating new scripts of varying degrees specific to the project (mostly server-side, but sometimes client-side)
-Assembling already created monsters/characters with existing or non-existing code.
-Creating VFX
This project is unpaid, but with royalties.

---