Sythe.Org Forums     Register     FAQ     Members List     Calendar     Mark Forums Read    
 
Sythe.Org Forums  
   Runescape Gold

Sythe.org — A Virtual Goods Trading Hub

Make real cash! buying and selling in-game items.

We have a no-scam policy.

You can make thousands playing your favourite games here at Sythe.org.

Just sign up an account and follow the rules!


Take me to

Runescape Markets

Other Game Markets

Support Center

Register an Account

Close
Structs are just abstractions (proof)
Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old 12-25-2010, 02:59 PM
Govind's Avatar
Hero
Highly Respected Highly Trusted Sythe Verified User
 
Join Date: Apr 2005
Posts: 7,082
Default Structs are just abstractions (proof)

Proof that structs are just abstractions of contiguous memory:
Code:
#include <stdlib.h>
#include <stdio.h>
typedef struct __s1
{
	int a;
	int b;
} s1;

int main()
{
	long f = 0;
	s1 st;
	st.a = 7;
	st.b = 4;
	memcpy(&f, &st, 4);
	printf("%d\n", f);
	return 0;
}
f = 7.
__________________
I am currently looking for a personal donation of $50.00 via PayPal to start my own website for a project of mine. PM me if you are interested in helping me out.

>> Learn To Make Your Own Bots! No Prior Knowledge Required! <<


<@Verts> theres nothing wrong with playing video games naked occasionally

Reply With Quote
  #2  
Old 12-28-2010, 06:16 AM
Sythe's Avatar
Director
 
Join Date: Apr 2005
Posts: 4,028
Default Re: Structs are just abstractions (proof)

Careful, the padding in a struct is based on the architecture's data alignment.
Reply With Quote
  #3  
Old 12-28-2010, 01:48 PM
Govind's Avatar
Hero
Highly Respected Highly Trusted Sythe Verified User
 
Join Date: Apr 2005
Posts: 7,082
Default Re: Structs are just abstractions (proof)

Would there be padding before the first variable? Besides, no type other than int is used.
__________________
I am currently looking for a personal donation of $50.00 via PayPal to start my own website for a project of mine. PM me if you are interested in helping me out.

>> Learn To Make Your Own Bots! No Prior Knowledge Required! <<


<@Verts> theres nothing wrong with playing video games naked occasionally


Last edited by SMR : 12-28-2010 at 08:14 PM.
Reply With Quote
  #4  
Old 12-28-2010, 10:27 PM
Sythe's Avatar
Director
 
Join Date: Apr 2005
Posts: 4,028
Default Re: Structs are just abstractions (proof)

Quote:
Originally Posted by SMR View Post
Would there be padding before the first variable? Besides, no type other than int is used.
Not usually, but strictly speaking it's up to the compiler.

Examples:
http://msdn.microsoft.com/en-us/libr...=vs.80%29.aspx
Reply With Quote
  #5  
Old 07-08-2011, 12:52 AM
Guru
 
Join Date: Jan 2007
Posts: 1,455
Default Re: Structs are just abstractions (proof)

Yikes SMR, I wouldn't do that except for experimental/demonstration purposes: as was correctly noted above, there is usually no padding before the first variable in a struct, but what if you were on an architecture where an int is 2 bytes, not 4? The result would be (7 << 2 | 4), whatever that works out to.

Last edited by Blondi : 07-08-2011 at 12:57 AM.
Reply With Quote
Reply



Cheap RS Gold Store  Runescape Gold

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

All times are GMT +1. The time now is 09:53 AM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.6.1