run time Error :(

G

Gizmo

hello all have been trying to write a Mid() function a bit like the one in
vb. i come to compile it and there are no errors however when i run it an
error accours and it says the program has to close. The odd thing is the
error accours after the mid function has done its job. but if i take out the
mid function the error does not accour.

i have found that if i do

cout <<mid(newString.GetString(),iPosition,3);

the erorr will occur. However if i do

cout <<strcpy(new char,mid(newString.GetString(),iPosition,3));

the erorr does not occur.

If i debug it . . im given the error message "Unhandled exception in
Main.exe: 0xc0000005: Access Violation." and it takes me to the part that
its says the error occurs.

The following If statment is where it says it is going wrong (i did not
write this code it must be part of the code that comes with c++)

if (!CheckBytes(pbData(pHead) + pHead->nDataSize,
_bNoMansLandFill, nNoMansLandSize))
_RPT3(_CRT_ERROR, "DAMAGE: after %hs block (#%d) at
0x%08X.\n",
szBlockUseName[_BLOCK_TYPE(pHead->nBlockUse)],
pHead->lRequest,
(BYTE *) pbData(pHead));


The following code is my mid function code that i wrote (it all compiles
with out any errors).


char* mid(char* cStringBeingSentIn, int iPositionToStartAt, int iLength)
{
iLength = iLength - 1;

char *cNewStringBeingLookedAt = (char*)malloc(iLength+1+1);
int iCharPositionToBeStoredAt = 0;


if ((iPositionToStartAt + iLength+1) > (int)strlen(cStringBeingSentIn) )
{
return new char = "ERROR";
}

if (cStringBeingSentIn == NULL)
{
return new char = "ERROR";
}

if ( (iPositionToStartAt <0) || (iLength < 0) )
{
return new char = "ERROR";
}

if (strlen(cStringBeingSentIn) == 0)
{
return new char = "ERROR";
}

for (int iCount = iPositionToStartAt; iCount <= iPositionToStartAt +
iLength; iCount++)
{
cNewStringBeingLookedAt[iCharPositionToBeStoredAt] =
cStringBeingSentIn[iCount];
iCharPositionToBeStoredAt++;
}

cNewStringBeingLookedAt[iLength+1] = '\0';

return strcpy(new char,cNewStringBeingLookedAt) ;
}


Any help would be grate if any one can figure out what going on. Thanks in
adv.

Gizmo.
 
A

Aggro

Gizmo said:
hello all have been trying to write a Mid() function a bit like the one in
vb.

First question: Why? What's wrong with std::string function substr()
which does exactly the same.

string substr (size_type pos, size_type n);
Returns a substring of the current string, starting at position pos and
of length n:

Example:
------------------------------------------------------
#include <iostream>
#include <string>
int main()
{
std::string str18 = "abcdefghi";
std::string str19 = str18.substr(6,2);
std::cout << str19 << std::endl; // Prints out: "gh"
return 0;
}
------------------------------------------------------
 
J

John Harrison

Gizmo said:
hello all have been trying to write a Mid() function a bit like the one in
vb. i come to compile it and there are no errors however when i run it an
error accours and it says the program has to close. The odd thing is the
error accours after the mid function has done its job. but if i take out the
mid function the error does not accour.
[snip]

Any help would be grate if any one can figure out what going on. Thanks in
adv.

Gizmo.

There quite a lot wrong with the posted code. I think you need to pick up
your favourite C++ book and have a look at dynamic memory allocation and
string handling.
return new char = "ERROR";
return strcpy(new char,cNewStringBeingLookedAt) ;

These two lines are completely bogus. I'd like to offer some advice but I
can't really see what you think you are doing here. Here's how you should
allocate memory for a string

char* aString = new char[aLength + 1];

Hope this helps.

john
 
K

Kevin Goodsell

Gizmo said:
hello all have been trying to write a Mid() function a bit like the one in
vb.

That doesn't help me. What is Mid() supposed to do?
i come to compile it and there are no errors however when i run it an
error accours and it says the program has to close. The odd thing is the
error accours after the mid function has done its job. but if i take out the
mid function the error does not accour.

i have found that if i do

cout <<mid(newString.GetString(),iPosition,3);

What is newString? It looks like some non-standard string class. Why are
you not using the standard class std::string?
the erorr will occur. However if i do

cout <<strcpy(new char,mid(newString.GetString(),iPosition,3));

This is doomed to fail. If mid returns a pointer to a string longer than
0 characters you will write over memory you don't own. 'new char' gives
you *one* char. Just one. Try to copy more than that and your program
will go down in flames, or worse.

Part of the problem here is that you are using char pointers as C-style
strings. You would be much better off using std::string.
the erorr does not occur.

If i debug it . . im given the error message "Unhandled exception in
Main.exe: 0xc0000005: Access Violation." and it takes me to the part that
its says the error occurs.

The following If statment is where it says it is going wrong (i did not
write this code it must be part of the code that comes with c++)

'C++' does not come with code. This is part of a particular C++
implementation. It looks like MS Visual C++ to me.

The error indicates that you've written to memory you don't own. The
code below indicates that you've written past the end (or before the
beginning) of a dynamically allocated chunk of memory. Incidentally,
this is exactly what is likely to happen with your 'strcpy(new char' above.
if (!CheckBytes(pbData(pHead) + pHead->nDataSize,
_bNoMansLandFill, nNoMansLandSize))
_RPT3(_CRT_ERROR, "DAMAGE: after %hs block (#%d) at
0x%08X.\n",
szBlockUseName[_BLOCK_TYPE(pHead->nBlockUse)],
pHead->lRequest,
(BYTE *) pbData(pHead));


The following code is my mid function code that i wrote (it all compiles
with out any errors).

That certainly does not mean it's correct.
char* mid(char* cStringBeingSentIn, int iPositionToStartAt, int iLength)
{
iLength = iLength - 1;

char *cNewStringBeingLookedAt = (char*)malloc(iLength+1+1);

Don't use malloc unless you have a very good reason. You don't seem to
have any reason in this case. Use 'new' instead.
int iCharPositionToBeStoredAt = 0;


if ((iPositionToStartAt + iLength+1) > (int)strlen(cStringBeingSentIn) )

This cast is a bad idea. First, don't use C-style casts in C++. Use a
C++ cast operator instead (static_cast in this case). Second, converting
size_t to int may have unexpected results. You'd do better to convert
the left side to size_t, or to write code that doesn't require casts.
{
return new char = "ERROR";

This is so wrong I hardly know where to start. 'new char' returns a
pointer to one, single, solitary character. That single character cannot
store an entire string, only a single character. Even if you allocated
enough space for your entire "ERROR" string, the assignment is
completely wrong. All you are doing is allocating a char, then
immediately assigning the address of a string literal to the temporary
char * that was pointing to your allocated char (which causes a memory
leak since you no longer have a pointer to your allocated char), then
returning that address. The effect is the same as this:

new char;
return "ERROR";

Any attempt outside this function to modify the string that was returned
or to delete it will break your program. You can't modify a string
literal, so it's a very bad idea to ever create a (non-const) pointer to
one. In fact, conversion from string literal to (non-const) char* is a
deprecated language feature.

Besides that, if you return here you leak the memory pointed to by
cNewStringBeingLookedAt.
}

if (cStringBeingSentIn == NULL)
{
return new char = "ERROR";

Same problem.
}

if ( (iPositionToStartAt <0) || (iLength < 0) )
{
return new char = "ERROR";

And again.
}

if (strlen(cStringBeingSentIn) == 0)
{
return new char = "ERROR";

And again.
}

for (int iCount = iPositionToStartAt; iCount <= iPositionToStartAt +
iLength; iCount++)
{
cNewStringBeingLookedAt[iCharPositionToBeStoredAt] =
cStringBeingSentIn[iCount];
iCharPositionToBeStoredAt++;
}

cNewStringBeingLookedAt[iLength+1] = '\0';

I don't understand what any of this is intended to do, but it looks very
dangerous.
return strcpy(new char,cNewStringBeingLookedAt) ;

This is extremely broken. Again, you are returning without freeing the
memory pointed to by cNewStringBeingLookedAt. But even worse is that you
are again trying to copy a string into a single char.
}


Any help would be grate if any one can figure out what going on. Thanks in
adv.

What's going on is that you seem to have very little understanding of
pointers, strings, or dynamic memory. You need to back up and learn
these things before you attempt to use them in anything non-trivial.

-Kevin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,142
Messages
2,570,817
Members
47,363
Latest member
eitamoro

Latest Threads

Top