malloc question..

This is a discussion on malloc question.. within the C Programming forums, part of the General Programming Boards category; hat the plus 4 means in the last part how it change the number of cells in the "i" row?? ...

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    malloc question..

    hat the plus 4 means in the last part
    how it change the number of cells in the "i" row??

    Code:
    int **arr;
    arr=(int**)malloc(... )
    for i=0..
    arr[i]=(int*)malloc(sizeof(int)+4);

  2. #2
    Maz
    Maz is offline
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    You should *REALLY* learn to read manuals. Why don't you try searching for example for "man malloc" with google?

  3. #3
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i know that if its *4 then
    4 cells

    what if +4
    ??

  4. #4
    Maz
    Maz is offline
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    read the documentation of malloc as I said. It tells what the sole argument of malloc means.

  5. #5
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i know what it means .
    i know that +4 means adding 4 bits

    how it change the number of cells in the "i" row??

  6. #6
    Maz
    Maz is offline
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    It does not add 4 bits. It adds 4 bytes.

    And you get the answer when you think how many bytes one item in your array needs.

  7. #7
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    integer is 8 bytes
    so it doesnt add any new cells??

  8. #8
    Always learning
    Join Date
    Apr 2009
    Posts
    25
    there's not a fixed size for int. It's usually 4, but can be different in other systems. That's why sizeof() exists, it's a function that returns the size (in bytes) of a data type.

  9. #9
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    integer is 8 bytes
    so it doesnt add any new cells??
    It's 8 bytes on a 64-bit system. It's 4 bytes on a 32 bit system.

    We can't really tell you if it adds new cells. The call just returns a pointer to a certain area in memory equal to the size you specified. It's impossible to tell if that's what you want and what you mean by adding cells - because it depends on how you work with that area of memory.

    You should *REALLY* learn to read manuals.
    Why don't you, by the way? You've been asked this many times, and you seem to make no improvement towards helping yourself.

  10. #10
    Maz
    Maz is offline
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    Actually, integer is standardized to 4 bytes. But pointer to integer may be 4 or 8 bytes (or something else) depending on the machine's architecture.

  11. #11
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Quote Originally Posted by Maz View Post
    Actually, integer is standardized to 4 bytes. But pointer to integer may be 4 or 8 bytes (or something else) depending on the machine's architecture.
    No it's not. I've used systems where an integer has been 8 bits, 16 bits, 32 bits, and 64 bits.

  12. #12
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,263
    Quote Originally Posted by Maz View Post
    Actually, integer is standardized to 4 bytes. But pointer to integer may be 4 or 8 bytes (or something else) depending on the machine's architecture.
    Not standardized... Just common. Most compilers I've used on x86-64, for instance, default to a 32-bit integer type (you must specify "long long" to get 64 bits) but this is not specified by the standard.

    Other 64-bit architectures I've worked with default to 64-bit integers. Sometimes the compiler lets you select.

    You should not assume anything, especially when somebody can change it via compiler switch.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  13. #13
    Maz
    Maz is offline
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    Quote Originally Posted by bithub View Post
    No it's not. I've used systems where an integer has been 8 bits, 16 bits, 32 bits, and 64 bits.
    Youre right. int size is not standardized. Apologies

    I guess I should use uint32_t from now on (as if it was supported by all libraries...)
    Last edited by Maz; 04-15-2009 at 03:26 PM.

  14. #14
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Doesn't that standard say that int is the natural word size of the processor? In other words, a pointer and an int should always be the same size? And a 64-bit compiler should always make a 64-bit int?

    That's what I understood, but it sounds like I'm mistaken?

  15. #15
    Maz
    Maz is offline
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    I won't say a word about standard anymore But that really is not the case. I've been doing some hell of a porting work to get pointer arithmetics working after we changed from 32bit arch to 64 bit arch... (there were pointers casted to ints, and then calculated some offsets && casted them back. Also we had some ID numbers for things, and actually those IDs were then used as memory locations... A mess...

Page 1 of 2 12 LastLast
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. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 01:14 PM
  3. Alternative to malloc
    By stellastarr in forum C Programming
    Replies: 13
    Last Post: 04-30-2007, 05:10 PM
  4. malloc, calloc question
    By chen1279 in forum C Programming
    Replies: 12
    Last Post: 09-07-2006, 06:54 PM
  5. Question about malloc()
    By cdalten in forum C Programming
    Replies: 6
    Last Post: 05-12-2006, 11:57 AM

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