|
![]() |
|
| Author |
|
|||||||
|
Hunter
Posts: 4523
Location: Brisbane, Queensland
|
Another quick question about a C function, this time its strcpy(). My question is, is there a limit on the size of the strings? I only ask because if I try to copy a string larger than 12 characters it causes an invalid page fault in kernel32.dll.
I'm probably doing something wrong but I vaguely recall seeing something about there being a maximum size to strings that can be copied and was dictated by the operating system. And yes I've checked the standard c library reference, and it mentions nothing about a limit. If anyone could shed some light on this I'd appreciate it :). |
|||||||
| #0 12:25am 29/05/02 |
|
|||||||
|
system
|
--
|
|||||||
| #0 |
|
|||||||
|
ineffable
Posts: 2278
Location: Brisbane, Queensland
|
You'd be after strdup, strcpy needs the ram to be allocated before hand.
|
|||||||
| #1 12:30am 29/05/02 |
|
|||||||
|
Hunter
Posts: 4524
Location: Brisbane, Queensland
|
Yeah I *thought* I had allocated the right amount of space for strcpy() yet as it turns out, I was allocating 4 bytes less than what I needed. I looked at strdup() in my book and considered that and then found that its non-ANSI :|. Oh well writing your own version of it wouldn't be too much extra work.
|
|||||||
| #2 12:41am 29/05/02 |
|
|||||||
|
Hunter
Posts: 4525
Location: Brisbane, Queensland
|
Oh and my compiler (lcc-win32) doesn't appear to have strdup() either.
|
|||||||
| #3 12:42am 29/05/02 |
|
|||||||
|
Khel
Posts: 2638
Location: Brisbane, Queensland
|
Yeah, you have to already have allocated a buffer to use for strcpy. Usually thats not too much of a hassle though. If you know how long the string is gonna be before hand (or you know the maximum size it could be), its not a problem, if you dont you could count the number of characters in the source string and then use something like malloc or calloc to dynamically allocate the memory. You could use something like
char *strPtr, *destString; int charCount; for(charCount = 0, strPtr = srcString; *strPtr != '\0'; strPtr++, charCount++); destString = (char *)calloc(charCount+1, sizeof(char)); strcpy(destString, srcString); That is of course assuming srcString (the source string you want to copy) is null terminated, which it should be if you're using any str functions on it anyway. I can't remember if I have the arguments for strcpy and calloc around the right way, but I'm sure you can figure it out if I dont. Its 1am and I'm going to bed and I couldn't be bothered looking it up to make sure I'm right :) |
|||||||
| #4 12:58am 29/05/02 |
|
|||||||
|
Hunter
Posts: 4526
Location: Brisbane, Queensland
|
Oh well I worked around another problem I was having (not related to the strcpy() problem). Its probably just one big kludge but I don't care it works perfectly. When I release the source you can read it and tell me how bad it is :).
|
|||||||
| #5 01:21am 29/05/02 |
|
|||||||
|
hast
Posts: 2
Location: Brisbane, Queensland
|
strcpy is a mad function
it just keeps putting characters in the destination buffer until it hits a null in the source buffer.. |
|||||||
| #6 11:07am 29/05/02 |
|
|||||||
|
Leon Trotsky
Posts: 213
Location: Brisbane, Queensland
|
i dun do much c, but i know that malloc is a bitch to use...
don't know about calloc but from what khel has shown looks like it could be wicked, dynamically allocating memory |
|||||||
| #7 11:14am 29/05/02 |
|
|||||||
|
Goa`uld
Posts: 4330
Location: Brisbane, Queensland
|
Heh C was such a bitch to write. Whenever I went to get help people were like "what libraries are you using" and I was like "err I'm a noob i have nfi"
|
|||||||
| #8 05:02pm 29/05/02 |
|
|||||||
|
Sly
Posts: 13
Location: Brisbane, Queensland
|
char *strPtr, *destString; No need for the for loop. use strlen(). int charCount = strlen(srcString); or destString = (char *)malloc(strlen(srcString) + 1); strcpy() blindly overwrites anything in the destination buffer anyway, so using calloc is a waste. calloc is basically a malloc followed by filling the buffer with zeroes. Just use malloc and let strcpy overwite the buffer. A very basic version of strcpy()... while ((*dest++ = *src++) != 0) {;} |
|||||||
| #9 08:57am 30/05/02 |
|
|||||||
|
Khel
Posts: 2653
Location: Brisbane, Queensland
|
Hah, I never even knew there was a strlen function in Ansi C :( Woe is me.
|
|||||||
| #10 09:51am 30/05/02 |
|
|||||||
|
Hunter
Posts: 4572
Location: Brisbane, Queensland
|
Really!? /me pats C book :P
|
|||||||
| #11 12:31pm 30/05/02 |
|
|||||||
|
fpot
Posts: 4847
Location: Gold Coast, Queensland
|
'C' comes after 'B'.
|
|||||||
| #12 12:32pm 30/05/02 |
|
|||||||
|
Hunter
Posts: 4574
Location: Brisbane, Queensland
|
But B does not come after A.
|
|||||||
| #13 12:40pm 30/05/02 |
|
|||||||
|
hast
Posts: 4
Location: Brisbane, Queensland
|
hunter i thought you did a course in c programming
and you don't even know how to use strcpy() properly?? |
|||||||
| #14 02:54pm 30/05/02 |
|
|||||||
|
Khel
Posts: 2658
Location: Brisbane, Queensland
|
Yeah, I spose I should get a C book one day, all I ever use for reference is the MSDN cds, which are good, but are kinda hard to browse through and read like a book. Books are good like that, even if you're not looking for anything in particular you can just flick through them and most of the time you find something interesting you didn't know.
I've also noticed sometimes when you search the MSDN help thing it gives you really really stupid results :( |
|||||||
| #15 05:47pm 30/05/02 |
|
|||||||
|
orbitor
Posts: 1726
Location: Brisbane, Queensland
|
go the good old "A Book on C" :)
|
|||||||
| #16 06:04pm 30/05/02 |
|
|||||||
|
Hunter
Posts: 4586
Location: Brisbane, Queensland
|
I did a couple of modules (Intro to C - eg hello world stuff and Applications using C/C++ which was hash tables, I/O communications and a few other things) in a course which isn't even a programming course... its to broaden your knowledge, not give you a full overview of the entire C language and its standard library functions. Khel: Yeah that's why I like books - they go anywhere and you don't need a computer to view them. And reading a book for a few hours doesn't hurt your eyes like reading text on a monitor does. And yeah you do find a lot of interesting tidbits when flipping through a book. |
|||||||
| #17 08:45pm 30/05/02 |
|
|||||||
|
system
|
--
|
|||||||
| #17 |
|
|||||||
|
| ||||||||