0

I would like to ask if there is a limit for a struct in C.

I have:

#define MAX_DOC_LENGTH (1<<22)

And:

struct MyDocument {
DocID doc_id;
unsigned int num_res;
QueryID* query_ids;
unsigned int size;
char str[MAX_DOC_LENGTH];
};
glarkou
  • 7,023
  • 12
  • 68
  • 118

4 Answers4

1

It depends if you are creating an instance of your structure on heap or stack. If you define a pointer to the object and allocate on the heap through malloc, then it depends on the available memory of your system.

If you define an instance of the object on the stack as struct MyDocument mydoc;, then this is bound to fail as your system will not have such a huge stack value.

It would recommended to declare str as a pointer i.e. char *str and allocate the memory for the same through malloc.

The structure definition could be redefined as

struct MyDocument {
    DocID doc_id;
    unsigned int num_res;
    QueryID* query_ids;
    unsigned int size;
    char *str; // Modified the declaration
};

With this change, it doesn't matter if you create the object on stack or heap. If you are defining a pointer to the object, then you could allocate the object and str as shown in the example below

struct MyDocument *myDoc; // Declare an object pointer

myDoc = malloc(sizeof(MyDocument)); // Allocate memory for the object
myDoc->str = malloc(sizeof(char) * MAX_DOC_LENGTH); // Allocates memory for str

Alternatively, you could define an object on the stack and allocate space for str only as

struct MyDocument someDoc; // Declare an object

someDoc.str = malloc(sizeof(char) * MAX_DOC_LENGTH); // Allocates memory for str
Ganesh
  • 5,880
  • 2
  • 36
  • 54
  • Can you give me a malloc example as well? – glarkou Mar 22 '13 at 01:54
  • @Ganesh: Are you aware of a preprocessor macro that stores the maximum size of any object? That is, the maximum value that you may pass to malloc, and the maximum size of any object that may be declared... – autistic Mar 22 '13 at 02:02
  • Additionally, I don't think "heap" and "stack" are at all relevant to memory allocation. – autistic Mar 22 '13 at 02:04
  • @modifiablelvalue Sorry I am unable to recollect. Could you please update? Also, why heap and stack are irrelevant? I am not sure if we can allocate an object that would require say 1 MB on stack. Being from an embedded systems' background, I am not aware if we can really allocate huge objects on stack – Ganesh Mar 22 '13 at 02:07
  • 1
    It's quite simple to configure a stack of several gigabytes in Unix. – teppic Mar 22 '13 at 02:07
  • @Ganesh Consider an implementation that, upon entrance to a function or block of code, mallocs its non-static locals and upon exit of that block of code, frees them. Are those objects still *on the stack*? If so, does a large object occupy any more space *on the stack* than a small one? – autistic Mar 22 '13 at 02:18
  • @modifiablelvalue If you are mallocing the object, it is allocated on the heap. With the original question, `struct MyDocument strDoc;` would create object on the stack. If the object is defined to be in a specific scope, the instantaneous requirement for stack will be higher, even though it is released at the end of the scope. Even though the memory is freed, can a system support such a large request for stack? – Ganesh Mar 22 '13 at 02:27
  • @teppic thank you.. could you please share a reference? – Ganesh Mar 22 '13 at 02:27
  • 1
    `ulimit -s 131072` would set the stack to 1gb. – teppic Mar 22 '13 at 02:31
  • @Ganesh: I think you've missed my point. Suppose `struct MyDocument strDoc;` creates the object on the heap, instead, and the pointer to that object is stored on the stack. Now, with this in mind, go back and read my last comment and try to respond the same way. – autistic Mar 22 '13 at 02:31
  • @Ganesh: A helpful reference is the C standard, which mentions nothing about a stack or heap... – autistic Mar 22 '13 at 02:33
  • @modifiablelvalue To quote you _creates the object on the heap, instead, and the pointer to that object is stored on the stack_, yes this is exactly what I have suggested in my response also i.e. define the pointer on stack and allocate on heap – Ganesh Mar 22 '13 at 02:46
  • @modifiablelvalue Thanks for your comment. I acknowledge that C Standard doesn't talk about stack or heap, but the implementation is typically environment dependent. Hence, I don't think we can neglect stack and heap. Maybe the perspective is different between PC and embedded worlds. I believe both views are correct and there may not be single solution for both these diverse universes. – Ganesh Mar 22 '13 at 03:05
1

Here your problem is associated with the size of the string str not with the number of variables declared inside the structure. There won't be any restrictions by the compiler, if any problem occurs it will be due to the memory capacity.

Deepu
  • 7,592
  • 4
  • 25
  • 47
  • 1
    There are restrictions by the compiler: A compiler will only support objects up to `SIZE_MAX` bytes in size. – autistic Mar 22 '13 at 01:58
1

4MB is a probably gonna be too big for the stack. Allocate your str on the heap.

struct MyDocument {
DocID doc_id;
unsigned int num_res;
QueryID* query_ids;
unsigned int size;
char* str;
};

And when you allocate:

struct MyDocument doc;
doc.str = malloc(MAX_DOC_LENGTH);
alf
  • 681
  • 1
  • 8
  • 19
1

SIZE_MAX, which is the maximum value of the size_t type, is the absolute maximum size (in C's bytes, which have CHAR_BIT bits in them, which is >= 8) of any single object in C.

Alexey Frunze
  • 61,140
  • 12
  • 83
  • 180