Thread: what is malloc doing?

  1. #16
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by claudiu View Post
    No need to sizeof char since it's always going to be 1, but I guess that's the more general way of malloc-ing if you had something other than char.
    True... but then there's int, short, long, double etc. all different sizes so it's become something of a habit. Might take an extra microsecond of processor time, but saves a ton of debugging time...

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by CommonTater
    True... but then there's int, short, long, double etc. all different sizes so it's become something of a habit.
    So write:
    Code:
    char *envStr = malloc(MAX_ENV * sizeof(*envStr));
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #18
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by laserlight View Post
    So write:
    Code:
    char *envStr = malloc(MAX_ENV * sizeof(*envStr));
    Oh gawd, not this argument about which one is better again .

    You know how I feel about that way of doing it.
    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.

  4. #19
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by CommonTater View Post
    True... but then there's int, short, long, double etc. all different sizes so it's become something of a habit. Might take an extra microsecond of processor time, but saves a ton of debugging time...
    It won't take any extra processor time. Well, for compiling, yes, but not for running. I mean, any compiler should be clever enough to optimize away "integer*1". If not; get a different compiler.

  5. #20
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by tpe View Post
    OK, clear. But why is it working? I mean, when I use "=" I have no segmentation faults. With everything else (malloc, strdup, etc), I get some nice segmentation faults that are not very helpful...
    So, either gcc transparently does that (I doubt that!), or something else is happening...
    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
    Code:
    envProxySrv=malloc(strlen(getenv("http_proxy"))+1);
    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).


    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".
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #21
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by EVOEx View Post
    It won't take any extra processor time. Well, for compiling, yes, but not for running. I mean, any compiler should be clever enough to optimize away "integer*1". If not; get a different compiler.
    Which is why I said "might"... An optimizing compiler will resolve sizeof for standard types and some will even resolve the computation to a "magic number" for you... I get all that... but "might" because not all compilers do it.

  7. #22
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by CommonTater View Post
    Which is why I said "might"... An optimizing compiler will resolve sizeof for standard types and some will even resolve the computation to a "magic number" for you... I get all that... but "might" because not all compilers do it.
    Agreed . You were right. But I still thought it should be noted that even if you care about processor speed you shouldn't care about such a thing.

  8. #23
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by EVOEx View Post
    Agreed . You were right. But I still thought it should be noted that even if you care about processor speed you shouldn't care about such a thing.
    Yep... glad you chimed in.

  9. #24
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by claudiu
    Oh gawd, not this argument about which one is better again .

    You know how I feel about that way of doing it.
    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 in

    Quote Originally Posted by CommonTater
    Which is why I said "might"... An optimizing compiler will resolve sizeof for standard types and some will even resolve the computation to a "magic number" for you... I get all that... but "might" because not all compilers do it.
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #25
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by laserlight View Post
    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.
    Modern compilers, yes, I would hope so...
    Some of the older brain dead ones... not so much...

    (hint: You wouldn't believe how many compilers I tried before finally settling on PellesC...)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What does Malloc do?
    By Albinoswordfish in forum C Programming
    Replies: 4
    Last Post: 03-10-2010, 06:23 AM
  2. malloc + segmentation fault
    By ch4 in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 03:46 PM
  3. Is there a limit on the number of malloc calls ?
    By krissy in forum Windows Programming
    Replies: 3
    Last Post: 03-19-2006, 12:26 PM
  4. Malloc and calloc problem!!
    By xxhimanshu in forum C Programming
    Replies: 19
    Last Post: 08-10-2005, 05:37 AM
  5. malloc() & address allocation
    By santechz in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 09:08 AM

Tags for this Thread