Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

peter86

strncpy crashes!

This topic is 5864 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is anything wrong with the code below and why does it crash?
  
LPSTR	lpErrMsg;
LPVOID	lpMsgBuf;

// ...

// Initializion

// ...


strncpy(lpErrMsg, reinterpret_cast<char *>(lpMsgBuf), sizeof(lpMsgBuf));
  

Share this post


Link to post
Share on other sites
Advertisement
1. You need to allocate memory for both both lpErrMsg and lpMsgBuf, either with new or by using an array: char lpErrMsg[100];

2. sizeof(lpMsgBuf) is always 4. That's not probably what you want. The intent of your code can be accomplished by just calling strcpy.

[edited by - IndirectX on June 27, 2002 6:47:32 PM]

Share this post


Link to post
Share on other sites
You seem to think that sizeof(lpMsgBuf) will return the length of the string lpMsgBuf points to. That is not the case. Read the manpage for strncpy, and then decide what that third argument should be.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
The length parameter to strncpy is the maximum number of characters to copy and should therefore be the size of the recieving char-array (to make sure that strncpy doesn''t write stuff in memory after the array). You don''t need to tell strncpy how long the source-string is because all strings in C and C++ are terminated by a NUL-character (ASCII 0), so strncpy can figure it out by itself.

Share this post


Link to post
Share on other sites
I isn´t working like this either:


  
LPSTR *lpErrMsg = new LPSTR;
LPVOID *lpMsgBuf = new LPVOID;
// ...// Initializion// ...

strncpy(*lpErrMsg, reinterpret_cast<char *>(lpMsgBuf), sizeof(lpMsgBuf));


Could someone give an working example and tell what´s wrong?

Share this post


Link to post
Share on other sites
<yoda>Hear you nothing that we say? </yoda>

[edited by - sneftel on June 27, 2002 7:00:32 PM]

Share this post


Link to post
Share on other sites
They did tell you enough information to solve this on you own, but since you want an example:

    
const size_t Size = 10;
char Src[] = "Fish.", *Dst;

Dst = new char[Size];

strncpy(Dst,Src,Size);

delete [] Dst;

Also, I don't think you should use Win32 typedefs (LPSTR, LPVOID) where you aren't interacting with the Win32 API. Not that it really matters, it's just not right .



[edited by - Null and Void on June 27, 2002 7:11:03 PM]

Share this post


Link to post
Share on other sites
Has NO ONE heard of strlen?

P.s. <tone sarcasm=0 type=sincere>D00d, you need to read up on how memory and dynamic allocation work. There''s a good article (that might be released on GameDev if whoever reads the mail at writers@gamedev.net gets their patooshka in gear, *hint*) titled "Understanding Pointers: A Game Developer''s Approach." I''d recommend it (ignoring the fact that I''m the author <tone>

Later,
ZE.

//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
If you''re going to use strlen(), you wouldn''t be using strncpy() - you''d be better off using strcpy().

Thinking before posting often helps.

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
If you''re going to use strlen(), you wouldn''t be using strncpy() - you''d be better off using strcpy().

Thinking before posting often helps.


I''m pretty sure ZE was talking about the code fragment allocating space for the destination string. For that matter, if you''re using strlen() for the copy-size, then you''d use memcpy, not strcpy. Take your own advice.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!