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 6241535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:47:05+00:00 2026-05-24T11:47:05+00:00

I do not work with C directly much and a basic language nuance just

  • 0

I do not work with C directly much and a basic language nuance just set me back and I would like to understand it better so I can avoid it in the future.

I am using the Queue implementation found at the following link.

http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=9202&lngWId=3

It’s a pretty common queue where values are pushed and popped. (FIFO) But for some reason the last value that I push onto the queue sets all values in the queue with the same value.

I started with the following code.

Queue q;
queue_init(&q);
for (int i=0;i<10;i++) {
    MyData *in_md;
    in_md->mNumber = i;
    queue_push(&q, in_md);

    MyData *h = (MyData *)q.head->data;
    MyData *t = (MyData *)q.tail->data;

    NSLog(@"in_md: %i", in_md->mNumber);
    NSLog(@"h->mNumber: %i", h->mNumber);
    NSLog(@"t->mNumber: %i", t->mNumber);
    if (q.head->link) {
        MyData *l = (MyData *)q.head->link->data;
        NSLog(@"l->mNumber: %i", l->mNumber);
    }
}

But I found that when I popped the values off the number value was 9 for each instance. I thought I had some pointer bug and tried to fix it for a while. I thought that since I declared in_md inside of the block of the for loop it would be a unique instance with a unique memory address. Eventually updated the code to the following…

Queue q;
queue_init(&q);
for (int i=0;i<10;i++) {
    void *data = malloc(sizeof(MyData)); // key change
    MyData *in_md = (MyData *)data;
    in_md->mNumber = i;
    queue_push(&q, data);

    MyData *h = (MyData *)q.head->data;
    MyData *t = (MyData *)q.tail->data;

    NSLog(@"in_md: %i", in_md->mNumber);
    NSLog(@"h->mNumber: %i", h->mNumber);
    NSLog(@"t->mNumber: %i", t->mNumber);
    if (q.head->link) {
        MyData *l = (MyData *)q.head->link->data;
        NSLog(@"l->mNumber: %i", l->mNumber);
    }
}

Now the ensures that a new instance is created because malloc is used and I cast it to a variable I can set the int value. This creates the output appears as I expect.

I assume this is how C is supposed to work but I did not expect it. Normally in other languages like Java, C# and even JavaScript declaring a variable within a block would create a new instance of the variable which is what I expected here. But that is not the case.

What is happening in this case?

  • 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-24T11:47:06+00:00Added an answer on May 24, 2026 at 11:47 am

    In your code:

    You created a pointer:

    MyData *in_md;
    

    and without reserving memory for that pointer (that’s what the malloc does in your second code block) you assing values to it’s members:

    in_md->mNumber = i;
    

    You were basically accessing undefined memory. In your case the value was ‘9’ but you could have gotten an application crash as well.

    In your second code block you allocated memory for that pointer and then stored it in the queue – that’s the right way.

    Keep in mind that you need to deallocate the memory using the free() method before you finally release the queue.

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

Sidebar

Related Questions

Is there any reason something like this would not work? This is the logic
I'm trying to understand why this does not work. (Basic example with no validation
I would like to have external proofreaders to work directly inside my Drupal site.
I am trying to understand why ZendX_JQuery will not work when used directly in
IIS does not work when I start applications like Skype since it also uses
Is there any reason why JQuery would not work when used in an ASPX
How can allow this page to run on IIS server... It does not work
py2exe does not work with the standard email module Hello. I am trying to
The following method does not work because the inner block declares a variable of
Why does the following code not work as I was expecting? <?php $data =

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.