• 0

[c] Heap Question


Question

i tried really hard to avoid posting this question but i cant seem to spot the problem :(

so here it is..

basically i want to print the highest number of 'search_counts' from a heap.

> there is an array of struct HEAP

> there is only 1 element in HEAP , it is DATA *dataPtr , basically it points to a struct DATA (which has been allocated memory dynamically)

> i have to delete the top most item from the heap ary and print it and then reheapDown to get it back to a heap form.

problem: it prints all but the last element correctly. for the last element it prints out garbage. :(

i have been through the debugger so many times but yet it failed.

here is the code

int j is the count of how many elements were inserted into the heap ary

int whatFn is basically a checker to see that if the number of elements inserted was less than the HEAP_SIZE which is 7 then use j otherwise use HEAP_SIZE for the restrictor

void printHeap1 (HEAP *ary, int j, int whatFn)
{
	int i = 0;
	HEAP *pWalker, *pCur, *pLast, dataOut;

	pCur = ary;

	printf("--- 7 Most popular searches ---\n");

	if (whatFn == NOT_EQUAL)
	{
  pLast = &ary[j];
  j--;
  for (pWalker = pCur; pWalker < pLast; pWalker++)
  {
 	 if (deleteHeap (ary, &j, &dataOut) == 1)
    printToScreen (&dataOut, "heap");
 	 else
    printf("Unable to delete node\n");
  }
	}
	else
	{
  pLast = &ary[HEAP_SIZE -1];
  j--;
  for (pWalker = pCur; pWalker < pLast; pWalker++)
  {
 	 if (deleteHeap (ary, &j, &dataOut) == 1)
    printToScreen (&dataOut, "heap");
 	 else
    printf("Unable to delete node\n");
  }
	}
	return;
}

this goes to the delete heap function that is

int deleteHeap (HEAP *heap, int *last, HEAP *dataOut)
{
//	int item;

	if (*last < 0)
	{
  return 0;
	}

	dataOut->dataPtr = heap[0].dataPtr;
	heap [0].dataPtr = heap [*last].dataPtr;
//	heap[*last].dataPtr = NULL;
	(*last)--;

	reheapDown (heap, 0, *last);

	return 1;
}

this later goes to the reheapDown function which is

void reheapDown (HEAP *heap, int root, int last)
{
	DATA hold, leftKey, rightKey, largeChildKey;
	int largeChildIndex = 0;
	//int parent;

	if ((root * 2 + 1) <= last)
	{
  leftKey = *(heap [root * 2 + 1].dataPtr);

  if ((root * 2 + 2) <= last)
  {
 	 rightKey = *(heap [root * 2 + 2].dataPtr);
  }
  else
  {
 	 rightKey.search_count = -1;
  }

  if (leftKey.search_count > rightKey.search_count)
  {
 	 largeChildKey = leftKey;
 	 largeChildIndex = root * 2 + 1;
  }
  else
  {
 	 largeChildKey = rightKey;
 	 largeChildIndex = root * 2 + 2;
  }

  if (heap[root].dataPtr->search_count < heap [largeChildIndex].dataPtr->search_count)
  {
 	 hold = *(heap [root].dataPtr);
 	 heap [root].dataPtr = heap [largeChildIndex].dataPtr;
 	 heap [largeChildIndex].dataPtr = &hold;

 	 reheapDown (heap, largeChildIndex, last);
  }
	}
	return;
}

any form of help is appreciated. i just need someone to spot the error. i can fix the rest myself

thanks :)

Link to comment
https://www.neowin.net/forum/topic/52712-c-heap-question/
Share on other sites

7 answers to this question

Recommended Posts

  • 0

You like them? Geez, how masochistic is that? :D

My personal opinion is that pointers:

1. Make code practically unreadable.

2. Rarely increase performance.

3. Don't really allow you to do anything that you couldn't already do.

4. Make it easy to introduce bugs.

I'm not denying that there are situations where pointers are absolutely necessary, but for the above reasons I would rather not use them. Thankfully, C# (my language of choice) allows you to avoid using pointers altogether (unlike C and C++). It still has them, mind you, but you are heavily discouraged from using them.

:alien:

Link to comment
https://www.neowin.net/forum/topic/52712-c-heap-question/#findComment-526933
Share on other sites

  • 0

i know it sounds crazy but once u get a hang of pointers it aint that bad.. :)

i havent tried C# yet... i will be doing C++ in jan and i already know c and beginning java and a little of actionscripting..

as u know, java and actionscripting dont have pointers (well, the do have them but they are hidden)

and its all cool but i still feel i have more control with the use of pointers.. i feel C and C++ form a stronger combo than what java and its rich APIs could present :)

its just my opinion derived from my 1 yr of programming experience :)

Link to comment
https://www.neowin.net/forum/topic/52712-c-heap-question/#findComment-527998
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • It's expensive, but I bet it will still sell out just like the Steam Deck and the Steam Controller
    • This is listed as a preview in WU for me. I usually don't grab them as previews; what's your opinion on it so far?
    • Pale Moon 34.3.1 by Razvan Serea Pale Moon is an Open Source, Goanna-based web browser available for Microsoft Windows, Linux and Android, focusing on efficiency and ease of use. Make sure to get the most out of your browser! Pale Moon offers you a browsing experience in a browser completely built from its own, independently developed source that has been forked off from Firefox/Mozilla code, with carefully selected features and optimizations to improve the browsers speed, resource use, stability and user experience, while offering full customization and a growing collection of extensions and themes to make the browser truly your own. Features: Optimized for modern processors Based on proprietary optimized layout engine (Goanna) Safe: forked from mature Mozilla code and regularly updated Secure: Additional security features and security-aware development Supported by our user community, and fully non-profit Familiar, efficient, fully customizable interface Support for full themes: total freedom over any elements design Support for easily-created lightweight themes (skins) Smooth and speedy page drawing and script processing Increased stability: experience fewer browser crashes Support for many Firefox extensions Support for a growing number of Pale Moon exclusive extensions Extensive and growing support for HTML5 and CSS3 Many customization and configuration options Pale Moon 34.3.1 changelog: Pale Moon will now exclude local resources from CSP checks, aligning it with the rest of CSP handling. Fixed an issue where the devtools JSON viewer would, in some cases, make erroneous requests to remote servers. Updated libpng to 1.6.58+apng. Updated NSS to 3.90.12 (UXP), addressing multiple security issues. Fixed several intermittent and rare crashes. Security issues addressed: CVE-2026-12318 (CWE-125), CVE-2026-12322, CVE-2026-12292 (DiD), and multiple other issues that did not have a CVE designation at the time of patching. Download: Pale Moon (64-bit) | Portable 64-bit | ~40.0 MB (Freeware) Download: Pale Moon (32-bit) | Portable 32-bit Links: Pale Moon Homepage | Add-ons | Themes | Extensions | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
  • Recent Achievements

    • Grand Master
      Jaybonaut went up a rank
      Grand Master
    • One Year In
      Philsl earned a badge
      One Year In
    • Dedicated
      Scoobystu earned a badge
      Dedicated
    • First Post
      Tom Schmidt earned a badge
      First Post
    • One Month Later
      D0nn13 earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      416
    2. 2
      +Edouard
      179
    3. 3
      PsYcHoKiLLa
      124
    4. 4
      Michael Scrip
      77
    5. 5
      Xenon
      76
  • Tell a friend

    Love Neowin? Tell a friend!