Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
I created a simple C# console application that contains this code.

C#
struct CharTest 
{
    char a;
    char b;
}


I expect sizeof(CharTest) to be two times sizeof(char). but surprisingly I see in watch window that It is 2. I don't know why. Can any one tell me what happens?
Posted
Updated 18-Dec-10 0:02am
v2
Comments
JOAT-MON 18-Dec-10 6:07am    
Wrapped code in <pre> tags.
JOAT-MON 18-Dec-10 6:51am    
Doesn't make sense...can you post your test code where you are calling the sizeof() so we can take a look?
mr.abzadeh 18-Dec-10 8:15am    
I did not call sizeof() in my code. I tried to pass a sequential class containing char fields to unsafe code and It didnot work. there is a conflict between ANSI char fields and [StructLayout(LayoutKind.Sequential)] attribute. I need to use type unicode char so that its size shouldnot change and
passing it to unsafe code in DLL make sense. Please refer to my comment 2 for Your EDIT 2

My watch window says:
- sizeof(char) = 2
- sizeof(CharTest) = 4


EDIT-----------------------

Just replicated it by using:
C#
System.Runtime.InteropServices.Marshal.SizeOf ( new CharTest() )


EDIT 2---------------------

InteropServices.Marshal.SizeOf is affected by the CharSet that applies to the struct. By default, C# applies ANSI multple-byte CharSet to types. ANSI multiple-byte will use one or two bytes to store character, and on a new instance of the struct the char variables are initialized to their default value which is "\0" or null, thereby only using one byte.

Because of the default CharSet for C#, this struct does not have a predefined size and the sizeof() can only be used in unsafe block where it apparently calculates the max potential of the struct, whereas Marshal.SizeOf is applied to an instance of the struct and is measuring actual size in use.
 
Share this answer
 
v5
Comments
mr.abzadeh 18-Dec-10 6:27am    
I use Visual studio 2010. what version of VS you use?
JOAT-MON 18-Dec-10 6:40am    
VS 2010
mr.abzadeh 18-Dec-10 7:36am    
You are right. This is the case as in native C++. but c# documentation says that the type 'char' in C# is unicode. I encountered this problem when passing a class or struct to afunction in Native DLL.
.

How can we define unicode char in C# if char is ANSI multibyte?
mr.abzadeh 18-Dec-10 7:57am    
I used this code to pass a class to an unsafe DLL

using System.Runtime.InteropServices;
namespace ns1 {
[StructLayout(LayoutKind.Sequential)]
class Class1 {
.
.
char ch1;
char ch2;
.
.
};

so, ANSI implementation of char conflicts with [StructLayout(LayoutKind.Sequential)] attribute and type 'char' should not be used when passing struct or class to unsafe code because char type in structs or classes is multibyte ansi with predefined size of 1 byte, and changes to 2 bytes when assigning a 2 byte char. It would be a good thing if compiler annonced me for this conflict.

How can I have unicode chars in structs or classes?
JOAT-MON 18-Dec-10 8:19am    
In your [StructLayout()] attribute there are named parameters. One of them is CharSet. Set it to CharSet.Unicode.
Why would you expect 1 when you have two chars defined in the struct?



EDIT ==============


I think it might have something to do with culture settings. Unicode characters are two bytes, and ascii is just 1.

EDIT 2 =============

Well, calling a Marshal.SizeOf on a CharTest object reports a size of 2. Doing a sizeof(char) returns 2. Therefore, I agree with the OP that Marshal.SizeOf would be expected to return 4, but it doesn't.

Here's my code:

C#
public struct MyStruct
{
    public char a;
    public char b;
    public int SizeOf()
    {
        return (sizeof(char) * 2);
    }
}


and then do this:

C#
MyStruct ms = new MyStruct();
ms.a = 'a';
ms.b = 'b';
int sz = System.Runtime.InteropServices.Marshal.SizeOf(ms); // returns 2
int sz2 = ms.SizeOf(); // returns 4


For the record, I'm also using VS2010...

 
Share this answer
 
v5
Comments
mr.abzadeh 18-Dec-10 6:24am    
because sizeof(char) is 2
JOAT-MON 18-Dec-10 7:22am    
Did a little research on it...see EDIT 2 in my answer.
It gives 4 to me for a struct and 2 for a char (unicode). This seems correct to me. I am using .Net 4.0.
 
Share this answer
 
Comments
mr.abzadeh 18-Dec-10 7:37am    
Please refer to Answer 2.

Thanks for your intereset.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900