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 ...

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.

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.

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.

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).

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.

I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.