strcat crashes program

Started by
15 comments, last by Zahlman 18 years, 8 months ago
thanks for all the help. CHAR szWebPage[4000] = ""; gave an error, somthing about initialazation of szWebPage skipped by 'case' label(same error I get when I initalize and assigne a value to a variable in the same line in winproc on this program for some odd reason), so after its defined I just used szWebPage[0] = 0; and that fixed the problem. I've got plenty of work left to do on this program, including making the string stuff more versitile. the szWebPage was originally 400,000 in size but messagebox couldn't print a string that long (can't imagine why *sarcasm*), since my program was testing on www.google.com and since google html page source when saved to a text file is 2.5 kb I figured that 4k would be enough. I'll try to keep the advice here in mind in the future. thanks again.
Bloodshed Dev-C++ 4.9.8.0 Mingw DX 9.0a DX SDK 6.1win2k#define WIN32_LEAN_AND_MEANthe Particle Projection Cannon fires a shimmering blue bolt, much like a cross between lightning and a sine wave that ripples along its path.mechwarrior 2 mercenaries, 4 particle projection cannons, thug chassis
Advertisement
Just clear out both strings so you don't have to worry about putting in NULLs before or after:
memset( szBuffer, 0, 1024 );
memset( szWebPage, 0, 4000 );
Quote:CHAR szWebPage[4000] = ""; gave an error, somthing about initialazation of szWebPage skipped by 'case' label

The obvious solution is to create a local scope under the case label
switch( whatever ){case whatever:    {        // do all your work    }    break;case somethingElse:    ...}
_______________The essence of balance is detachment. To embrace a cause, to grow fond or spiteful, is to lose one''s balance after which, no action can be trusted. Our burden is not for the dependent of spirit. - Mayar, Third Keeper
Quote:Original post by izhbq412
Quote:CHAR szWebPage[4000] = ""; gave an error, somthing about initialazation of szWebPage skipped by 'case' label

The obvious solution is to create a local scope under the case label
switch( whatever ){case whatever:    {        // do all your work    }    break;case somethingElse:    ...}
Yes, that is the typical approach. It's also usually a warning not an error, so perhaps you have warnings-as-errors set to true?
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
Quote:Original post by iMalc
Quote:Original post by SiCrane
Quote:Original post by Zahlman
No, you can't assign a string literal to a char array like that, because the array is a char * const - you can't repoint it. You can certainly initialize the array (or at least make it appear to contain zero-length data) in the way that Aprosenf indicated, or initialize as "CHAR szWebPage[4000] = {0}", but either of these still has lots of problems.


CHAR szWebPage[4000] = ""; is a valid char array initialization statement, equivalent to CHAR szWebPage[4000] = {0};
Exactly, and one I use regularly enough too. It's one of the rare cases where you don't have to use a strxxx function.


You learn something every day... that's an odd sort of special case :/ I hate C++ syntax. :(

Quote:When you say "either of these still has lots of problems" do you mean with the rest of the code? I must admit, I wasn't looking for other bugs.


I mean that using a char array has limitations, even if you wouldn't consider them bugs, and it forces stuff (like the strxxx functions) into the rest of the code that are likely to cause more problems. :)

Quote:the szWebPage was originally 400,000 in size but messagebox couldn't print a string that long (can't imagine why *sarcasm*), since my program was testing on www.google.com and since google html page source when saved to a text file is 2.5 kb I figured that 4k would be enough. I'll try to keep the advice here in mind in the future. thanks again.


Well, you know, one of these days you *will* have to deal with a 400Kb webpage (or bigger; I know of some documents on the web that are several megs in what was a text file that got a minimum of HTML markup added). And you won't want to have to allocate a buffer big enough to make sure of handling those cases, only to have 99% of the space going unused most of the time. Using std::string will take care of this for you; it automatically resizes according to need (it will keep extra space, in general, for efficiency reasons - to avoid resizing too frequently - but again in general you can expect it to take no more than 1.5-2x as much space as is needed or so, depending on your implementation).

It also can make repeated appending faster, because it will automatically keep track of string lengths and therefore "append points", whereas hand-written code to do this stuff in C is often slowed down by unnecessary repeated strlen() calls (either explicit ones or implicitly via other strxxx functions). It's also a heckuva lot easier to get right which is the important thing. Oh, and it reads much more nicely too. :)

Quote:Original post by Zahlman
Quote:Original post by iMalc
Quote:Original post by SiCrane
CHAR szWebPage[4000] = ""; is a valid char array initialization statement, equivalent to CHAR szWebPage[4000] = {0};
Exactly, and one I use regularly enough too. It's one of the rare cases where you don't have to use a strxxx function.


You learn something every day... that's an odd sort of special case :/ I hate C++ syntax. :(


It's not so odd if you keep in mind that "foo" is syntactic sugar for { 'f', 'o', 'o', 0 }
Ack sadlkfjg, it's an initialization, not an assignment. Never mind me. :)

This topic is closed to new replies.

Advertisement