why use of size 0 or 1 array in structure for URI'S etc

Reply

Join Date: Dec 2007
Posts: 4
Reputation: ashok.g15 is an unknown quantity at this point 
Solved Threads: 0
ashok.g15 ashok.g15 is offline Offline
Newbie Poster

why use of size 0 or 1 array in structure for URI'S etc

 
1
  #1
Oct 12th, 2009
Why do some code for eg in gstreamer etc use code which has code like

struct xxx
{
int foo;
...
..
char arrURI[1];
char data[0];
}
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 457
Reputation: Grn Xtrm is on a distinguished road 
Solved Threads: 37
Grn Xtrm's Avatar
Grn Xtrm Grn Xtrm is offline Offline
Posting Pro in Training
 
0
  #2
Oct 12th, 2009
Array indexes start at 0.
char arrURI[1]; -- creates an array of size one
-- so there are two indexes 0 and 1
char data[0]; -- creates an initially empty array
Hope that is what you were looking for.
Last edited by Grn Xtrm; Oct 12th, 2009 at 10:56 am.
Check out my new band URL on facebook. I'm the bass player. :) Become a fan and leave comments if you like.
URL on facebook!
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 681
Reputation: Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of 
Solved Threads: 132
Tom Gunn's Avatar
Tom Gunn Tom Gunn is offline Offline
Practically a Master Poster
 
2
  #3
Oct 12th, 2009
If the array is the last one in the struct, you are probably seeing the struct hack. Prior to C99 it is an unportable trick for allocating arrays in a struct:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef struct
  6. {
  7. char s[1];
  8. } String;
  9.  
  10. int main()
  11. {
  12. /* allocate 15 extra bytes for the string */
  13. String *s = malloc(sizeof *s + 15);
  14.  
  15. strcpy(s->s, "test string");
  16. puts(s->s);
  17. free(s);
  18.  
  19. return 0;
  20. }
The idea is that by tacking on extra memory and accessing it by overflowing the last array member, you can save an extra call to malloc:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef struct
  6. {
  7. char *s;
  8. } String;
  9.  
  10. int main()
  11. {
  12. String *s = malloc(sizeof *s);
  13.  
  14. s->s = malloc(15);
  15. strcpy(s->s, "test string");
  16. puts(s->s);
  17. free(s->s);
  18. free(s);
  19.  
  20. return 0;
  21. }
C99 makes the struct hack a legal feature of the language, but I do not think it is a good idea even then because it makes the code less clear.
-Tommy (For Great Justice!) Gunn
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 4
Reputation: ashok.g15 is an unknown quantity at this point 
Solved Threads: 0
ashok.g15 ashok.g15 is offline Offline
Newbie Poster
 
0
  #4
Oct 13th, 2009
Originally Posted by Grn Xtrm View Post
Array indexes start at 0.
char arrURI[1]; -- creates an array of size one
-- so there are two indexes 0 and 1
char data[0]; -- creates an initially empty array
Hope that is what you were looking for.

Thank you very much for responding .

As I understand.
from the example I have given
char arrURI[1] ;
is same to declaring
char arrURI;
BOTH allocates only one byte.

But in real usage arrURI is assigned some string which is more than one byte when it is declared as char arrURI[1].

In char data[0];
data is a constant pointer which cannot be assigned another address and data has no size . So how is it equivalent as creating an empty array.

Hope my question is clear.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,176
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 147
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Veteran Poster
 
1
  #5
Oct 13th, 2009
char arrURI[1]; -- creates an array of size one
-- so there are two indexes 0 and 1
Actually there is only 1 index, which is 0 in char A[1];
A[0] = 'a'; //ok
A[1] = 'b'; //bug
I give up! 
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e ]
2) What does this sequence  equal to :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
3) What is the 123456789 prime numer?
Ask4Answer
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 33
Reputation: SVR is an unknown quantity at this point 
Solved Threads: 4
SVR SVR is offline Offline
Light Poster
 
1
  #6
Oct 13th, 2009
In the case of zero sized array, it was generally used as an easy way to index into a variable sized buffer ...

  1. struct Header {
  2. DWORD dwMagic;
  3. DWORD dwSize;
  4. BYTE data[];
  5. };
  6.  
  7. void MyFunction()
  8. {
  9. Header *pHdr = (Header *)buffer;
  10.  
  11. Read(buffer, MAX_SIZE);
  12.  
  13. switch(pHdr->dwMagic)
  14. {
  15. case M_TAG('R','I','F','F') :
  16. HandleRIFF(pHdr->data,pHdr->dwSize);
  17. break;
  18. // ... etc
  19. }
  20.  
  21. }

It lets the compiler calculate the offset so you don't have to.
Last edited by SVR; Oct 13th, 2009 at 12:49 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 457
Reputation: Grn Xtrm is on a distinguished road 
Solved Threads: 37
Grn Xtrm's Avatar
Grn Xtrm Grn Xtrm is offline Offline
Posting Pro in Training
 
0
  #7
Oct 13th, 2009
Originally Posted by firstPerson View Post
Actually there is only 1 index, which is 0 in char A[1];
A[0] = 'a'; //ok
A[1] = 'b'; //bug
Wow, yes you are right. I mistyped there. Thanks for the correction.
With only one element there is only one index, which is 0.
Check out my new band URL on facebook. I'm the bass player. :) Become a fan and leave comments if you like.
URL on facebook!
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 4
Reputation: ashok.g15 is an unknown quantity at this point 
Solved Threads: 0
ashok.g15 ashok.g15 is offline Offline
Newbie Poster
 
0
  #8
Oct 14th, 2009
Why can't we just use a pointer. It will do the same
eg : BYTE *byte;
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 681
Reputation: Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of 
Solved Threads: 132
Tom Gunn's Avatar
Tom Gunn Tom Gunn is offline Offline
Practically a Master Poster
 
0
  #9
Oct 14th, 2009
Why can't we just use a pointer.
In C99 because a pointer might not work while an empty array will. Prior to C99, I cannot think of any reason why an array is better than a pointer except for keeping the array size out of the struct size when using sizeof . I think the original struct hack uses an array size of 0, and it evolved to use 1 for compilers that do not allow an array size of 0.
-Tommy (For Great Justice!) Gunn
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 33
Reputation: SVR is an unknown quantity at this point 
Solved Threads: 4
SVR SVR is offline Offline
Light Poster
 
0
  #10
Oct 14th, 2009
Originally Posted by ashok.g15 View Post
Why can't we just use a pointer. It will do the same
eg : BYTE *byte;
Let's try it with my previous example.

  1. struct Header {
  2. DWORD dwMagic;
  3. DWORD dwSize;
  4. BYTE *data;
  5. };
  6.  
  7. void MyFunction()
  8. {
  9. Header *pHdr = (Header *)buffer;
  10.  
  11. Read(buffer, MAX_SIZE);
  12.  
  13. switch(pHdr->dwMagic)
  14. {
  15. case M_TAG('R','I','F','F') :
  16. HandleRIFF(pHdr->data,pHdr->dwSize);
  17. break;
  18. // ... etc
  19. }
  20. }

Now this will crash. Why ?
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC