944,092 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1272
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 12th, 2009
1

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

Expand Post »
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];
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ashok.g15 is offline Offline
4 posts
since Dec 2007
Oct 12th, 2009
0
Re: why use of size 0 or 1 array in structure for URI'S etc
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.
Reputation Points: 100
Solved Threads: 48
Posting Pro in Training
Grn Xtrm is offline Offline
495 posts
since Nov 2008
Oct 12th, 2009
2
Re: why use of size 0 or 1 array in structure for URI'S etc
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.
Reputation Points: 1446
Solved Threads: 135
Practically a Master Poster
Tom Gunn is offline Offline
681 posts
since Jun 2009
Oct 13th, 2009
0
Re: why use of size 0 or 1 array in structure for URI'S etc
Click to Expand / Collapse  Quote originally posted by Grn Xtrm ...
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ashok.g15 is offline Offline
4 posts
since Dec 2007
Oct 13th, 2009
1
Re: why use of size 0 or 1 array in structure for URI'S etc
Quote ...
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
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,864 posts
since Dec 2008
Oct 13th, 2009
1
Re: why use of size 0 or 1 array in structure for URI'S etc
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.
SVR
Reputation Points: 10
Solved Threads: 4
Light Poster
SVR is offline Offline
44 posts
since May 2008
Oct 13th, 2009
0
Re: why use of size 0 or 1 array in structure for URI'S etc
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.
Reputation Points: 100
Solved Threads: 48
Posting Pro in Training
Grn Xtrm is offline Offline
495 posts
since Nov 2008
Oct 14th, 2009
0
Re: why use of size 0 or 1 array in structure for URI'S etc
Why can't we just use a pointer. It will do the same
eg : BYTE *byte;
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ashok.g15 is offline Offline
4 posts
since Dec 2007
Oct 14th, 2009
0
Re: why use of size 0 or 1 array in structure for URI'S etc
Quote ...
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.
Reputation Points: 1446
Solved Threads: 135
Practically a Master Poster
Tom Gunn is offline Offline
681 posts
since Jun 2009
Oct 14th, 2009
0
Re: why use of size 0 or 1 array in structure for URI'S etc
Click to Expand / Collapse  Quote originally posted by ashok.g15 ...
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 ?
SVR
Reputation Points: 10
Solved Threads: 4
Light Poster
SVR is offline Offline
44 posts
since May 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: 2d-board algorithm: Total number of different moves to reach a location
Next Thread in C Forum Timeline: Adding 2 arrays together





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC