473,659 Members | 3,631 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Memory address of an object

I want to get the memory address of an object. Can someone tell me how
to do that in C#. Also, I tried using the unsafe and the "&" operator
and could not acheive it. Can you tell me a good way of doing it? I am
new to this and have no clue how to get the memory address.

Nov 17 '05 #1
37 21658
I want to get the memory address of an object.


Why do you need it? In the garbage collected world of .NET it may
change from one moment to the next.
Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 17 '05 #2
Could you explain what are you trying to do. Why would you need "memory
address".

"kp******@gmail .com" wrote:
I want to get the memory address of an object. Can someone tell me how
to do that in C#. Also, I tried using the unsafe and the "&" operator
and could not acheive it. Can you tell me a good way of doing it? I am
new to this and have no clue how to get the memory address.

Nov 17 '05 #3
We are in someway trying to create a unique id. Though I know we could
use Guid, my supervisors are advising me to use a logic to do it,
whereby the memory address of that instance of a class (object) is used
in this logic. Please help.

Nov 17 '05 #4
kp******@gmail. com wrote:
We are in someway trying to create a unique id. Though I know we could
use Guid, my supervisors are advising me to use a logic to do it,
whereby the memory address of that instance of a class (object) is used
in this logic. Please help.


In .NET, you can pin an object and thus get hold of a pointer to it. I
don't know exactly what kind of objects you can pin, but I know you need
to use unsafe code to do it.

What's wrong with a Guid?

--
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:la***@vk arlsen.no
PGP KeyID: 0x2A42A1C2
Nov 17 '05 #5
Unfortunately, your supervisors don't know much about .NET.

If you really want something that is unique for each object, you really
have no choice but to use a GUID (it's the easiest way to create something
that is guaranteed to be unique, and doesn't require a manager of your own
to synchronize generation of the unique values).

You would have to associate that guid with the object, and expose it
somehow. This would be best done through a method on your object itself.
If you store it outside your objects, then you will be holding references to
them (in order to do a lookup for the id based on the object) and that will
extend the lifetime of them (and force you to resort to some gnarly lifetime
management code).

You could use a hash, but that isn't guaranteed to generate a unique id
for each object instance.

Besides creating a unique id, what are you trying to do? What is the id
for?

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

<kp******@gmail .com> wrote in message
news:11******** *************@g 49g2000cwa.goog legroups.com...
We are in someway trying to create a unique id. Though I know we could
use Guid, my supervisors are advising me to use a logic to do it,
whereby the memory address of that instance of a class (object) is used
in this logic. Please help.

Nov 17 '05 #6
"Memory addresses" in .NET aren't really static. Object can be moved around
as part of memory management. You can fix objects in memory, but I don't
think this is really a good solution to your problem.

If there's no other way to uniquely identify your objects, why not have a
static int which is simply incremented each time an object is created and
assigned to the object? Not much different than assigning a Guid in terms of
there not being any particular logic associated with the objects themselves.

Pete
<kp******@gmail .com> wrote in message
news:11******** *************@g 49g2000cwa.goog legroups.com...
We are in someway trying to create a unique id. Though I know we could
use Guid, my supervisors are advising me to use a logic to do it,
whereby the memory address of that instance of a class (object) is used
in this logic. Please help.

Nov 17 '05 #7
Lasse,

This isn't completely correct. You don't need to use unsafe code to pin
an object reference. You can use the GCHandle structure to do it.

Also, this would be a very, very, very, VERY MONUMENTALLY BAD idea. If
you pin the object for a long period of time (and it would have to be, so
that the id remains unique), you will end up fragmenting the heap and make
the GC do WAY more work than it needs to.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Lasse Vågsæther Karlsen" <la***@vkarlsen .no> wrote in message
news:eb******** ************@te lenor.com...
kp******@gmail. com wrote:
We are in someway trying to create a unique id. Though I know we could
use Guid, my supervisors are advising me to use a logic to do it,
whereby the memory address of that instance of a class (object) is used
in this logic. Please help.


In .NET, you can pin an object and thus get hold of a pointer to it. I
don't know exactly what kind of objects you can pin, but I know you need
to use unsafe code to do it.

What's wrong with a Guid?

--
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:la***@vk arlsen.no
PGP KeyID: 0x2A42A1C2

Nov 17 '05 #8


"kp******@gmail .com" wrote:
We are in someway trying to create a unique id. Though I know we could
use Guid, my supervisors are advising me to use a logic to do it,
whereby the memory address of that instance of a class (object) is used
in this logic. Please help.


Niether naturally implementable, nor a good idea. Convince your boss to use
a guid.

The address of an object in the GCed world is mutable and can change with
each successive GC pass. You can pin the address of an object in memory to
keep the GC from moving it, but this is only intended to be used briefly to
sent a pointer to un unmanaged codeblock because doing so will affect the
GCs performance.

As a sidebar question, if a GC pass opens a gap in the heap between the last
movable object and a pinned one with a higher address, does the heap fragment
like in the c/c++ world or is the free memory pointer placed after the pinned
object with the memory between the two temporarily unusable?
Nov 17 '05 #9
How much do your supervisors know about .Net? Did they give you a reason not
to use a Guid?

To use a memory address would be problematic and require unsafe coding. A
Guid is 128 bits in size. Now, the trade-off here is the expense of
resources to get (and use) the memory address, combined with the use of
unsafe code, balanced against using 128 bits of memory. Sounds like a
no-brainer to me. Therefore, I am led to strongly suspect that your
supervisors have no brains.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Big things are made up of
lots of little things.

<kp******@gmail .com> wrote in message
news:11******** *************@g 49g2000cwa.goog legroups.com...
We are in someway trying to create a unique id. Though I know we could
use Guid, my supervisors are advising me to use a logic to do it,
whereby the memory address of that instance of a class (object) is used
in this logic. Please help.

Nov 17 '05 #10

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

Similar topics

1
7887
by: Peter Markowsky | last post by:
Hi all, I've been searching through the Python Essential Reference, and python.org but I can't seem to find a way to get the memory address for an object in python. Please help. How do you get the address of an object in memory? Sincerely, Pete
4
3901
by: | last post by:
Hi I have a list containing several instance address, for example: I'd like to invoke a method on each of these instance but I don't know : 1. if its possible 2. how to proceed
10
5766
by: Dan Elliott | last post by:
I am working on some tricky code and need some help from the experts. I have several large data structures (uBLAS matrices) that must be written to a pre-allocated (by another program) chunk of static memory. Currently our code emulates this behavior using BOOST::serialize archives. According to the documentation, these archive objects will write to a given ostream. We would like to define an ostream that writes to this address without...
3
1764
by: scott.lewis | last post by:
Hi, I'm developing in C++ using Visual Studio 7.1. Is there any way to get the memory address from a referenced parameter? In other words, can I find the pointer value as if it were passed in as a pointer? For example:
4
13712
by: Someonekicked | last post by:
Is it possible to read a memory address with C++; For example, If I run this code first: ************* #include <iostream> using namespace std; void main() { int *zz = new int;
5
45679
by: Jeong-Gun Lee | last post by:
I'm writing a code of writing a value to a specific memory address. ================================================================= #include <stdio.h> int main() { long air; long *air_address;
12
1957
by: FI | last post by:
Hello All, I am relatively new to C programming and I am struck with a problem in dynamic memory allocation. I would like to know if it is ok to pass the 'memory address' returned by malloc()(stored not in a pointer variable but as an element of an array of type int*) directly to free(). My program allocating and freeing memory this way is corrupting the heap. Can anybody guide me on this. Thanks in advance
15
2198
by: polas | last post by:
Hi everyone - I have a question. I am just playing around with C (I realise there are better ways to do what I want, but I would like to do it this way to increase my understanding of C) and would like to read an executable file in to a portion of memory and then pass execution to this and execute the file. However, I can not get it working and my efforts have resulted in a Seg Fault. Below is the code I have got #include "stdio.h"
5
2403
by: erfan | last post by:
vc++6.0,winxp. I use this code to see the memory address and the real value`s address below: #include<stdio.h> int main() { int a=22; int *p; int i; p=&a;
0
8428
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8337
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8628
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7359
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5650
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2754
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1978
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.