need a pair of eyes - pointer

Go To Last Post
7 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Guys, I have two functions.  The first function, "retrieve_small_leaf1" return the pointer to the last value in a array of Nodes that is not 0.  Note, the array is sorted!

 

The Second function is supposed to return the second last value.  Because the list is sorted it will not be zero.

 

The functions are thus:

static struct Node * retrieve_small_leaf1(struct Node * array){

	for(int i = SubTrees; ((i != 0) && (array->value == 0)); i--, array--);

	return array;
}

static struct Node * retrieve_small_leaf2(struct Node * array){

	for(int i = SubTrees; ((i != 0) && (array->value == 0)); i--, array--);

	printf("\n\rtest %c", array->marker);
	array--;
	printf("\n\rtest %c\n\r", array->marker);

	return array;
}

Function one returns correctly.

Function two however returns garbage.

 

I'm doing a quick pointer decrement in function two that should point to next value.  

 

EDITED, I'm passing the end pointer to the array, i'm trying to find the next non zero value in the array.  For example, the array could be, 12, 10, 5, 4, 4, 1, 0, 0, 0.

 

function one should return 1.

function two should return 4.

 

 

This topic has a solution.
Last Edited: Wed. Nov 27, 2019 - 07:38 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


run the following test:

static struct Node * retrieve_small_leaf2(struct Node * array){

	for(int i = SubTrees; ((i != 0) && (array->value == 0)); i--, array--);

	printf("\n\rtest %p", array);
	array--;
	printf("\n\rtest %p\n\r", array);

	return array;
}

and I got the resut:

 

The structure of Node is thus:

 

struct Node{

    unsigned char type;

    unsigned int  value;

    unsigned char marker;



    struct Node * left;

    struct Node * right;

};

 

So it decremented 14Hex and not 14 Decimal.  Why?

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You can use an online compiler-

https://godbolt.org/z/yP46XJ

https://godbolt.org/z/pSrRXW

 

Then everyone can play along.

Last Edited: Wed. Nov 27, 2019 - 07:17 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Once again, your program worked for me. smiley

 

This is in Visual Studio; it prints:

 

test 5
test 4

#include "stdafx.h"

struct Node {
	unsigned char type;
	unsigned int  value;
	unsigned char marker;
	struct Node * left;
	struct Node * right;
};

struct Node NodeArray[] = {
	{ 0, 12, '0', NULL, NULL },
	{ 0, 10, '1', NULL, NULL },
	{ 0, 5,  '2', NULL, NULL },
	{ 0, 4,  '3', NULL, NULL },
	{ 0, 4,  '4', NULL, NULL },
	{ 0, 1,  '5', NULL, NULL },
	{ 0, 0,  '6', NULL, NULL },
	{ 0, 0,  '7', NULL, NULL },
};

const int SubTrees = sizeof NodeArray / sizeof(*NodeArray);

static struct Node * retrieve_small_leaf1(struct Node * array)
{

	for (int i = SubTrees; ((i != 0) && (array->value == 0)); i--, array--);

	return array;
}

static struct Node * retrieve_small_leaf2(struct Node * array)
{

	for (int i = SubTrees; ((i != 0) && (array->value == 0)); i--, array--);

	printf("\n\rtest %c", array->marker);
	array--;
	printf("\n\rtest %c\n\r", array->marker);

	return array;
}

int main(int argc, char* argv[])
{
	struct Node *ar = retrieve_small_leaf1(&NodeArray[SubTrees - 1]);
	retrieve_small_leaf2(ar);

	return 0;
}

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Fianawarrior wrote:

So it decremented 14Hex and not 14 Decimal.  Why?

Alignment probably eg. the int and pointers are aligned to 4 byte boundary.

Try it as

struct Node{
    unsigned char type;

    unsigned char marker;

    unsigned int  value;
    struct Node * left;
    struct Node * right;
};

see if it makes any difference

(I would just print out  sizeof(struct Node))

 

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi Winterbottom, your code also works for me.  Think I'll scratch the function and restart.

 

Thanks for taking the time to test that code....

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm an eejit.  found the problem.  Its that stupid I'll keep it to myself...  lmfao