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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:50:13+00:00 2026-05-27T08:50:13+00:00

Question: what is causing these values to change randomly? Info: My Code: int q

  • 0

Question: what is causing these values to change randomly?


Info:

My Code:

int q = nonZero;
for(j = nonZero;j>1;j--)
{
    printf("Top: %i %i\n",j,q);
    qsort(tree,j,sizeof(Node),cmp);
    printf("Bottom: %i %i\n",j,q);
    Node t = {tree[0],tree[1],-1};
    tree[0] = &t;
    tree[1] = tree[j];
    tree[j] = NULL;
}

Not a complicated little program. make a node out of the top two nodes on the tree, make that the new top node, resort the array, repeat. I added ‘q’ in as a debug value, I’m completely clueless as to what exactly is happening. If I try and have it run, j typically starts at 73, which it should, the expected is that it would then be 72,71,70,69,68…3,2 and that q will remain 73 indefinitely. here’s my output though:

Top: 73 73
Bottom: -796584576 32767
Segmentation fault

but thats not all, w/o recompiling, i got:

Top: 73 73
Bottom: 0 0
Segmentation fault

it consistently would give me one of those two outputs. after several runs I saw that 32767 never changed, the q value, but the j value, here -796584576, was always some different absurdly large negative number. anyone have any idea why on earth it would seem as if qsort was changing my j value, as well as what seems to be a completely unrelated value of q?

  • 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-27T08:50:14+00:00Added an answer on May 27, 2026 at 8:50 am

    The qsort() call suggests that tree[] is an array of Nodes, but the statement tree[0] = &t; rather suggests that it is an array of pointers. That being then case then:

    qsort( tree, j, sizeof(&Node), cmp ) ;
    

    or better

    qsort( tree, j, sizeof(*tree), cmp ) ;
    

    the latter ensures the size is the sizeof whatever tree is an array of regardless of its type and is therefore safer and more maintainable.

    Assigning the address of t to a variable with greater scope than t is also doomed.

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

Sidebar

Related Questions

What is causing some browsers to see my code as unsolicited? I have a
Whilst running test code from this question and fiddling with the JVM's thread stack
Question as stated in the title.
Question is pretty self explanitory. I want to do a simple find and replace,
Question Alright, I'm confused by all the buzzwords and press release bingo going on.
Question in the title. And what happens when all 3 of $_GET[foo] , $_POST[foo]
Question Can I build a image database/library that has an e-commerce style checkout system
Question says it all, really. My application is a time tracker. It's currently written
Question Using XSLT 1.0, given a string with arbitrary characters how can I get
Question: ((lambda (x y) (x y)) (lambda (x) (* x x)) (* 3 3))

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.