Newbie question: Problem with malloc

This is a discussion on Newbie question: Problem with malloc within the C Programming forums, part of the General Programming Boards category; Hi, this code seems to work, but I don't know if it really is working because I get a different ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    5

    Newbie question: Problem with malloc

    Hi, this code seems to work, but I don't know if it really is working because I get a different size of what I spect. The code is:

    Code:
    #include <stdlib.h>
    
    int *buff
    
    main(int argc, char argv[])
    {
    if ((buff = malloc(sizeof(int)*4)) = NULL) {
    printf("Malloc didn't work"\n);
    } else {
    printf("Malloc worked\n");
    *buff = 1;
    printf("%d\n", *buff);
    printf("%d\n", sizeof(buff));
    The problem that I have is that didn't understand is the second printf because I expect to be bigger because the call to malloc.
    I suppose malloc is working, but don't know what is doing?.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    A comparison is ==. Post the actual code rather than trying to retype.

    buff is a pointer, sizeof(buff) is the size of this pointer.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    5
    Sorry about the mistake typing the = for the Null, that's ok in my code. But think that the main question you have solve it. Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. validation problem in a loop (newbie question)
    By Aisthesis in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2009, 10:47 PM
  2. struct problem and general C question
    By techrolla in forum C Programming
    Replies: 8
    Last Post: 01-09-2004, 12:37 AM
  3. C++ newbie / linux not so newbie question
    By goldmonkey in forum C++ Programming
    Replies: 7
    Last Post: 12-13-2003, 11:27 AM
  4. malloc problem in SUN in 64-bit compilation
    By ylzhang in forum C Programming
    Replies: 6
    Last Post: 05-31-2003, 11:48 AM
  5. newbie problem
    By face_master in forum C++ Programming
    Replies: 7
    Last Post: 05-15-2002, 08:56 AM

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