dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Access Violation Error with cstring

  1. #1
    Join Date
    Jan 2009
    Posts
    16

    Access Violation Error with cstring

    My declaration:
    Code:
    	char * tempToken;
    	tempToken = "";
    What's giving the error:
    Code:
    strcpy_s(tempToken, tempInt, tempString);

    Both tempInt and tempString have valid values. I have a struct called Node that has a token and a link to another Node (for a linked list). I've tried

    Code:
    strcpy_s(v->token, tempInt, tempString);
    And the error THAT gave me something along the lines of DST != NULL && sizeInByte > 0

    I've tried char tempToken[100]; as a declaration and char *tempToken = ""; and char * tempToken = malloc(sizeof(char)*100);

    I'm banging my head against the wall here. Anyone tell me what I'm doing wrong?

    Thanks in advance,
    Lang

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Access Violation Error with cstring

    Code:
    	char * tempToken;
    	tempToken = "";
    You've just pointed tempToken at a literal string. An empty one, but a literal nonetheless. Literal strings are allowed to live in a read-only portion of memory, and usually do on modern compilers.

    Code:
    strcpy_s(tempToken, tempInt, tempString);
    Now you've just attempted to modify read-only memory.

    What you should have done instead is allocate memory for tempToken and the copy in the literal string, so that you'd have modifiable memory to work with. Or better yet, just use a std::string so that you don't have to concern yourself with such things.

  3. #3
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Access Violation Error with cstring

    I'm banging my head against the wall here. Anyone tell me what I'm doing wrong?
    Then I suggest a good C++ book: http://www.codeguru.com/forum/showthread.php?t=231039.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)