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. 3 4304
Gizmo wrote: 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;
}
------------------------------------------------------
"Gizmo" <sc***********@hotmail.com> wrote in message
news:bi**********@news6.svr.pol.co.uk... 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
Gizmo wrote: 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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Ken |
last post by:
Hi.
Can anyone refer me to any articles about the compatibility between
c++ polymorphism and real-time programming?
I'm currently on a real-time c++ project, and we're having a
discussion...
|
by: Carmine Cairo |
last post by:
Hi,
I'm working on a project and today I've note a little problem during the
compile fase.
Here a little piece of code:
// 1st version
welldone = 0;
size = p->getSize();
backbone = new...
|
by: Christaaay |
last post by:
I have been using the code below successfully for almost a year.
yesterday, I began getting a run time error 6 (overflow). I am using
the code in an Access 2000 database. Can anyone help me...
|
by: MLH |
last post by:
I want to change my system time date each time an A97 app is
started. Here's how I've been doing it. Am looking for a better
way. Sure some of you have researched this.
Function...
|
by: Mark |
last post by:
I have .aspx code-behinds that inherit from the class below. The code runs
just fine, but the form designer bombs at design time when trying to view
the .aspx page in VS.NET 2003. If I comment...
|
by: Alfonso Morra |
last post by:
Hi I'm compiling some code and need to generate some random numbers. To
save time, I decided to use the srand, rand and time functions.
My code worked (atleast built fine) until I added time.h,...
|
by: HNguyen |
last post by:
Hi,
I have a Web application in ASP.NET. My Application allows the users upload
files into the server after checking their user names and passwords. For
each transaction, the Web program will...
|
by: Khurram |
last post by:
I have a problem while inserting time value in the datetime Field.
I want to Insert only time value in this format (08:15:39) into the SQL
Date time Field.
I tried to many ways, I can extract...
|
by: ThunderMusic |
last post by:
Hi,
I'd like to create a compile time error in my class... maybe there's a way
already built in in the framework so I can achieve what I want... I have 2
constructors in my class. One of them...
|
by: ahmurad |
last post by:
Dear Xperts,
In my database table some fault code type records are inserted and the values are like bellows:
Error:2/FC:10; 00:15:13 16/03/2009;
Error:2/FC:20; 00:15:15 16/03/2009; ...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: Mushico |
last post by:
How to calculate date of retirement from date of birth
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
| |