help with pointers

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

I have the following routine for checking if a pointer has gone out of bounds and resetting it back to the start.

 

char boudary_configure(struct ff_file * stream, char ** boundary, char * violation){

	// test for boundary violation
	if(((char *)*boundary - stream->directory_base) == cluster_size(stream)){

		if(another_cluster(stream) == 0)
			return 0;

		printf("\n\rViolation Detected!!!");

		*violation = 1;

		// reset working data pointer
		*boundary = (char *)stream->directory_base;
	}
	return 1;
}

And example of its use if like so.

 

struct stream_extension * streamExt;



if(boudary_configure(file, (char **)&streamExt, &violation) == 0)
	break;

 

So if streamExt is at the end of cluster_size then it is reset to the base address.

This topic has a solution.
Last Edited: Tue. Sep 10, 2019 - 05:33 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Your code is not testing for out-of-bounds but rather not at the boundary. So (re-arranging a bit)  ....

char boundary_configure(struct ff_file *stream, char **boundary, char *violation) {
    if (*boundary >= stream->directory_base + cluster_size(stream)) {
        ...
    }
}

 

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

Still no joy, must be something else leading to the failure.

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

You are not giving much info.  In your example streamExt is not initialized so when it's dereferenced as *boundary it can be anything.

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

1. Formally, what you are doing is undefined. It is undefined to re-interpret (type-pun) one pointer type as another pointer type, with only a few exceptions. Moreover, using the `*(T*) &` techinque for type-punning is almost always undefined. C language supports type-punning through unions. Your type-punning technique might work in practice, but this is still bad.

 

2. It is not clear why you are casting `(char *)*boundary`, when `*boundary` by itself is already a `char *`.

 

3. What is the type of `stream->directory_base`? Judging by your code, it is `char *`. In that case, why are you casting it in `*boundary = (char *)stream->directory_base;`?

 

4. Not necessarily a problem, but not all control paths set the value of `*violation`. Is this by design?

 

Fianawarrior wrote:

Still no joy, must be something else leading to the failure.

 

What "failure" are you talking about? Your original question actually contains no question and no mentions of any problems. Then suddenly you are talking about some "failure" without providing any details. What exactly are you talking about?

Last Edited: Tue. Sep 10, 2019 - 04:07 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

When having trouble following pointers, I usually find it helps to step through things in the debugger and use it to view the pointers and see exactly what they're pointing at.

 

Since pointers are a standard 'C' feature, you can generally do this on a PC with some Native toolchain (eg, Visual Studio) - rather than have to mess about with cross-compiling, downloading, and using an embedded target...

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Okay, the first routine was not the problem.  The problem was another pointer also needed reset that I happened to miss.  My first snipped of code was working that I posted at the start.

 

Thanks for the input guys.  Like many bug the devil is in the detail.