Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3407028
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:42:02+00:00 2026-05-18T05:42:02+00:00

I created a structure pointer, but each call for the new node returns the

  • 0

I created a structure pointer, but each call for the new node returns the same address. But i expected different address to be returned for each invocation of the new node. Can someone please help?

public unsafe struct Node
{
    public int Data;
}
class TestPointer
{
    public unsafe Node* getNode(int i)
    {
        Node n = new Node();
        n.Data = i;
        Node* ptr = &n;
        return ptr;
    }
    public unsafe static void Main(String[] args)
    {
        TestPointer test = new TestPointer();
        Node* ptr1 = test.getNode(1);
        Node* ptr2 = test.getNode(2);
        if (ptr1->Data == ptr2->Data)
        {
            throw new Exception("Why?");
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T05:42:03+00:00Added an answer on May 18, 2026 at 5:42 am

    Don’t be fooled by the Node n = new Node(); syntax! Node being a struct, n is allocated on the stack. You call getNode twice from the same function in the same environment, so naturally you are getting two pointers to the same stack location. What’s more these pointers become invalid (‘dangling’) once getNode returns, because the stack location that belonged to n may be overwritten by a different call. In short: don’t do it. If you want CLR to allocate memory, make Node a class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a hierarchical structure stored in a table. Each element has a pointer
I'd like to create a tree structure from JSON, but with multiple columns that
I created this 2D Qlist. Code works but when I consider a bit deeper
I need to create a structure or series of strings that are fixed lenght
I have the following table structure CREATE TABLE `table` ( `id` int(11) NOT NULL
I need to create a db structure where Supplier inherits from Client. I used
Given a table structure like this: CREATE TABLE `user` ( `id` int(10) unsigned NOT
I have a structure which I create a custom constructor to initialize the members
I need to create an XML schema that validates a tree structure of an
I'm trying to create routes which follow the structure of a tree navigation system,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.