473,394 Members | 1,887 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

vc++ - strtok() access violation

Hi:

I am using Visual Studio .NET 2003 and console project (disabled
precompiled header).

I am using _tcstok() which resolves to strtok in my case. It keeps
crashing (unhandled error, access violation). Firt I thought my variable
wasn't writtable (const) but it wasn't so I used literal as input but it
still crashes.

it crashes exactly in strtok.c (line 99):

....

for ( ; *str ; str++ )
if ( map[*str >> 3] & (1 << (*str & 7)) ) {
*str++ = '\0';
break;
}

....
My test call is like this strtok("hklm\\software","\\"); Anyhow I went
through all over the net and I didn't find anything related to my problem.

Thanks in advance.
Aug 7 '05 #1
6 7363

"nobody" <no****@hotmail.com> wrote in message
news:h8********************@rogers.com...
Hi:

I am using Visual Studio .NET 2003 and console project (disabled
precompiled header).

I am using _tcstok() which resolves to strtok in my case. It keeps
crashing (unhandled error, access violation). Firt I thought my variable
wasn't writtable (const) but it wasn't so I used literal as input but it
still crashes.
That's because attempting to modify a literal produces
undefined behavior (it seems in your case, this is manifest
as a 'crash').

it crashes exactly in strtok.c (line 99):

...

for ( ; *str ; str++ )
if ( map[*str >> 3] & (1 << (*str & 7)) ) {
*str++ = '\0';
break;
}

...
My test call is like this strtok("hklm\\software","\\"); Anyhow I went
through all over the net and I didn't find anything related to my problem.


Try some C++ books. See www.accu.org for peer reviews.

Change your literal to an array:

char arr[] = "hklm\\software";
strtok(arr,"\\");

And be sure to check 'strtok()'s return value to
see if it actually found anything.

-Mike
Aug 7 '05 #2
Now I feel dumb and it works like a charm.

The thing that doesn't make sense is that I made a function calling
strtok() initially. ex:

getField("hkml\\software","\\");

then getField calls strtok(), so literal passed through another variable
doesn't make that variable modifiable (ie: strtok can't modify it)?

Thanks alot.
Aug 7 '05 #3
nobody wrote:
Now I feel dumb and it works like a charm.

The thing that doesn't make sense is that I made a function calling
strtok() initially. ex:

getField("hkml\\software","\\");

then getField calls strtok(), so literal passed through another
variable doesn't make that variable modifiable (ie: strtok can't
modify it)?

Arrays that are passed to functions are not copied. Instead, the array
name is converted to a pointer to the first element. It is undefined
behavior to modify a string literal, such as when strtok() punches
holes in the string as it works down through it.

You'd be much better off with std::string and using one of the various
techniques for doing something similar to strtok() that yield a vector
of strings. Some have been posted here in the past.

Brian
Aug 8 '05 #4
"Default User" <de***********@yahoo.com> wrote in news:3lpjfkF13p0arU1
@individual.net:
nobody wrote:
Now I feel dumb and it works like a charm.

The thing that doesn't make sense is that I made a function calling
strtok() initially. ex:

getField("hkml\\software","\\");

then getField calls strtok(), so literal passed through another
variable doesn't make that variable modifiable (ie: strtok can't
modify it)?

Arrays that are passed to functions are not copied. Instead, the array
name is converted to a pointer to the first element. It is undefined
behavior to modify a string literal, such as when strtok() punches
holes in the string as it works down through it.

You'd be much better off with std::string and using one of the various
techniques for doing something similar to strtok() that yield a vector
of strings. Some have been posted here in the past.

Brian

Since the first reply I moved away from strtok because it's easier to
just do the samething myself (hard to believe myself:)).

Can someone tell me if my code is optimized and what could I do better
(areas of improvement)? In my code I let the caller release the memory
(someone said that's the COM way). Is it better to use a CONST for the
first argument?

//return left side of the delimiter
TCHAR* _tleft(TCHAR* lpszString, CONST TCHAR* chDelimiter)
{
BYTE byteSize = _tcsclen(lpszString) + 1;
TCHAR *lpszCurPos;

lpszCurPos = lpszString;

TCHAR *lpszFound = 0;

TCHAR *lpszOut = new TCHAR[byteSize];
TCHAR *lpszOut2;

lpszOut2 = lpszOut; //hold final string

lpszFound = _tcsstr(lpszString,chDelimiter);

while ((*lpszCurPos)&&((lpszFound - lpszCurPos) != 0))
{
*lpszOut++ = (TCHAR)*lpszCurPos++;
}

*lpszOut='\0';
return lpszOut2;
}

//return right side of the delimiter
TCHAR* _tright(TCHAR* lpszString, CONST TCHAR* chDelimiter)
{
BYTE byteSize = _tcsclen(lpszString) + 1;
TCHAR *lpszCurPos;

lpszCurPos = lpszString;

TCHAR *lpszFound = 0;

TCHAR *lpszOut = new TCHAR[byteSize];
TCHAR *lpszOut2;

lpszOut2 = lpszOut; //hold final string

lpszFound = _tcsstr(lpszString,chDelimiter);
if(lpszFound)
{
while ((*lpszCurPos++)&&((lpszFound - lpszCurPos) != 0))
{}
while(*lpszCurPos++)
{
*lpszOut++ = (TCHAR)*lpszCurPos;
}
*lpszOut='\0';
return lpszOut2;
}
else
return NULL;
}
Aug 9 '05 #5

nobody wrote:
[snip]

I am using _tcstok() which resolves to strtok in my case. It keeps
crashing (unhandled error, access violation).

[snip]

http://kernelnewbies.org/documents/k...api/r1401.html
strtok is deprecated, use strsep instead (on UNIX).
Or use another features of C++, for instance see
"Splitting string into vector of vectors" at
http://groups.google.com/group/sourc...993fb8841382c8

--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Aug 9 '05 #6
nobody wrote:
"Default User" <de***********@yahoo.com> wrote in news:3lpjfkF13p0arU1
@individual.net:
nobody wrote:
then getField calls strtok(), so literal passed through another
variable doesn't make that variable modifiable (ie: strtok can't
modify it)?

Arrays that are passed to functions are not copied. Instead, the
array name is converted to a pointer to the first element. It is
undefined behavior to modify a string literal, such as when
strtok() punches holes in the string as it works down through it.

You'd be much better off with std::string and using one of the
various techniques for doing something similar to strtok() that
yield a vector of strings. Some have been posted here in the past.


Since the first reply I moved away from strtok because it's easier to
just do the samething myself (hard to believe myself:)).
Ok.
Can someone tell me if my code is optimized and what could I do
better (areas of improvement)? In my code I let the caller release
the memory (someone said that's the COM way). Is it better to use a
CONST for the first argument?
Do you have a particular reason for using all those proprietary data
types rather than the standard ones?
//return left side of the delimiter
TCHAR* _tleft(TCHAR* lpszString, CONST TCHAR* chDelimiter)
{
BYTE byteSize = _tcsclen(lpszString) + 1;


I won't even bother trying to decipher the code with all this stuff. Go
to a Microsoft newsgroup and have them look at it. If you want to do it
using nice portable standard constructs, then we'll talk again.

On the whole it looks longer and more convoluted than need be.
Brian
Aug 9 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Binary | last post by:
VC++ .NET 2003: Access violation with /O2 compiler switch; no AV with /O Hi I'm in the process of narrowing down a problem, and I have reduced the code involved to the following If someone could...
2
by: Itjalve | last post by:
I have a heap problem in my com exe server. When i use windbg as the compiler in application verifier is windbg started and i can press 'g' and the server is running unitil i get the exception. ...
1
by: Robert Ludewig | last post by:
I have sucessfully imported and compiled a complex MFC 6.0 project from vc++6.0 (MFC6.0) into vc++ 8.0 (MFC 8.0). It contains several subprojects (libs and dlls). In vc++ 6.0 those project linked...
13
by: ern | last post by:
I'm using strtok( ) to capture lines of input. After I call "splitCommand", I call strtok( ) again to get the next line. Strtok( ) returns NULL (but there is more in the file...). That didn't...
10
by: Diego Martins | last post by:
I just discovered a serious issue when compiling code using auto_ptr<> and VC 8.0 consider some sort of create() function blahblah * create() { return new blahblah; } and a pretty auto_ptr
2
by: farshid.roumi | last post by:
I was able to solve the problem in vc++5 by changing the vc++ settings. ButI can't find the same options in vc++ 2005. I have a lot of big arrays! "Unhandled exception at 0x0040c247 in jan7.exe:...
1
by: Kirzak pascuale | last post by:
ok I made this exercise, for a compiler design course. it is not urgent as the assignment was due 2 days ago. i got a 4 out of 10 for effort. mainly because i did not just rehash code examples from...
3
by: raghunadhs | last post by:
Hi all, i have developed a ".dll" in vc++, regarding to find out CRC32. and in my V.B 6.0 application i am calling that .dll. If i run my v.b application it is working means.. i am able to find...
6
by: Sick0Fant | last post by:
When I try to use strtok, I get an access violation. The program compiles and runs up to that point, but then MSVC asks me for the path of STRTOK.C and if I cancel that dialog, MSVC provides me with...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.