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

  • SEARCH
  • Home
  • 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 6030899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:06:21+00:00 2026-05-23T05:06:21+00:00

suppose I have a structure. struct node { string info; node link*; } what

  • 0

suppose I have a structure.

struct node {
 string info;
 node link*;

}

what is the difference between

node k;
node b;

k.info = "string";
b.info = "string";
k.link = &b;

and

node *k;
node *b;
k = new node;
b = new node;    
k->info = "string";
b->info = "string";
k->link = b;

in terms of memory allocation? Are both examples correct and create a proper linked list? Added: In most books the second example is used, why is that? Is there a down side to using the first example?

  • 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-23T05:06:22+00:00Added an answer on May 23, 2026 at 5:06 am

    Yes. both are technically correct.

    In the first example, the memory is on the stack, in the second on the heap.

    When the memory is on the stack, the “compiler” is in charge of freeing the memory (when variables go out of scope). Since your linked list can outlive the variables you explicitly created, this can cause all sorts of problems. For example, in the first example, if b goes out of scope, but k doesn’t, then k will have a dangling pointer, which leads to undefined behavior (aka bad).

    When the memory is on the heap, you have to manage the memory, which can cause problems if you forget to free it. This is harder then it sounds, considering exceptions. I suggest you use some form of a smart pointer. This is the most common and safest way to use a linked list.

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

Sidebar

Related Questions

Suppose I have my node structure as: struct Employee { int age; int salary;
Suppose I have a structure in C or C++, such as: struct ConfigurableElement {
Suppose I have the following (desired) folder structure: *CommonProject *Project#1 ----> CommonProject(link) *Project#2 ---->
Suppose I have a structure, and a pointer to a memory location p, how
I have a structure, that contain string. Something like that: struct Chunk { int
Suppose I have a structure: typedef struct { float x; float y; float z;
Suppose I have a structure in C++ containing a name and a number, e.g.
suppose you have the following structure: #include <windows.h> // BOOL is here. #include <stdio.h>
Suppose I have code that maintains a parent/children structure. In such a structure I
Suppose I have a stringbuilder in C# that does this: StringBuilder sb = new

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.