Thread: urgent regarding malloc()

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    32

    urgent regarding malloc()

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<malloc.h>
    struct list{
    int a;
    struct list *ptr;
    };
    void main(void)
    {
    typedef struct list *nn ;
    nn node;
    printf("\t%d",sizeof(nn));
    node=(nn)malloc(sizeof(nn));
    printf("\tenter node\n");
    scanf("%d",node->a);
    printf("\t%d",node->a);
    node->ptr=NULL;
    free(node);
    getch();
    }
    variable a is input from the user
    but it gives a garbage value as output.
    Last edited by samirself; 01-12-2005 at 09:59 AM. Reason: mistake

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    A good reason to argue that hiding pointers is a bad idea:
    Code:
    node=(nn)malloc(sizeof(nn));
    You're only allocating enough memory for a pointer.

    There's a zillion other things wrong with that surprisingly short program that I'm not even going to get into. I don't know if I could throw that many bad habits into such a short program even if I tried.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    32
    plz write it in a corect manner,if possible,
    thanks

  4. #4
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Sure, why not?
    Code:
    int main(void)
    Take good note.
    Code:
    int main(void)
    {
    	typedef struct list* nn;
    	nn node;
    	printf("\t%d",sizeof(struct list));
    	node=(nn)malloc(sizeof(struct list));
    	printf("\tenter node\n");
    	scanf("%d",&node->a);
    	printf("\t%d",node->a);
    	node->ptr=NULL;
    	free(node);
    	getch();
    	return 0;
    }
    Indentation is nice. If you really want to keep the pointer hidden, you'll need the size of the struct, not the pointer to the struct. In addition, scanf will need an address of a variable, or else it'll crash.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > node=(nn)malloc(sizeof(struct list));
    I'd prefer removing the cast

    Code:
    node=malloc( sizeof *node );
    Plus I notice the OP used #include<malloc.h> instead of #include<stdlib.h> which makes me suspicious that they're using some brain-dead fossil compiler.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    >I'd prefer removing the cast
    Good call. I'm too used to C++, which would complain about that. Also, I wasn't sure if it was safe to dereference node since it hadn't been allocated any memory.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Despite what it looks like, sizeof *node is NOT a dereference of the pointer.

    sizeof is compile-time, and the only "dereferencing" is done on a type basis. So what you get is the size of the type which a pointer points to. This can all be done without any reference to actual memory locations - it's just a traversal of the compiler's internal type tables.

    Oh, and one more thing - the return result of malloc needs to be checked (for being not NULL) before it is dereferenced.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Good to know. Thanks a lot, Salem. Useful as usual.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc + segmentation fault
    By ch4 in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 03:46 PM
  2. 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
  3. Malloc and calloc problem!!
    By xxhimanshu in forum C Programming
    Replies: 19
    Last Post: 08-10-2005, 05:37 AM
  4. malloc() & address allocation
    By santechz in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 09:08 AM
  5. malloc always setting length of 8?
    By Zarkhalar in forum C++ Programming
    Replies: 7
    Last Post: 08-01-2004, 11:36 PM