Help for typedef arrays

Hello All

For the vptr pointer, is it allocated as [3][10] or [10][3]??
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int nshoot = 10;
	typedef double vect_t[3];

	vect_t *vptr = (vect_t *)malloc(nshoot * sizeof(double));

	double a[30];

	for (int i = 0; i < 30; i++) { a[i] = i+2; }

	for (int i = 0; i < 3; i++) {
		for (int j = 0; j < nshoot; j++) {

			vptr[j][i] = a[nshoot*i] + j  or vptr[i][j] = .. ???
			
		}
	}

	
	free(vptr);
Last edited on
Firstly:
Dereferencing a pointer to some type T gives you the type T.

The type of vptr is pointer-to vect_t. Dereferencing vptr therefore gives you a vect_t. vect_t[10] is the same type as double[3][10]

Secondly, the bounds of the array are wrong:
vptr points to the beginning of a block of memory of size nshoot * sizeof(double) (i.e., typically 80 bytes). The block of memory contains enough space for exactly sizeof(double) * nshoot / sizeof (vect_t) objects of type vect_t. Did you mean to say sizeof(vect_t) instead of sizeof(double)?
Last edited on
> vect_t *vptr = (vect_t *)malloc(nshoot * sizeof(double));

Why not this?
1
2
3
vect_t *vptr = new vect_t[nshoot];
///
delete [] vptr;

Not only does it save you from not having to figure out (and get wrong) complicated sizeof expressions, it will also carry on working if you decide to suddenly give vect_t a constructor.
yes I meant sizeof(vect_t). Sorry for typing error.
I need to write in C, so there is no new operator. But i tried both as vptr[i][j] and vptr[j][i] and both run without any error. I could not understand why there is no error if vect_t[10]==double [3][10] ?
If you're writing in C, then just write
vect_t *vptr = malloc(nshoot * sizeof(*vptr));

> But i tried both as vptr[i][j] and vptr[j][i] and both run without any error.
> I could not understand why there is no error if vect_t[10]==double [3][10] ?
All the machine cares about is that vptr is the start of a block of 240 bytes capable of storing 30 doubles.
It really doesn't care whether it's [3][10] or [10][3]

Static code analysers like Klockwork and Flexelint might spot that the loop subscripts could be out of bounds compared to the declarations.
thnks but why sizeof(vptr) is returning 8 (not 240?) and sizeof(vptr[0]) is return as 24 not(80) ?
vptr is a pointer. it is a 'word sized' integer, nothing more. word sized means 'machines register size for ints' 'more or less'. It can also be tied to the OS (eg win32 on a 64 bit machine can be done, and that sets word back to 32 even though the registers support 64). All pointers are the same size (which is the size of one integer of one type or another) on modern systems at the c++ level. Older OS / systems or crude embedded systems or something may still support 2 pointer sizes (near and far concept). I don't know if the assembly still uses this idea or not, but it would be hidden from you if the compiler decided to optimize the 'near' pointers to 1/2 size.

vptr[0] is an array of 3 doubles. 3*8 is 24. a double is 8 in size.

sizeof(pointer) is kind of a new to c++ trap/confusing point. It gives you the size of the pointer, not the data pointed to, which makes sense once you get that, but its easy to think you would get the size of the data instead.
Last edited on
Topic archived. No new replies allowed.