473,327 Members | 1,896 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Allocating a continuous block of memory for a structure?

I have a structure that has a byte array as a field:
struct foo
{
public int A;
public int B;
public byte[] C;
}

I want to create an instance of this structure, such that all fields
A, B, and C are in a continous block of memory. Right now if I assign
C a new byte array, it gets created in a different spot in memory than
fields A and B.
Is there any way i can do this?

Oct 27 '07 #1
2 2096
"Kourosh" <kd*********@msn.comwrote in message
news:11**********************@i13g2000prf.googlegr oups.com...
>I have a structure that has a byte array as a field:
struct foo
{
public int A;
public int B;
public byte[] C;
}

I want to create an instance of this structure, such that all fields
A, B, and C are in a continous block of memory. Right now if I assign
C a new byte array, it gets created in a different spot in memory than
fields A and B.
Is there any way i can do this?


Not sure why you need this for, anyway, the only possible way is to declare
the array of fixed size in an unsafe block.

unsafe struct foo
{
public int A;
public int B;
fixed public byte[128] C; // embedded array of 128 bytes
}

Note both requirement; fixed size and unsafe.

Willy.

Oct 27 '07 #2
Willy Denoyette [MVP] <wi*************@telenet.bewrote:

<snip>
Not sure why you need this for, anyway, the only possible way is to declare
the array of fixed size in an unsafe block.

unsafe struct foo
{
public int A;
public int B;
fixed public byte[128] C; // embedded array of 128 bytes
}

Note both requirement; fixed size and unsafe.
Also note that this is only available as of C# 2.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 27 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Jakob Bieling | last post by:
Hi, Whenever allocating memory using operator new or operator new I feel like I should only use it very sparingly, because otherwise memory is wasted (by additional overhead needed to manage all...
11
by: fivelitermustang | last post by:
Actually, how would I go about allocating a four-dimensional dynamic array? I only know how to make two dimensional dynamic arrays: double **v; v = new double*; for (int i=0; i<C; i++) { v =...
4
by: Sameer | last post by:
Hello Group, This is one problem in programming that is troubling me. there is a segmentation fault just before creating memory to a structure ..i.e, just after the "allocating memory "...
14
by: Gattaca | last post by:
I would like to create a matrix of integers by allocating memory dynamically (malloc or calloc) because i and j are defined during execution of the program. I have got not problem to do this in...
7
by: boss_bhat | last post by:
Hi all , I am beginner to C programming. I have a defined astructure like the following, and i am using aliases for the different data types in the structure, typedef struct _NAME_INFO {...
94
by: smnoff | last post by:
I have searched the internet for malloc and dynamic malloc; however, I still don't know or readily see what is general way to allocate memory to char * variable that I want to assign the substring...
3
by: Amit_Basnak | last post by:
Dear Friends I have the follwoing function "tss_fe_get_own_info" which has the arguments as shows below tss_fe_get_own_info(char *user_id, tss_user_profile_t **p_buf_UserSecInfo, error_status_t...
8
by: Win Sock | last post by:
Hi All, I need one clarification regarding memory allocation for structure. The details are as given below : let us consider one structure struct { uit32 len0; uint8 *pointer0; uit32 len1;
10
by: Chris Saunders | last post by:
Here is the declaration of a struct from WinIoCtl.h: // // Structures for FSCTL_TXFS_READ_BACKUP_INFORMATION // typedef struct _TXFS_READ_BACKUP_INFORMATION_OUT { union { //
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.