473,322 Members | 1,719 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,322 software developers and data experts.

how to reassign a fixed pointer

Hi !

What i want to do in C++ is :

int a[] = new a[2];
a[0] = 0, a[1] = 0;

int* p = &a[0];

while (1) // any condition instead of 1
{
(*p)++;

if (p == &a[0])
p = &a[1];
else
p = &a[0];
}

How is this in C# possible ? I googled and found the unsafe / fixed
statements, but couldnt find
how to reassign the pointer to a new address.

C# :
unsafe {
fixed (int* p = &a[0]) {
while(1)
{
//how can i now reassign the p to another address ?
}
}
--
mfg
Anil Bakirci

Apr 5 '07 #1
3 2562
Here is the definition of the "fixed" key word from the SDK:

The fixed statement prevents the garbage collector from relocating a movable
variable.

Note that the variable is movable, and the fixed pointer only affects the
garbage collector. So, you would move the pointer in the same way.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

"Schwammkopf" <mo********@yahoo.comwrote in message
news:64**********************************@microsof t.com...
Hi !

What i want to do in C++ is :

int a[] = new a[2];
a[0] = 0, a[1] = 0;

int* p = &a[0];

while (1) // any condition instead of 1
{
(*p)++;

if (p == &a[0])
p = &a[1];
else
p = &a[0];
}

How is this in C# possible ? I googled and found the unsafe / fixed
statements, but couldnt find
how to reassign the pointer to a new address.

C# :
unsafe {
fixed (int* p = &a[0]) {
while(1)
{
//how can i now reassign the p to another address ?
}
}
--
mfg
Anil Bakirci

Apr 5 '07 #2
>How is this in C# possible ? I googled and found the unsafe / fixed
statements, but couldnt find
how to reassign the pointer to a new address.
The variable declared in the fixed statement is read-only. But you can
use it to initialize another variable and change that instead.

unsafe {
fixed (int* p = &a[0]) {
int* p2 = p;
while(true)
{
// Work with p2 here
}
}

That said, I fail to see the need for using pointers here at all. If
that's your actual code, it can easily be written to use "safe" code
instead. Something like

int i = 0;
while(<condition>)
{
p[i]++;
i = ++i % 2;
}
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Apr 5 '07 #3
Thx.
No, its just a foo test code.

----- Original Message -----
From: "Mattias Sjögren" <ma********************@mvps.org>
Newsgroups: microsoft.public.dotnet.languages.csharp
Sent: Thursday, April 05, 2007 10:42 PM
Subject: Re: how to reassign a fixed pointer

>
>>How is this in C# possible ? I googled and found the unsafe / fixed
statements, but couldnt find
how to reassign the pointer to a new address.

The variable declared in the fixed statement is read-only. But you can
use it to initialize another variable and change that instead.

unsafe {
fixed (int* p = &a[0]) {
int* p2 = p;
while(true)
{
// Work with p2 here
}
}

That said, I fail to see the need for using pointers here at all. If
that's your actual code, it can easily be written to use "safe" code
instead. Something like

int i = 0;
while(<condition>)
{
p[i]++;
i = ++i % 2;
}
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Apr 5 '07 #4

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

Similar topics

7
by: Shailesh Humbad | last post by:
I have a class and map like this: class MyObject { ... }; map<int, MyObject> Box; Now, I want to reassign the keys of one or more of the pairs in Box based on another map<int, int> that...
2
by: Joshua Kolden | last post by:
STL allocators are templates so that when you write one you are obliged to make it work with any type. However, the Intel IPP library that we use has memory aligned allocators for each of 15...
5
by: Arthur Mnev | last post by:
This is probably beaten to death subject... Does anyone have a good idea of what penalties are for using Fixed statement in c#. On one side it allows for much greater flexibility with casts and...
2
by: coz | last post by:
I created a wrapper class for a dll written in C. Will the following code prevent the "ENTIRE ARRAY" from being moved, not just the first array element? The Wrapper class will be instantiated in...
4
by: taskswap | last post by:
I'm converting an application that relies heavily on a binary network protocol. Within this protocol are a lot of byte arrays of character data, like: public unsafe struct MsgAddEntry {...
1
by: O.B. | last post by:
In the example below, I'm trying to convert a fixed byte array to a string. I get an error about needing to use "fixed" but I have no clue where to apply it. Help? using System; using...
8
by: ms news group | last post by:
What happens if exception is thown within a fixed block? Will the pinned memory buffer get unpinned? and if the pinning pointer points to a managed memery buffer allocated within the throwing...
1
by: Beorne | last post by:
I have a cpp application with this structure: //////////////C++/////////////// typedef struct StatusStructure { char FixedLenString; long LongVariable; double DoubleVariable; BOOL...
2
by: O.B. | last post by:
When using Marshal to copy data from a byte array to the structure below, only the first byte of the "other" array is getting copied from the original byte array. What do I need to specify to get...
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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.