I have a Question about memory usage for C.

This is a discussion on I have a Question about memory usage for C. within the C Programming forums, part of the General Programming Boards category; oh sorry, I read your post wrong... thought you were saying that C compilers would error without a cast... my ...

  1. #16
    Unregistered
    Guest
    oh sorry, I read your post wrong... thought you were saying that C compilers would error without a cast... my mistake...

  2. #17
    Registered User oomakoo's Avatar
    Join Date
    Dec 2001
    Posts
    8
    I ran your code:

    #include <stdlib.h>
    int main()
    {
    char *p;
    p=(char *)malloc(30);
    if(p==NULL)
    {
    printf("Oops, not enought mem.");
    return EXIT_FAILURE;
    }
    free(p);
    return 0;
    }

    through the compiler with your recommended switches:

    gcc -W -Wall -pedantic -ansi test.c

    and got:

    test.c: In function 'main':
    test.c:9: warning: implicit declaration of function 'printf'

    I then included a cast:

    p=(char *)malloc(30);

    and got:

    test.c: In function 'main':
    test.c:9: warning: implicit declaration of function 'printf'

    CONCLUSION: Including or not including the cast makes no difference in the case of no errors.

    I then made a mistake (changed the type of the variable p) with the cast still in place:

    char **p;

    and got:

    test.c: In function 'main':
    test.c:6: warning: assignment from incompatible pointer type
    test.c:9: warning: implicit declaration of function 'printf'

    I then removed the cast:

    p=malloc(30);

    and got:

    test.c: In function 'main':
    test.c:9: warning: implicit declaration of function 'printf'

    My point is that I find warnings such as:
    test.c:6: warning: assignment from incompatible pointer type
    quite useful. Maybe it's a matter of style.



    Originally posted by Unregistered

    To reiterate again(so it gets through your thick skull)
    Sorry if my posting offended you. You are right about not using sizeof() being more efficient. (I do think that any ANSI compiler worth its salt would recognize '*sizeof(char)' as 'multiply by 1' and optimize it - without optimizations on!)

    CONCLUSION: I know nothing about good programming practice! :-)

  3. #18
    Unregistered
    Guest
    well the implicit printf problem could be solved by including stdio.h... sorry I forgot about that. By "through your thick skulls" I was being general. I meant those who had thick skulls(the ones who beleive something without proving it even though the opposite is proven to them) sorry if I offended you too much

  4. #19
    Unregistered
    Guest
    Oh I just looked back at your example and realized that you say that a warning about allocating memory for a char ** with a cast of (char *) is helpful... well if you leave the cast out like so
    char **c;

    c=malloc(30);

    you will end up with 30 pointers to char (char **)... what is wrong with that? If you want only 30 chars then you should be more watchful of your coding.

  5. #20
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    char **c;

    c = malloc( 30 ); //WRONG

    This only allocates 30 BYTES, not 30 pointers.

    c = malloc( 30 * sizeof( char* ) );

    This allocates 30 character pointers.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #21
    Unregistered
    Guest
    True quzah, I was rushed when I typed out that post(by a jack .......... friend ) but I think my point was still made...

  7. #22
    Unregistered
    Guest
    sorry to be one of those idiots who posts little thoughts all in short posts, but you could also say
    char **p;

    p=malloc(30*sizeof *p);

  8. #23
    Registered User oomakoo's Avatar
    Join Date
    Dec 2001
    Posts
    8
    Originally posted by quzah
    char **c;

    c = malloc( 30 ); //WRONG

    This only allocates 30 BYTES, not 30 pointers.

    c = malloc( 30 * sizeof( char* ) );

    This allocates 30 character pointers.

    Quzah.
    Whoops! This is why I like to use sizeof() for all my malloc()s. :-)

    My point was that by casting the compiler checks the type of the memory that I *think* I'm allocating. Without the cast the compiler doesn't warn me.

    i.e.

    char **c;

    /* no compiler warning */
    c = malloc( 30 * sizeof( char* ) );

    /* compiler warning */
    c = (char *)malloc( 30 * sizeof( char* ) );

  9. #24
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    then why waste CPU time calculating the value of a
    constant? Not only do you waste time by calculating the constant value of the size of char, you also waste more time multiplying it by how many elements you want.
    sizeof is an operator determined at compile time. That is why you can write something like

    Code:
    int main(void)
    {
        int A[50*sizeof(int)];
        return 0;
    }
    50*sizeof(int) isn't calculated by your program but by the compiler.

  10. #25
    Registered User
    Join Date
    Dec 2001
    Posts
    26
    Nick I have never heard that, but it makes a little sense. Do you have a reference to prove that though?
    And omakoo you are missing the point entirely, but I will give it a rest because I have tried to explain it to you about 3 times and quzah about the same...
    one fish two fish
    red fish blue fish

  11. #26
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    I'm not sure if the standard dictates that sizeof has to be handled at compile time or not, although I'm certainly at a loss as to how to do it otherwise. I mean, you can only pass it things, the size of which are defined at compile time, so it's clearly more efficient to handle at compile time...
    and its decision doesn't have anything to do with the memory of the variable you pass to it (obviously, since you can pass it keywords), so it will have to have some way to reference the variable declarations themselves to decide what to return (which would mean basically seeing the code, and your executable can't see it's own C code).
    Callou collei we'll code the way
    Of prime numbers and pings!

  12. #27
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    but I will give it a rest because I have tried to explain it to you about 3 times and quzah about the same...
    Really? You have? I've only posted twice in this thread, both on seperate issues. What exactly are you explaing to me over and over?

    Anyway...

    Your compiler optimizes most expressions at compile time, even with optimizations not cranked up. For example:

    for( x = 0; x < 4 * 23 + 5 / 15 * 7; x++ )

    You program does _NOT_ calculate that expression every single time through the loop. It happens one time, most likely at compile time. Additionally, I am fairly certin that the 'sizeof' operator is handled the same way.

    The compiler optimizes most things like this. You aren't wasting time "calculating the size of a constant" in either case. The only time this wouldn't be the case is when the return value of a function is deciding when to exit the loop. All other constants have the constant value subsituted for the expression either once at runtime, or during compile time. Example:
    Code:
    void myfun( CHARACTER *player )
    {
        int x;
        for( x=0; x < player->strength; x++ )
            ...do something...
    }
    In this example, the expression 'player->strength' will be replaced at the beginning of the loop, unless its value is subject to change in the loop itself. At least I beleive that's how it works. You'd have to do some digging on your compiler docs to find out for sure, but I'm fairly certin that's how it ends up.

    Quzah.
    Hope is the first step on the road to disappointment.

  13. #28
    Registered User oomakoo's Avatar
    Join Date
    Dec 2001
    Posts
    8
    Originally posted by Agrajag
    And omakoo you are missing the point entirely, but I will give it a rest because I have tried to explain it to you about 3 times and quzah about the same...
    Sorry that you've given up because I still don't understand. I did some tests that I posted earlier in the thread that show that there is no difference between casting and not casting when there are no errors. I still don't see any benefit from leaving out the cast. Sorry to be a pain but can anyone shed any light on the reason for leaving out the cast?

  14. #29
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    I think we're just lazy really. Personally, I don't cast my mallocs either, since the whole point of having a void pointer is that it will cast itself. Still, the only benefit I've ever really derived from doing so is that it becomes quite obvious when I forget to include stdlib.h

    On the other hand, casting does seem to offer the programmer some protection against himself, which can be a good thing, although certainly something that shouldn't be a part of the language. Also, casting does document the type of your variables, which could be nice (or it might be unnecisarily redundant, depending on the code). I know it seems a bit childish, but most of the programming books I've read, K&R included, cast their mallocs, so it seems kinda futile to criticise.
    Callou collei we'll code the way
    Of prime numbers and pings!

  15. #30
    Registered User
    Join Date
    Dec 2001
    Posts
    26
    To Quzah:
    I wasn't trying to explain anything to /you/ multiple times, I meant to say that I was trying to explain it to him and so were you, not that I was trying to explain it to you and him. Sorry for the confusion.

    To oomakoo I gave up trying to explain because I felt like a broken record and didn't want to plague people with a spam war. The only last thing I can suggest is try to run your casting malloc() code through lint's highest mode(which I havn't tried yet, but I think I can guess the outcome) and see what happens.
    one fish two fish
    red fish blue fish

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer memory question
    By Edo in forum C++ Programming
    Replies: 5
    Last Post: 01-21-2009, 03:36 AM
  2. Memory allocation question
    By dakarn in forum C Programming
    Replies: 11
    Last Post: 12-01-2008, 11:41 PM
  3. Another Question...memory allocation
    By quickclick330 in forum C Programming
    Replies: 4
    Last Post: 12-12-2007, 04:25 PM
  4. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 01:57 AM
  5. [C] Trouble with memory usage
    By BianConiglio in forum Windows Programming
    Replies: 1
    Last Post: 09-28-2004, 09:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21