How to check the boundary of pointer? 
Author Message
 How to check the boundary of pointer?

Hi, anybody
    I have two pointer:

//.............................//
char* str1= strdup("123");
char* str2= strdup("456");
.......................
// the address of str1 is 0x00411b40;
// the address of str2 is 0x00411b70;
// now if I do following:

strcpy(str1,"123456789012345678901234567890123456789012345678901234567890");
delete str1; // or delete str2;
// application crashed.

I think if when we allocate memery for a pointer, but not give it enough
memery space, the problem will ocur.

I wonder if there is any way to check the boundary of a pointer, so we can
avoid
address access violation.????????????

    Anybody's advice is welcome!!!
Best Regards.
    Jay.



Sat, 07 Feb 2004 23:11:11 GMT  
 How to check the boundary of pointer?
You can use strlen() to determine the length of the string.

You should call free() (not delete) on strings allocated with _strdup().

See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcco...

Quote:

> Hi, anybody
>     I have two pointer:

> //.............................//
> char* str1= strdup("123");
> char* str2= strdup("456");
> .......................
> // the address of str1 is 0x00411b40;
> // the address of str2 is 0x00411b70;
> // now if I do following:

> strcpy(str1,"123456789012345678901234567890123456789012345678901234567890");
> delete str1; // or delete str2;
> // application crashed.

> I think if when we allocate memery for a pointer, but not give it enough
> memery space, the problem will ocur.

> I wonder if there is any way to check the boundary of a pointer, so we can
> avoid
> address access violation.????????????

>     Anybody's advice is welcome!!!
> Best Regards.
>     Jay.



Sat, 07 Feb 2004 11:45:57 GMT  
 How to check the boundary of pointer?


Quote:
> Hi, anybody
>     I have two pointer:

> //.............................//
> char* str1= strdup("123");
> char* str2= strdup("456");
> .......................
> // the address of str1 is 0x00411b40;
> // the address of str2 is 0x00411b70;
> // now if I do following:

strcpy(str1,"123456789012345678901234567890123456789012345678901234567890");

Quote:
> delete str1; // or delete str2;
> // application crashed.

> I think if when we allocate memery for a pointer, but not give it enough
> memery space, the problem will ocur.

> I wonder if there is any way to check the boundary of a pointer, so we can
> avoid
> address access violation.????????????

>     Anybody's advice is welcome!!!
> Best Regards.
>     Jay.

In most cases you allocate the memory associated with a pointer yourself and
thus simply need to keep a record of how much you allocated.

In this case, strdup allocates the memory. The documentation isn't explicit,
but in all such cases you should assume that only the minimum amount of
memory has been allocated. In this case, that means you should assume that
only enough memory has been allocated for three characters (plus the
terminating '\0').

--

Quixote
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)



Sat, 07 Feb 2004 17:18:51 GMT  
 How to check the boundary of pointer?


Quote:
> I wonder if there is any way to check the boundary of a pointer, so we can
> avoid
> address access violation.????????????

>     Anybody's advice is welcome!!!
> Best Regards.
>     Jay.

The simple solution is to write correct code...  However, there are
situations where you actually do need to know (or at least it is nice to
know) if the memory that a pointer is pointing to is valid and able to be
read or written to.  For this, the Win32 API provides the functions
IsBadReadPtr() and IsBadWritePtr().

I personally use the functions mostly in debug level code that triggers
assertions in my debug builds in order to easily localize problems.  In
addition, in some of the COM components that I have created, the functions
have been pretty valuable for protecting the component.  In general though,
it is more important to understand the C and C++ languages and memory
management than it is to learn about all of the tools that a specific
platform may provide.  The reason for this is so that you don't use such
tools as a crutch to help bad code limp along.

Anecdote:  I once answered a similar question for a junior level programmer
and she ended up using the above functions in nearly all of her code that
delt with pointers rather than nulling out invalid pointers and simply
checking for the null reference.  Not only was the code considerably slower
than the equivelent "correct" code, it was also much harder to follow.

-P



Sun, 08 Feb 2004 10:33:34 GMT  
 
 [ 4 post ] 

 Relevant Pages 

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

2. pointers & lower boundary

3. Re : Interface pointers over process boundaries

4. aligning a pointer on DWORD boundaries ???

5. Pointers outside of array boundary

6. Checking for NULL pointers

7. check for pointer or array

8. Please check my Linked Lists and Pointers

9. HELP:How to check if pointer is free?

10. Checking Pointer Allocation in Release Build

11. Check for bad pointer

12. How to check pointers before causing a segfault?

 

 
Powered by phpBB® Forum Software