3

Can somebody tell me about the memory allocation in c? What is the size of a char variable? 2 or 4? Why the difference in the address value between 2 neighboring char elements in an array is only 1?

char foo [] = {'a', 'b'};
printf ("This is the Address of val1 %d \n", &foo[1]);           // -1079295441
printf ("This is the Address of val2 %d \n", &foo[2]);           // -1079295440
printf ("The size of each array member is %d \n", sizeof(foo));  // 2
3
  • foo is a character array and as such has the same size as any other array since it's essentially just a pointer. Commented Sep 26, 2016 at 0:07
  • "What is the size of a char variable? 2 or 4?" It's 1 Commented Sep 26, 2016 at 0:08
  • 1
    @apokryfos, arrays are not pointers. They have various sizes depending on their element type and capacity. Commented Sep 26, 2016 at 0:30

2 Answers 2

3

You are not printing addresses correctly: an address is not an int, so you cannot use %d to print it. Use %p (for "pointer") instead, and cast the address to void* for printing:

printf ("This is the Address of val1 %p\n", (void*)&foo[1]);

Now your program produces this or similar output:

This is the Address of val1 0xffbd2fcf 
This is the Address of val2 0xffbd2fd0 
The size of each array member is 2

Two pointers are off by 1, which is the size of a single char. The size of the entire array is 2. If you want to print the size of a single element, use foo[0].

Sign up to request clarification or add additional context in comments.

4 Comments

@Oka There's no excessive cast in this answer
@Oka It's required. %p wants void*, so passing int* is considered UB.
Ah, this is an exception to the safe promotion of any pointer to void *. Guessing this has to do with varargs? Interesting.
@Oka Although any pointer can be used in the context where void* is needed (a call to a function with void* parameters, an assignment to a void* variables) printf has a variable-length argument list. Hence, by default pointers go unconverted.
0

Memory Allocation is a bit tricky, but it's easier than you think. It is, as the name implies, the usage and selection of data to optimize program function and speed. Basically, Memory Allocation is a "perk" of languages such as C/C++ that allow programmers to only use EXACTLY as much data as needed, freeing up memory for other computer functions.

Some good info to know about memory...

  • memory is known in "bytes", these are 8-bit "groups" of data.
  • A "bit" is a value that is either 0 or 1.

Variable sizes:

  • Char: 1 byte
  • Int: 4 bytes
  • double: 8 bytes

When neighboring elements are of the same type (for example, an array of chars), they will have a difference in address that increments/decrements by the value of the memory size. Since a char variable has a size of 1 byte, neighboring elements will have addresses that differ by 1.

Ex: char addresses: 1204, 1205, 1206, 1207... (1 byte)

int addresses: 1204, 1208, 1212, 1216... (4 bytes)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.