435,148 Members | 765 Online
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,148 IT Pros & Developers. It's quick & easy.

weird code.

 P: n/a 1: float (*data)[16384]; 2: data = malloc(31 * sizeof(data[0])); 3: data[i][j] = VARIABLE; Question 1: The variable data is a float pointer? Question 2: When the [16384] is used. Does that mean it is an array of float pointers? What does it mean? Question 3: Or does it mean that there are 16384 floats allocated with the pointer data that points to the head of those 16384 floats? Question 4: It will malloc 31*16384 floats... I assume this is true based on how the program uses it. But shouldn't it be mallocing 31*sizeof(float*)? Question 5: Then the code starts using data as a multidimensional array. When did it become a multidimensional array?! and how did it become multidimensional?! Aug 23 '06 #1
12 Replies

 P: n/a syn1kk wrote: 1: float (*data)[16384]; 2: data = malloc(31 * sizeof(data[0])); 3: data[i][j] = VARIABLE; Question 1: The variable data is a float pointer? Question 2: When the [16384] is used. Does that mean it is an array of float pointers? What does it mean? Question 3: Or does it mean that there are 16384 floats allocated with the pointer data that points to the head of those 16384 floats? data is a pointer to an array of 16384 floats. Question 4: It will malloc 31*16384 floats... I assume this is true based on how the program uses it. But shouldn't it be mallocing 31*sizeof(float*)? Given it's a pointer to an array, you don't want 31 float*. 31 arrays of 16384 floats, sure. Question 5: Then the code starts using data as a multidimensional array. When did it become a multidimensional array?! and how did it become multidimensional?! 'data' is a pointer to an array. You've allocated room for 31 such arrays, so data[0] is the 1. array of 16384 floats, data[1] the next array, and so on. Aug 23 '06 #2

 P: n/a syn1kk wrote: 1: float (*data)[16384]; 2: data = malloc(31 * sizeof(data[0])); 3: data[i][j] = VARIABLE; Question 1: The variable data is a float pointer? "data" is a pointer to an array of 16384 elements of float. The parens are unnecessary. Question 2: When the [16384] is used. Does that mean it is an array of float pointers? What does it mean? "data" is a pointer to an array of 16384 elements of float. Question 3: Or does it mean that there are 16384 floats allocated with the pointer data that points to the head of those 16384 floats? "data" is a pointer to an array of 16384 elements of float. Question 4: It will malloc 31*16384 floats... I assume this is true based on how the program uses it. But shouldn't it be mallocing 31*sizeof(float*)? There are a lot of things that "should be" in this code, beginning with a few hundred words of comments where the programmer can explain himself. He does have some explaining to do. > Question 5: Then the code starts using data as a multidimensional array. When did it become a multidimensional array?! and how did it become multidimensional?! There's nothing magical about the array notation. It's just another way of expressing an offset from your pointer. Aug 23 '06 #3

 P: n/a jmcgill wrote: syn1kk wrote: 1: float (*data)[16384]; 2: data = malloc(31 * sizeof(data[0])); 3: data[i][j] = VARIABLE; Question 1: The variable data is a float pointer? "data" is a pointer to an array of 16384 elements of float. The parens are unnecessary. The parentheses are completely necessary. With them, data is a pointer to an array of 16384 floats. Without them, data is an array of 16384 pointers. For example: #include #include int main(void) { float (*data0)[16384]; float *data1[16384]; printf("size of data0 is: %d\n", sizeof data0); printf("size of data1 is: %d\n", sizeof data1); return EXIT_SUCCESS; } [tmp]\$ ./a.out size of data0 is: 4 size of data1 is: 65536 [tmp]\$ Aug 23 '06 #5

 P: n/a actually this is all under the comp.lang.c FAQ. I did not know it existed. Check it out: http://c-faq.com/aryptr/ptrary2.html Aug 23 '06 #7

 P: n/a syn1kk wrote: Wow. I am thourghly impressed Eric (and the other replies). I have never seen this kind of declaration ever. And I am an intermediate/advanced programmer I my opinion. Your command of the C language is spot on... especially when you were quoting the precedence rule. ------ I suppose the last question is this. How does sizeof(data[0]) know that it is a `pointer to 16384 floats' ? Because you declared it that way. :) > In regards to no comments: Unfortunately, there are no comments because ... well that *seems* to be a trend in this company that I work at (they expect you to know everything and be a genius). Buck the trend. Failing to write comments is not the mark of a genius...it is the mark of someone who boosts his/her ego by seeing other people struggle through obfuscated crap which they have created, and take pleasure in thinking that their ability to produce such garbage is somehow a mark of skill. Remember that verbosity is often just as bad as excessive brevity (often worse), and merely commenting code is not sufficient. Code should be clear. Achieving clarity often requires subtle (or fundamental) rethinking of a design that generally leads to more efficient code. Comments are key, but they are not sufficient and they are not always appropriate. However, the general idea that "comments are for the weak" is a myth perpetrated by ignorant baboons. -- Bill Pursell -- Bill Pursell Aug 23 '06 #8

 P: n/a Bill Pursell wrote: >>Question 1: The variable data is a float pointer? "data" is a pointer to an array of 16384 elements of float. The parensare unnecessary. The parentheses are completely necessary. I'll take your word for that, but my cdecl parses it the same with or without the parens. Not that I'd ever write a declaration like this, unless it was absolutely necessary. Aug 23 '06 #9

 P: n/a syn1kk wrote: In regards to no comments: Unfortunately, there are no comments because ... well that *seems* to be a trend in this company that I work at (they expect you to know everything and be a genius). "Maybe your genius will be better appreciated at your next job." Aug 23 '06 #10

 P: n/a On Wed, 23 Aug 2006 17:03:05 -0700, jmcgill Bill Pursell wrote: >>>Question 1: The variable data is a float pointer?"data" is a pointer to an array of 16384 elements of float. The parensare unnecessary. The parentheses are completely necessary. I'll take your word for that, but my cdecl parses it the same with orwithout the parens. Then you need to find one that works correctly. >Not that I'd ever write a declaration like this, unless it wasabsolutely necessary. Remove del for email Aug 24 '06 #11

 P: n/a In article Question 1: The variable data is a float pointer? "data" is a pointer to an array of 16384 elements of float. The parens are unnecessary. The parentheses are completely necessary. I'll take your word for that, but my cdecl parses it the same with or without the parens. cdeclexplain float (*data)[16384]; declare data as pointer to array 16384 of float cdeclexplain float *data[16384]; declare data as array 16384 of pointer to float cdecl> What does yours give? -- dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131 home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/ Aug 24 '06 #12

 P: n/a Dik T. Winter wrote: In article Question 1: The variable data is a float pointer? "data" is a pointer to an array of 16384 elements of float. The parens are unnecessary. The parentheses are completely necessary. > I'll take your word for that, but my cdecl parses it the same with or without the parens. cdeclexplain float (*data)[16384]; declare data as pointer to array 16384 of float cdeclexplain float *data[16384]; declare data as array 16384 of pointer to float cdecl> What does yours give? It says I need to get more than 3 or 4 hours of sleep. Apologies, and especially apologies to the o.p. Aug 24 '06 #13

This discussion thread is closed

Replies have been disabled for this discussion.