Re: error allocating memory with realloc(). how can i increase max_allowed in the system?
On Tue, 12 Aug 2008, Jordi Moles Blanco wrote: Hi, i'm running a FreeBSD 7.0 amd64 machine and struggling with some C code i'm writing. In addition to the other comments already given, I think that it might be useful for your learning experience to have some additional comments. I've had some trouble with this home-made script as it keeps crashing while launching a realloc() call. I narrowed down the problem and here i'm sending you a short example of code that crashes: * #include stdio.h #include stdlib.h int main() { int midataula; midataula = 3000; char *missatge = (char *)malloc(midataula * sizeof(char)); You should not cast the return value of malloc. [1] missatge[0]='h'; missatge[1]='o'; missatge[2]='l'; missatge[3]='a'; Recall that malloc() makes no promises about the contents of the memory allocated. I recommend, in the current style, adding: missatge[4]='\0'; printf(\n\ntaula1: %s,missatge); int voltes; voltes = 0; Traditionally in C, all variable declarations appear at the beginning of the local scope. Declaring new variables mid-scope is valid in certain off-shoots of C and may be acceptable in newer dialects/standards of the language, but the majority of C programmers might make certain assumptions about your code following older traditions. This certainly is not too egregious, but while I was here, I thought I'd mention it. while(voltes4) { midataula = midataula+500; realloc(missatge, midataula * sizeof(char)); voltes++; } printf(\n\ntaula2: %s,missatge); } * this is a full working you can compile on your machine. Like this... i get Segmentation fault (core dumped) but if instead of while(voltes4) i use while(voltes3) the script works fine with this output: ** taula1: hola taula2: hola ** so... i guess there must be a limit in the system somewhere. I've tried to reset all variables that i've seen in the sysctl -a list refering to malloc, memory, mem, and so on... but so far i haven't fixed the problem. i'm running this script as root and in the /etc/login.conf file there's only the default group with the unlimited values. A part from that, if i perform a limit call, i get this: * # limit cputime unlimited filesize unlimited datasize 33554432 kbytes stacksize524288 kbytes coredumpsize unlimited memoryuseunlimited vmemoryuse unlimited descriptors 45000 memorylocked unlimited maxproc 22500 sbsize unlimited * i've tried to resize datasize and stacksize, but the system won't let me do so. any idea how to solve this? thanks. Good luck with your programming and systems work! -Derek. [1] http://c-faq.com/malloc/mallocnocast.html ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
error allocating memory with realloc(). how can i increase max_allowed in the system?
Hi, i'm running a FreeBSD 7.0 amd64 machine and struggling with some C code i'm writing. I've had some trouble with this home-made script as it keeps crashing while launching a realloc() call. I narrowed down the problem and here i'm sending you a short example of code that crashes: * #include stdio.h #include stdlib.h int main() { int midataula; midataula = 3000; char *missatge = (char *)malloc(midataula * sizeof(char)); missatge[0]='h'; missatge[1]='o'; missatge[2]='l'; missatge[3]='a'; printf(\n\ntaula1: %s,missatge); int voltes; voltes = 0; while(voltes4) { midataula = midataula+500; realloc(missatge, midataula * sizeof(char)); voltes++; } printf(\n\ntaula2: %s,missatge); } * this is a full working you can compile on your machine. Like this... i get Segmentation fault (core dumped) but if instead of while(voltes4) i use while(voltes3) the script works fine with this output: ** taula1: hola taula2: hola ** so... i guess there must be a limit in the system somewhere. I've tried to reset all variables that i've seen in the sysctl -a list refering to malloc, memory, mem, and so on... but so far i haven't fixed the problem. i'm running this script as root and in the /etc/login.conf file there's only the default group with the unlimited values. A part from that, if i perform a limit call, i get this: * # limit cputime unlimited filesize unlimited datasize 33554432 kbytes stacksize524288 kbytes coredumpsize unlimited memoryuseunlimited vmemoryuse unlimited descriptors 45000 memorylocked unlimited maxproc 22500 sbsize unlimited * i've tried to resize datasize and stacksize, but the system won't let me do so. any idea how to solve this? thanks. ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
Re: error allocating memory with realloc(). how can i increase max_allowed in the system?
On Tue, 12 Aug 2008 17:02:43 +0200, Jordi Moles Blanco [EMAIL PROTECTED] wrote: Hi, i'm running a FreeBSD 7.0 amd64 machine and struggling with some C code i'm writing. I've had some trouble with this home-made script as it keeps crashing while launching a realloc() call. I narrowed down the problem and here i'm sending you a short example of code that crashes: * #include stdio.h #include stdlib.h int main() { int midataula; midataula = 3000; char *missatge = (char *)malloc(midataula * sizeof(char)); missatge[0]='h'; missatge[1]='o'; missatge[2]='l'; missatge[3]='a'; printf(\n\ntaula1: %s,missatge); int voltes; voltes = 0; while(voltes4) { midataula = midataula+500; realloc(missatge, midataula * sizeof(char)); voltes++; } There's your problem. realloc() works fine, but it *returns* the new pointer; it does _not_ modify missatge in place. The program should work fine if you use size_t for midataula (it is the 'size' of an array, which may not necessarily fit in an 'int'), and if you use realloc() correctly, as in: #include stdlib.h #include err.h size_t midataula; char *missatge; /* * DON'T cast the result of malloc(). It may 'hide' the bug of * a missing stdlib.h include, and cause troubles when * malloc() is implicitly defined by the compiler as: * *int malloc(...); * * On a 64-bit machine converting a 64-bit pointer to `int' will * lose the high-order 32 bits of the address, and you will try * to access unexpected memory areas. */ midataula = 3000; missatge = malloc(midataula * sizeof(*missatge)); if (missatge == NULL) err(1, malloc); Then when you use realloc() keep both midataula and missatge in temporary copies until you are sure that realloc() worked: while (voltes 4) { char *tmp; size_t newsize; newsize = midataula + 500; tmp = realloc(missatge, newsize * sizeof(*missatge)); if (tmp == NULL) err(1, realloc); /* * Now that you know the resize has succeeded, update * midataula and missatge. realloc() is allowed to * relocate missatge. See the following note in its * manpage: * * Note that realloc() and reallocf() may move the * memory allocation, resulting in a different return * value than ptr. */ midataula = newsize; missatge = tmp; } Right now you are calling realloc() as: realloc(missatge, newsize * sizeof(*missatge)); and throwing away the resulting pointer. The first time that realloc() discovers that the `resized' vector cannot fit in its original location, it relocates the array, and returns the new location. You throw away that location and your next iteration through the loop tries to access an invalid (already freed) memory region. That's what causes your segmentation fault. ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
Re: error allocating memory with realloc(). how can i increase max_allowed in the system? [solved]
Hello, thank you very much for your time and help, i had completely misunderstood how realloc() works. i though i was able to write some C code but now i feel a complete newbie, hehehe. anyway... that made everything clear to me and now my script is working like a charm. thanks for everything En/na Giorgos Keramidas ha escrit: On Tue, 12 Aug 2008 17:02:43 +0200, Jordi Moles Blanco [EMAIL PROTECTED] wrote: Hi, i'm running a FreeBSD 7.0 amd64 machine and struggling with some C code i'm writing. I've had some trouble with this home-made script as it keeps crashing while launching a realloc() call. I narrowed down the problem and here i'm sending you a short example of code that crashes: * #include stdio.h #include stdlib.h int main() { int midataula; midataula = 3000; char *missatge = (char *)malloc(midataula * sizeof(char)); missatge[0]='h'; missatge[1]='o'; missatge[2]='l'; missatge[3]='a'; printf(\n\ntaula1: %s,missatge); int voltes; voltes = 0; while(voltes4) { midataula = midataula+500; realloc(missatge, midataula * sizeof(char)); voltes++; } There's your problem. realloc() works fine, but it *returns* the new pointer; it does _not_ modify missatge in place. The program should work fine if you use size_t for midataula (it is the 'size' of an array, which may not necessarily fit in an 'int'), and if you use realloc() correctly, as in: #include stdlib.h #include err.h size_t midataula; char *missatge; /* * DON'T cast the result of malloc(). It may 'hide' the bug of * a missing stdlib.h include, and cause troubles when * malloc() is implicitly defined by the compiler as: * *int malloc(...); * * On a 64-bit machine converting a 64-bit pointer to `int' will * lose the high-order 32 bits of the address, and you will try * to access unexpected memory areas. */ midataula = 3000; missatge = malloc(midataula * sizeof(*missatge)); if (missatge == NULL) err(1, malloc); Then when you use realloc() keep both midataula and missatge in temporary copies until you are sure that realloc() worked: while (voltes 4) { char *tmp; size_t newsize; newsize = midataula + 500; tmp = realloc(missatge, newsize * sizeof(*missatge)); if (tmp == NULL) err(1, realloc); /* * Now that you know the resize has succeeded, update * midataula and missatge. realloc() is allowed to * relocate missatge. See the following note in its * manpage: * * Note that realloc() and reallocf() may move the * memory allocation, resulting in a different return * value than ptr. */ midataula = newsize; missatge = tmp; } Right now you are calling realloc() as: realloc(missatge, newsize * sizeof(*missatge)); and throwing away the resulting pointer. The first time that realloc() discovers that the `resized' vector cannot fit in its original location, it relocates the array, and returns the new location. You throw away that location and your next iteration through the loop tries to access an invalid (already freed) memory region. That's what causes your segmentation fault. ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED] ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
Re: error allocating memory with realloc(). how can i increase max_allowed in the system? [solved]
On Tue, 12 Aug 2008 18:22:21 +0200, Jordi Moles Blanco [EMAIL PROTECTED] wrote: Hello, thank you very much for your time and help, i had completely misunderstood how realloc() works. You are welcome, of course :-) ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
Re: error allocating memory with realloc(). how can i increase max_allowed in the system?
realloc(missatge, midataula * sizeof(char)); should be missatge=realloc(missatge, midataula * sizeof(char)); ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]