matrix pointer


 
Thread Tools Search this Thread
Top Forums Programming matrix pointer
# 1  
Old 03-02-2009
matrix pointer

Can anyone tell me what the following statements do?
float (*tab)[MAXCLASS];
tab=(float (*)[MAXCLASS]) calloc(MAXCLASS,
(MAXCLASS+1)*sizeof(float));
# 2  
Old 03-02-2009
Code:
/* creates a pointer named tab that points to an array of MAXCLASS items of type float */
float (*tab)[MAXCLASS];

/* 
  allocates memory for MAXCLASS elements each of size (MAXCLASS+1)*sizeof(float) bytes and
  initializes all memory locations to zeros and casts the pointer to the appropriate type
 */
tab=(float (*)[MAXCLASS]) calloc(MAXCLASS, (MAXCLASS+1)*sizeof(float));

The last one allocates too much memory and IMO the calloc should be...
Code:
/*
  allocates memory for MAXCLASS elements each of sizeof(float) bytes and sets
  the memory locations to zeros and casts the pointer to the appropriate type
 */
tab=(float (*)[MAXCLASS]) calloc(MAXCLASS, sizeof(float));

# 3  
Old 03-02-2009
Forgot to add that though the two statements are equivalent their difference is that this one allocates memory on the stack...
Code:
float (*tab)[MAXCLASS];

while the one below allocates memory from the heap...
Code:
tab=(float (*)[MAXCLASS]) calloc(MAXCLASS, (MAXCLASS+1)*sizeof(float));

and written as is the heap one uses more memory than the stack one unless written as shown in my previous post.
# 4  
Old 03-03-2009
Thanks very much.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Pointer for 2D array seems to be 3D in C

I am struggling with the pointer to 2D-array (cf: 2D array of pointers). Can anybody help me elaborate how the pointer x moves in the memory to access the individual of y, especially the high lighted lines? I have talked to one of the curators of the forum, but I am still not quite clear. Here... (1 Reply)
Discussion started by: yifangt
1 Replies

2. Programming

pointer problem

Does anyone know? int x = 1; int *p = &++x; //ok ! int *q = &x++; //gives an error :O why the first pointer is ok but the second is an error? (13 Replies)
Discussion started by: nishrestha
13 Replies

3. Shell Programming and Scripting

awk? adjacency matrix to adjacency list / correlation matrix to list

Hi everyone I am very new at awk but think that that might be the best strategy for this. I have a matrix very similar to a correlation matrix and in practical terms I need to convert it into a list containing the values from the matrix (one value per line) with the first field of the line (row... (5 Replies)
Discussion started by: stonemonkey
5 Replies

4. Ubuntu

How to convert full data matrix to linearised left data matrix?

Hi all, Is there a way to convert full data matrix to linearised left data matrix? e.g full data matrix Bh1 Bh2 Bh3 Bh4 Bh5 Bh6 Bh7 Bh1 0 0.241058 0.236129 0.244397 0.237479 0.240767 0.245245 Bh2 0.241058 0 0.240594 0.241931 0.241975 ... (8 Replies)
Discussion started by: evoll
8 Replies

5. Programming

pointer conversion in c

hi there fellos could someone teach me how to convert a character pointer into an integer using c much love (1 Reply)
Discussion started by: surubi_abada
1 Replies

6. Shell Programming and Scripting

diagonal matrix to square matrix

Hello, all! I am struggling with a short script to read a diagonal matrix for later retrieval. 1.000 0.234 0.435 0.123 0.012 0.102 0.325 0.412 0.087 0.098 1.000 0.111 0.412 0.115 0.058 0.091 0.190 0.045 0.058 1.000 0.205 0.542 0.335 0.054 0.117 0.203 0.125 1.000 0.587 0.159 0.357... (11 Replies)
Discussion started by: yifangt
11 Replies

7. Programming

pass a pointer-to-pointer, or return a pointer?

If one wants to get a start address of a array or a string or a block of memory via a function, there are at least two methods to achieve it: (1) one is to pass a pointer-to-pointer parameter, like: int my_malloc(int size, char **pmem) { *pmem=(char *)malloc(size); if(*pmem==NULL)... (11 Replies)
Discussion started by: aaronwong
11 Replies

8. Programming

far pointer

what is far pointer in C (1 Reply)
Discussion started by: useless79
1 Replies

9. Programming

why we never delete a pointer twice

can u tell me the reson that why we should not delete a pointer twice.? if we delete ponter twice then what happen and why this happen Regards, Amit (2 Replies)
Discussion started by: amitpansuria
2 Replies

10. Programming

pointer

void main() { int a={1,2,3,4,5,6,7,8,9,10}; int *p=a; int *q=&a; cout<<q-p+1<<endl; } The output is 10, how? if we give cout<<q it will print the address, value won't print.... if we give cout<<p it will print the address, value won't print.... p has the base addr; q... (1 Reply)
Discussion started by: sarwan
1 Replies
Login or Register to Ask a Question