Thread: malloc and printf of its size

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    61

    malloc and printf of its size

    Hi there,

    I am allocating a 1D array of integers by using the malloc function (I report the code down here).
    Once allocated the memory, I would like to print to screen the size of such array by means of sizeof(), but it seems that what I obtain does not correspond to the number of elements of the newly allocated array, but the size of one element of type int.

    To make it easy: I allocate 13 elements inside array _esup2, but when I printf to screen the sizeof(_esup2), I obtain 4 instead of 13.

    What am I doing that is not correct? I need to verify this because the bogger code within this is enclosed crashes for wrong memory allocation.

    Here the code, I hope someone can help
    Thank you in advance,

    All the best
    S.M.

    Code:
           int nnode = 13;
    	int *_esup2;
    
    	_esup2 = (int*) malloc((sizeof(int[nnode+1])));
    
    	printf("size(ESUP2) %d\n", sizeof(_esup2));
    	;

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    sizeof(_esup2) is the sizeof the pointer. It's your job (not the system's) to know how much memory is allocated to a pointer.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The answer is that you cannot.
    Firstly, you are printing the size of the pointer, not the data.
    But even if you dereference, you will get only the size of one element, because sizeof is compile-time and the compiler cannot know how big a dynamic array is.
    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.

  4. #4
    Registered User
    Join Date
    Aug 2006
    Posts
    61
    I see, thank you very much to both for replying so quickly. I wasn sure if that was the reason.

    Given that I am allocating correctly then, I have a question regarding a variable-size array that becomes larger within a certain loop. Should I keep asking this here or should I open a new thread on this (I havent been able to find anything on that)?
    Thank you very much in advance
    S.M.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If your new question fits under the heading "malloc and printf of its size" then you can ask here, otherwise there's no harm in starting a new thread.

  6. #6
    Registered User
    Join Date
    Aug 2006
    Posts
    61
    Hi Tabstop,

    thank you, I will start a thread then.
    All the best
    S.M.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory leaks
    By TehOne in forum C Programming
    Replies: 4
    Last Post: 10-10-2008, 09:33 PM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. An exercise in optimization
    By Prelude in forum Contests Board
    Replies: 10
    Last Post: 04-29-2005, 03:06 PM
  4. Replies: 4
    Last Post: 04-01-2003, 12:49 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM