1
char* pointer;
char array[10];

I know that the memory of second one is already allocated in the buffer. But, I don't know how exactly pointer works in terms of memory allocation. How much space does pointer initially takes before it is allocated by the programmer with malloc or calloc? Additionally, if I initialize it like this

char* pointer;
pointer = "Hello World!";

If the memory isn't allocated before it's initialized with some random string size, how is this being initialized? Wouldn't there be any error involved?

I was just programming with pointers and arrays mechanically w/o really knowing how it works inside the computer. And, I thought I should understand this perfectly for better programming practice.

slow
  • 805
  • 3
  • 13
  • 27

7 Answers7

3

Pointer is just to store address of one variable. i.e, if you say char* it stores address of one character. like int i=9; means memory of sizeof(int) is reserved and labled as "i" inyour program. Like wise char* c; means memory of size(char*) is reserved and labled as "c"; in c="hello"; "h","e","l","l","o" got seperate "continous" memory allocated. and pointer c points to first char "H".

consider in memory HELLO is store before string "India".

"HELLOINDIA."

for char *c="HELLO"; c[5] returns I. for char c[5]="HELLO"; c[5] is array out of bound error.

krishna
  • 3,148
  • 5
  • 19
  • 24
2

char array[10] , It will reserve memory of 10 bytes on stack frame of function in which you have declared it.

Whereas in char *ptr = "hello" ptr will get 4 bytes memory on stack in 32 bit O.S,also "hello" is string literal which will get stored on non-bss part of your executable,and ptr is pointing to it from stack frame.

Hitesh Menghani
  • 977
  • 1
  • 6
  • 14
1
  1. This declaration:

    char *pointer;
    

    reserves sizeof(char *) bytes for the pointer value. No other memory is allocated.

  2. This declaration:

    char array[10];
    

    reserves 10 bytes for the array.

  3. In this case:

    char *pointer;
    pointer = "Hello World!";
    

    You still have a single pointer (sizeof(char *) in size) that points to a string literal somewhere in memory - no other allocations are taking place. Storage for the string literal is worked out by your toolchain at compile time.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
  • if *pointer reserves 8bytes in the memory, only 8bytes of string can be initialized on that pointer? – slow Mar 11 '13 at 05:30
  • No, you can `malloc` as much memory as you want and store the pointer to it into your `pointer` variable. The memory reserved for the pointer itself and the memory allocated for whatever it points to are pretty much completely unrelated. – Carl Norum Mar 11 '13 at 05:31
1

Pointers and arrays are completely different things. Any similarity and confusion between the two is an artifact of the C language.

A pointer is a variable which holds the location of another variable. An array (in C) is an aggregate of values of identical type, consecutively allocated in memory.

Arrays are accessed via arithmetic upon a pointer to the base element, [0]. If an expression which refers to an array is evaluated, the value which emerges is a pointer to element [0].

int array[10];
int *p = array;     /* array expression yields pointer to array[0] */
int *q = &array[0]; /* q also points to same place as p */

The array notation in C is a sham which actually works with pointers. The syntax E1[E2] means the same thing as *(E1 + E2) (assuming that E1 and E2 are sufficiently parenthesized that we don't have to be distracted by associativity and precedence.) When we take the address of an element via &E1[E2], this is the same as &*(E1 + E2). The address-of and dereference "cancel out" leaving E1 + E2. Therefore, these are also equivalent:

int *r = array + 3;
int *q = &array[3];

Because array[i] and pointer[i] are both valid syntax, people in the newbie stage (mistaking syntax to be semantics) conclude that arrays and pointers are somehow the same thing.

Spend some time programming in an assembly language. In assembly language, you might define some storage like this:

 A: DFS 42          ;; define 42 words, labelled as A.

Then use it like this:

 MOV R13, A         ;; address of A storage is moved into R13
 MOV R1, [R13 + 3]  ;; load fourth word, A[3]

R13 points to the storage. That doesn't mean A is a pointer. A is the name of the storage. Of course to use the storage, we need its effective address and so a reference to A resolves to that. When the code is assembled and linked, that MOV instruction will end up loading some numeric address into R13.

C is just a higher level assembly language. Arrays are like named storage which resolves to its effective address (being a pointer data type).

Arrays do not always resolve to their effective address. sizeof a calculates the size of an array a in bytes, whereas sizeof p calculates the size of the pointer data type p.

Another difference is that the name of an array cannot be made to refer to any place other than that array, whereas we can assign values to pointers:

array++;           /* invalid */
array = &array[0]; /* invalid */
p = &array2[0];    /* valid */
p++;               /* valid: point to the next element in array2 */
Kaz
  • 55,781
  • 9
  • 100
  • 149
0

A pointer takes up whatever space is required to describe a memory location. In general (but not always), the size of a pointer is the same as the bit-size of the processor/OS/program-mode (e. g. 8 bytes for a 64-bit program on a 64-bit OS, 4 bytes on a 32-bit program, etc.). You can find this out using

sizeof (void *)

In the case of

char* pointer;
pointer = "Hello World!";

You'll have one pointer allocated in R/W memory, plus the space for the string (13 bytes, including the trailing null byte) in R/O memory, perhaps more if the next object in memory is aligned on better than a byte boundary). Note that the same R/O space would be allocated for

printf("Hello World!");

so that actually has nothing to do with the pointer. In fact, most optimizing compilers would notice that the two strings are exactly the same and only allocate it once in R/O memory.

DoxyLover
  • 3,366
  • 1
  • 15
  • 19
  • Nitpick: strictly speaking, pointer size doesn't have to have anything to do with word size of a processor, no do all varieties of pointers have to be the same size on a given machine. – Carl Norum Mar 11 '13 at 06:00
0

I don't know how exactly pointer works in terms of memory allocation. How much space does pointer initially takes before it is allocated by the programmer with malloc or calloc?

pointer itself is just a data type, like int, char...etc. it point to a memory address (or NULL).

it can be malloc, become a pointer point to a block of memory you ask.

you very likely mistaken that pointer = malloc, it's not.

moeCake
  • 512
  • 4
  • 15
0

when you define a pointer in c, e.g. char *c; or int *i it will reserve a memory equal to sizeof(char *) and sizeof(int *) respectively.

But the actual memory reserved depends on your system/OS, if it is 64-bit it will reserve 8 bytes, if it is 32-bit it reserves 4 bytes.

In case of declaring char *c = "Hello world";

the string "Hello world" can be stored any where in memory but here c points to first character of the string that is 'H'.

Kinjal Patel
  • 405
  • 2
  • 7