Malloc

This is a discussion on Malloc within the C Programming forums, part of the General Programming Boards category; Hey guys I am taking a Data Structure class now, and we're using C, but I don't have much experience ...

  1. #1
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427

    Malloc

    Hey guys I am taking a Data Structure class now, and we're using C, but I don't have much experience with C, and as so don't really remember how to use malloc, can anybody help me here. I know it's very similar to it's "new" counterpart in C++.

    Code:
    
    #include <stdio.h>
    
    int main()
    
    {
    
    	int *Articulos;
    	int Cantidad;
    
    	printf("Cuantos articulos va a facturar?\n");
    	scanf("%d",&Cantidad);
    
    	Articulos=(*int)malloc(Cantidad * sizeof int);
    
    
    	return 0;
    
    
    }

    It says something about "type int unexpected";
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    In C the proper way is
    Code:
    Articulos = malloc (Cantidad * sizeof int);

  3. #3
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    I tried that and it still gave me the error. HOwever when I take out the sizeof int thing it works. (at least it does not give me any errors)
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    try sizeof(int)

  5. #5
    c99
    c99 is offline
    Registered User
    Join Date
    Feb 2004
    Posts
    79
    incognito.

    Articulos=(*int)malloc(Cantidad * sizeof int);
    (*int) should be (int *)

    IMO I'd #include stdib.h and not worry about the cast at all.
    R.I.P C89

  6. #6
    Registered User
    Join Date
    Feb 2004
    Posts
    46
    You shouldn't cast the return of malloc in C. It hides the error of not including stdlib.h. An accepted convention of calling malloc is to multiply the amount by the size if the dereferenced pointer that you're assigning to, then return without a cast.
    Code:
    Articulos = malloc(Cantidad * sizeof *Articulos);
    This works even though Articulos is an uninitialized pointer because sizeof doesn't evaluate the expression you give it. It only returns the final type of that expression, which in this case would be int.

    Also, sizeof doesn't require parentheses around the expression unless it is a type name. So sizeof int is ill-formed. It should be sizeof(int).

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, 04: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, 06: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-02-2004, 12:36 AM

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