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

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

> 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

> 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

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

> 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

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

 Page 1 of 1 [ 4 post ]

Relevant Pages