pointers & lower boundary 
Author Message
 pointers & lower boundary

Hi,
I have written a C function that parse an array of character
recursively in increasing value order:

int increase (char *s)
{
int tmp;
if (*(s+1) == '\0')
 return 0;
else {
 if (*s <= *(s+1)) {
  increase(s+1);
  }
 else {
  tmp = *(s+1);
  *(s+1) = *s;
  *s = tmp;
  increase (s-1);
  }
 }

Quote:
}

As you can see, if the n+1-th character is smaller than the n-th, the
function exchange their values and reinvoke itself one step back. What I
would like to understand is why it does not go below the 0 index
boundary?
For example, if s[0] > s[1] and s=&s[0], the last command of the second
else block, increase(s[-1]), is performed. I was expecting a core dump.
Is there a protective mechanism?
Thanks for your answer,
Edouard

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Fri, 20 Sep 2002 03:00:00 GMT  
 pointers & lower boundary
accessing the -1 element of the array may or may not cause a seg. fault
depending on the organization of your program.
For instance, if there was other data already on the heap when the 'char *s'
was malloc'd, you may actually be accessing another variable that was
created elsewhere in the program.  Say the crude figure below is the heap:

            .......[abcdefgh][ijklmnop]........

Say the first bracketed section is another char* pointer that you malloc'd
elsewhere in the program, and the second
bracketed section is the 'char* s' pointer you are passing to increase.
Then, s[0] == i, s[1] == j, s[2] == k, and s[-1] == h. Since there is no
array bounds checking in C (even in the negative direction it seems) this
memory access is perfectly legal, since s[-1] is actually allocated to your
process.  If there were no malloc's executed before 'char* s' was malloc'd,
then you should get a seg-fault for trying to access the system portion of
your process's address space.

Does this make sense?

Eric Petroelje

Quote:

> Hi,
> I have written a C function that parse an array of character
> recursively in increasing value order:

> int increase (char *s)
> {
> int tmp;
> if (*(s+1) == '\0')
>  return 0;
> else {
>  if (*s <= *(s+1)) {
>   increase(s+1);
>   }
>  else {
>   tmp = *(s+1);
>   *(s+1) = *s;
>   *s = tmp;
>   increase (s-1);
>   }
>  }
> }

> As you can see, if the n+1-th character is smaller than the n-th, the
> function exchange their values and reinvoke itself one step back. What I
> would like to understand is why it does not go below the 0 index
> boundary?
> For example, if s[0] > s[1] and s=&s[0], the last command of the second
> else block, increase(s[-1]), is performed. I was expecting a core dump.
> Is there a protective mechanism?
> Thanks for your answer,
> Edouard

> Sent via Deja.com http://www.deja.com/
> Before you buy.



Fri, 20 Sep 2002 03:00:00 GMT  
 pointers & lower boundary

Quote:

> accessing the -1 element of the array may or may not cause a seg. fault
> depending on the organization of your program.
> For instance, if there was other data already on the heap when the 'char *s'
> was malloc'd, you may actually be accessing another variable that was
> created elsewhere in the program.  Say the crude figure below is the heap:
>             .......[abcdefgh][ijklmnop]........
> Say the first bracketed section is another char* pointer that you malloc'd
> elsewhere in the program, and the second
> bracketed section is the 'char* s' pointer you are passing to increase.
> Then, s[0] == i, s[1] == j, s[2] == k, and s[-1] == h. Since there is no
> array bounds checking in C (even in the negative direction it seems) this
> memory access is perfectly legal, since s[-1] is actually allocated to your
> process.  If there were no malloc's executed before 'char* s' was malloc'd,
> then you should get a seg-fault for trying to access the system portion of
> your process's address space.
> Does this make sense?

No, it's nonsense.  There's nothing "perfectly legal" about that access.
The only thing it shows is that you're using a platform that doesn't
catch this form of undefined behavior.

The fact that C doesn't require array bounds checking doesn't mean that
an implementation cannot.  Accessing past either end of an array results
in undefined behavior, and may do as your example suggests, or a
segmentation fault, or a number of other things.



Fri, 20 Sep 2002 03:00:00 GMT  
 pointers & lower boundary
Hello.

This program is so similar to sorting algorithm "shaker sort".
But I am regretful how to use a pointer to char.
Shaker sort always needs range of index.
But in this function "increase", an information of range will be lost.
Pointer "s" should point at the beginning of string through.
I think it is better to add a new parameter instead of "s" as follow:

#include <stdio.h>
#include <stdlib.h>

int increase(char *, char *);

int main()
{
  char s[] = "91324";

  increase(s, s);
  printf("%s\n", s);

  getchar();
  return EXIT_SUCCESS;

Quote:
}

int increase(char *s, char *t)
{
  int  tmp;

  if (*(t + 1) == '\0') return 0;
  else if (*t <= *(t + 1))
    return increase(s, t + 1);
  else {
    tmp = *(t + 1);
    *(t + 1) = *t;
    *t = tmp;
    if (s == t) return increase(s, t + 1);
    return increase(s, t - 1);
  }

Quote:
}

Thanks.
--
Takashi Itoh



Sat, 21 Sep 2002 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Re : Interface pointers over process boundaries

2. aligning a pointer on DWORD boundaries ???

3. How to check the boundary of pointer?

4. How to check the boundary of a pointer????????

5. Pointers outside of array boundary

6. strings: lower & upper

7. Select returning UPPER & LOWER CASE

8. Low level pointer manipulation

9. Low level pointer manipulation (MS-DOS)

10. Using low bits of pointers -- portably

11. pointers&&structures

12. Pointer && mmap on Linux help

 

 
Powered by phpBB® Forum Software