Thread: question about malloc

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    96

    question about malloc

    hi i have this program i wanted to know what int* is doing. i know that if i enter a number. it will be (y*int) for example if i enter 4 it is. 4X4=16. because int has 4 bytes and entering 4 marks. int has 4 bytes.

    so my question is what is int* doing. also how can i printed out the size of the dynamic memorry



    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    
    main()
    {
          int *marks;
          int y
          int t;
          
          printf("enter the mark);
          scanf("%i",&y);
          marks=(int*)malloc(y*sizeof(int)) //what is int * doing  why does it equals to mark
    
    }

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,337
    In that context, (int *) is a cast. It it casting the pointer returned by malloc as a pointer to an int.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    96
    so u are saying that it is return (int*) as a pointer if i understood you. and the marks will point to int* right. how can i print the total bytes of the dynamic array

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    int* is a type. You cannot point to a type. It points to whatever value malloc returns whose type is void*. The cast changes the type of the returned value from void* to int* (unnecessary in C).
    The only way to print the size of the allocation is to remember how much you've allocated. There is no standard method in C to get the size of an existing allocation.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    > marks=(int*)malloc(y*sizeof(int)) //what is int * doing why does it equals to mark
    In the context of a valid C program, compiled with a C compiler - absolutely nothing.

    It's a waste of key strokes.

    There's a FAQ on (NOT) casting the return result of malloc.
    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.

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. Alternative to malloc
    By stellastarr in forum C Programming
    Replies: 13
    Last Post: 04-30-2007, 04:10 PM
  3. malloc, calloc question
    By chen1279 in forum C Programming
    Replies: 12
    Last Post: 09-07-2006, 05:54 PM
  4. Question about malloc()
    By cdalten in forum C Programming
    Replies: 6
    Last Post: 05-12-2006, 10:57 AM
  5. 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