strncpy crashes!

Started by
21 comments, last by peter86 21 years, 10 months ago
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));
  
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]
---visit #directxdev on afternet <- not just for directx, despite the name
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.
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.
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?
<yoda>Hear you nothing that we say? </yoda>

[edited by - sneftel on June 27, 2002 7:00:32 PM]
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]
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

[twitter]warrenm[/twitter]

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.
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.

This topic is closed to new replies.

Advertisement