c - How is memory allocated in an array of integer pointers? -



c - How is memory allocated in an array of integer pointers? -

i practicing c programming , trying create 2d array fixed rows, variable columns. so, used "array of pointers" concept i.e. created array such int* b[4].

this code written:

#include <stdio.h>

int main(void) {

int* b[4]; int c[]={1,2,3}; int d[]={4,5,6,7,8, 9}; int e[]={10}; int f[]={11, 12, 13}; b[0]=c; b[1]=d; b[2]=e; b[3]=f; //printing b[0][0] b[0][2] i.e. c[0] c[2] printf("b[0][0]= %d\tb[0][1]=%d\tb[0][2]=%d\n", b[0][0], b[0][1], b[0][2]); //printing b[1][0] b[1][5] i.e. d[0] d[5] printf("b[1][0]= %d\tb[1][1]=%d\tb[1][2]=%d\tb[1][3]=%d\tb[1][4]=%d\tb[1][5]=%d\n", b[1][0], b[1][1], b[1][2], b[1][3], b[1][4], b[1][5]); //printing b[2][0] i.e. e[0] printf("b[2][0]= %d\n", b[2][0]); //printing b[3][0] b[3][2] i.e. f[0] f[2] printf("b[3][0]= %d\tb[3][1]=%d\tb[3][2]=%d\n", b[3][0], b[3][1], b[3][2]); homecoming 0;

}

and output expected:

b[0][0]= 1 b[0][1]=2 b[0][2]=3 b[1][0]= 4 b[1][1]=5 b[1][2]=6 b[1][3]=7 b[1][4]=8 b[1][5]=9 b[2][0]= 10 b[3][0]= 11 b[3][1]=12 b[3][2]=13

so, think memory has been allocated way: but, question chimes in when code executed:

#include <stdio.h>

int main(void) {

int* b[4]; int c[]={1,2,3}; int d[]={4,5,6,7,8, 9}; int e[]={10}; int f[]={11, 12, 13}; b[0]=c; b[1]=d; b[2]=e; b[3]=f; int i, j; (i=0; i<4; i++) { (j=0; j<7; j++) { printf("b[%d][%d]= %d ", i, j, b[i][j]); } printf("\n"); } homecoming 0;

}

and output unusual:

b[0][0]= 1 b[0][1]= 2 b[0][2]= 3 b[0][3]= 11 b[0][4]= 12 b[0][5]= 13 b[0][6]= -1079668976 b[1][0]= 4 b[1][1]= 5 b[1][2]= 6 b[1][3]= 7 b[1][4]= 8 b[1][5]= 9 b[1][6]= -1216782128 b[2][0]= 10 b[2][1]= 1 b[2][2]= 2 b[2][3]= 3 b[2][4]= 11 b[2][5]= 12 b[2][6]= 13 b[3][0]= 11 b[3][1]= 12 b[3][2]= 13 b[3][3]= -1079668976 b[3][4]= -1079668936 b[3][5]= -1079668980 b[3][6]= -1079668964

one can observe b[0][i] continues seeking values b[3][i], array b[2][i] continues seeking values b[0][i] followed a[3][i], array b[3][i] , b1[i] terminate.

every time when programme executed, same pattern followed. so, there more on way memory allocated, or mere co-incidence?

as hrishi notes in comments, reason happening you're trying access beyond end of arrays. what's happening?

the short version you're reading past end of arrays, , reading next array (or unallocated memory). why happening?

a brief aside on c-style arrays

in c, arrays pointers1. b pointer start of array, *b homecoming first element of array (which in case pointer start of b[0].

the syntax b[i] syntactic sugar; it's same *(b + i), doing pointer arithmetic. it's literally saying: "the memory address i places after b; tell me what's pointing there"2.

so if at, example, b[0][3], can translate *((*b) + 3): you're getting address of start of b, , getting whatever stored 3 memory address away that.

so what's happening you?

as happens, computer has stored b[3] starting @ address. that's telling you: computer placing each sub-array in memory. because arrays laid out contiguously, 1 position right after in memory (that's how pointer arithmetic trick works). because defined c, d, e, , f individually, memory manager did not allocate them contiguous 1 another, instead set them wherever wanted. resulting pattern came with. best can tell, arrays laid out in memory this:

-------- | e[0] | -------- | c[0] | -------- | c[1] | -------- | c[2] | -------- | f[0] | -------- | f[1] | -------- | f[2] | --------

d located somewhere in memory well, before or after contiguous block; don't know.

however can't rely on this. mention in footnote, ordering of allocated memory not defined language, (and does) alter depending on number of factors. run same code tomorrow, , won't same.

the next obvious question is: "what b[0][6]? why such weird number?"

the reply you've run out of array, , you're trying read unallocated memory.

when programme gets run, operating scheme gives chunk of memory , says "here, whatever like." when declare local variable on stack (as have here) or on heap (with malloc), memory manager grabs of memory , gives you4. memory you're not using still there, have no thought stored there; it's leftover info whatever lastly using particular chunk of memory. reading undefined behaviour in c, because have no command on stored in memory.

i should note other languages (java, instance) wouldn't allow this; throw exception because you're trying access beyond bounds of array. c, however, isn't smart. c likes give plenty rope hang yourself, need own bounds checking.

1 simplification. the truth more complicated 2 implementation why array indices start @ 0. 3 illustration of undefined behaviour, bad. means result isn't consistent. it's happening same way every time, on computer, right now. seek on friend's computer, or on computer hr now, , might different. 4 oversimplification, purposes it's close plenty true

c arrays pointers

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -