sizeof(<something>) --> WHAT DOES IT EXACTLY MEAN?

This is a discussion on sizeof(<something>) --> WHAT DOES IT EXACTLY MEAN? within the C Programming forums, part of the General Programming Boards category; Hi all, let's say that we have a program. In that program you just put characters and then push ENTER ...

  1. #1
    Beginning game programmer Petike's Avatar
    Join Date
    Jan 2008
    Posts
    64

    sizeof(<something>) --> WHAT DOES IT EXACTLY MEAN?

    Hi all,
    let's say that we have a program. In that program you just put characters and then push ENTER - for example "PUT character, ENTER , PUT character, ENTER, PUT character, ENTER, etc...". And after each ENTER, the program puts the pressed character into a "STRING".

    So if my inputs were:
    Code:
    'f' ENTER 'y' ENTER 'a' ENTER 'k' ENTER
    the output STRING would be:
    Code:
    "fyak"

    AND HOW TO DO IT? --> I wanted to do it by creating a "STRING of CHAR" (or ARRAY of CHAR) and then make for it a "DYNAMIC ALLOCATION" of memory (but I don't know WHICH CODE I should use to dynamic allocation - it's something with MALLOC(), but I don't know what exactly...).

    AND NOW MY QUESTION IS: --> It is possible to create an ARRAY that will be changing its size DURING the program running according to the situation (because before the starting of the program I don't know HOW BIG array I should create, because I don't know how many CHARACTERS I will add to the string)?



    ---> AND NOW TO MY MAIN PROBLEM: <---
    ==================================
    I have read that the function (for example for the type INT) "sizeof(int)" will return to me some number --> and that number should be the "SIZE OF INTEGER" --> AND THIS TERM I DON'T UNDERSTAND.
    I understand that the concrete NUMBER (or character), for example the number "6369" needs in program some amount of memory (for example "20 BYTES", ...I know, it's probably a wrong number but it doesn't matter), but HOW BIG MEMORY DOES THE TYPE "INT" (or "char" or "real") NEED??? --> it's JUST ONLY a TYPE, and NOTHING CONCRETE (like '6369').
    ==================================


    So if someone could explain to me, what does this mean:
    Code:
    sizeof(<something>)


    I have studied "C" just one semester at university, so I would appreciate simple answers --> something that I will be able to imagine.
    But thanks very much for every answer.

    Petike.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    To explain sizeof(), you'd have to understand that "memory" consists of little compartments that can hold some small unit of data. Usually, the smallest separate unit of data is a byte - 8 bits for most systems. That can hold 256 different values (2^8 different values).

    Sizeof() tells you how many bytes a particular type takes up. So for example sizeof(int) would be 4 in most (recent) systems. sizeof(char) should always be one, because that's part of the C language definition that a char is equal to the smallest unit that the system can use.

    Now, for dynamic memory allocation, you need to understand a whole lot more, and I suggest that you first read the tutorial on www.cprogramming.com, then ask further questions based on how far you got from that.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Yes, dynamic memory allocation "has something to do with malloc()". Technically, you won't have an array, but a pointer to memory; once you know how much memory you need, you ask for it and put its address in the pointer.

    sizeof(int), for instance, gives you the number of bytes of memory that are reserved for you when you declare an int variable. On many systems nowadays, sizeof(int)=4, which means that when you say int i; the compiler reserves four bytes for you to put an integer in. (If the integer can't be represented in four bytes, you are plain out of luck.) All variables of the same type get the same amount of space.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,437
    Well to store a number, you need a certain amount of space to store the information about that number. For integers, it's an easy calculation which is just log2(n).

    So 6369 would require 12.63 bits of information, being that it lies between 4096 (2^12) and 8192 (2^13).

    Fortunately, we only have to count bytes (usually multiples of 8-bits), so an int would typically contain either 2 or 4 bytes. For us, this is good news since even the smallest int at 16 bits is going to be big enough to hold the 12 or so bits we need for our test value.

    So whether you say
    int a = 24;
    int b = 1234567;
    both take up the same amount of space in memory, namely a small block of memory who's size is sizeof(int). Or you could say sizeof(a) to get the size of a known variable as well.


    The size of a whole array is just the number of elements of the array, say 80, multiplied by the size of an element of the array, say an int.
    so
    int arr[80];
    would give typically a size of 320 bytes (sizeof(int)==4, times 80 elements = 320).
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Beginning game programmer Petike's Avatar
    Join Date
    Jan 2008
    Posts
    64
    thanks for the answers,

    so if i have the array:
    int arr[50] --> the size of it will be "4 * 50 == 200" - so I should allocate at least 200 bytes, thus:
    Code:
    arr = sizeof(sizeof(int) * 50)
    - this I understand because I know how many elements are in the array.



    But what if don't know how many elements will be in the array (just the very first example in my first post) - if it depends on the USER? --> I should allocate at least:
    Code:
    arr = sizeof(sizeof(char) * ???)
    How should I solute this?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,409
    But what if don't know how many elements will be in the array (just the very first example in my first post) - if it depends on the USER?
    I am coming from a C++ background here, so my view may be tainted... but: allocate some reasonable size. Read up to that size. If there is more input, realloc() (or something along those lines) for a bigger size, then read more. Keep on doing this until there is no more input.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Beginning game programmer Petike's Avatar
    Join Date
    Jan 2008
    Posts
    64
    Quote Originally Posted by laserlight View Post
    Read up to that size. If there is more input, realloc() (or something along those lines) for a bigger size, then read more. Keep on doing this until there is no more input.
    Yes I have heard about this progress and I probably don't have any troubles with this steps.
    I would just like to know it there is some other method, something other than always memory reallocating?


    Thanks anyway.

    Petike.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,424
    Yes there are, but they are platform specific (I've done such a thing myself); nothing in the C standard.
    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.

  9. #9
    Beginning game programmer Petike's Avatar
    Join Date
    Jan 2008
    Posts
    64

    Thanks for answers...

    OK,
    thank you very much for all your answers.
    I am a bit more clever now.

    Petike.

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    you could find out how much memory you need beforehand, and then allocate that.

  11. #11
    Registered User
    Join Date
    Nov 2007
    Location
    Bangalore, India
    Posts
    24
    I have read that the function (for example for the type INT) "sizeof(int)" will return to me some number ...
    sizeof() is an operator and not a function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. small -> big -> bigger -> bigger than bigger -> ?
    By happyclown in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 03-11-2009, 12:12 PM
  2. Dev-C++ -> Tools - > Editor -> Syntax
    By Yuri2 in forum C++ Programming
    Replies: 19
    Last Post: 07-03-2006, 07:48 AM
  3. > > > Urgent Help < < <
    By CodeCypher in forum C Programming
    Replies: 2
    Last Post: 01-31-2006, 01:06 PM
  4. electricity > AC circuits > tesla coil
    By dbaryl in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 03-14-2002, 01:16 PM

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