So write:Originally Posted by CommonTater
Code:char *envStr = malloc(MAX_ENV * sizeof(*envStr));
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
3. Get rid of conio.h and other antiquated DOS crap headers.
4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.
The "something else" that is happening is that you are completely misunderstanding what your code does. Or, more specifically, trying to do one thing while using a code construct that does something else entirely.
When you are using "=", you are just doing a pointer assignment. In themselves, pointer assignments are unlikely to cause any symptoms (such as a segmentation fault). However, the assignment you are doing is still invalid - any subsequent attempt to use that pointer (eg copy data to the object a pointer points at) is invalid, and will probably cause some form of runtime error.
The problem with your code is not with the working of malloc(). It is with the value your code supplies to to malloc(). sizeof getenv("http_proxy") does not give the length of the string returned by getenv - it gives the size of a pointer to char (which has the same value, regardless of that the pointer points at). The size of a pointer is compiler-dependent, but typically 4 for a 32-bit compiler.
malloc() when supplied a value of 5 (the result of 4+1) will always allocate 5 bytes - because that is what your code told it to do.
If you then attempt to write more than 5 bytes to it, then you will be merrily tromping some random area of memory past the last of those five bytes. The line "strcpy(envProxySrv, getenv("http_proxy"));" will do that if the length if strlen(getenv("http_proxy")) exceeds 4 i.e. if http_proxy expands to an environment string longer than 4. There is no magic performed by strcpy() wither. envProxySrv has been five bytes allocated to it by your malloc() call. strcpy() does not make envProxySev grow if you try to write more than 5 bytes to it.
What you need to do with the malloc() call is
as strlen() is the way to retrieve the length of a string, not sizeof. The +1 allows for a zero value that, by convention, marks the end of C-style string (i.e. is used by strlen(), written by strcpy(), etc).Code:envProxySrv=malloc(strlen(getenv("http_proxy"))+1);
Keep in mind, also, that "no runtime error" does not mean "code is working". It can, as in your case, simply mean "haven't done something yet that causes operating system to detect an error".
Sorry, I didn't know because I did not participate in that debate, or even observe it. You can tell what would have been my stand if I did chip inOriginally Posted by claudiu
I believe that all compilers will resolve sizeof because it is a compile time operator, not a function. It is constant folding that is in the realm of optimising compilers, but then it is a common and relatively easy to implement optimisation.Originally Posted by CommonTater
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)