struct

When writing struct to a file too many bytes are written

I'm trying to write a simple TGA image file saver as a learning exercise in C++. I'm basing my code on an example TGA loader that declares a struct for the header and then uses fread() to load the entire header in one go. My program isn't working right now, and it seems like there are two extra bytes being written to the file. I printed...

Manually zeroing variables VS copying struct

I have a long C (not C++) struct. It is used to control entities in a game, with position, some behavior data, nothing flashy, except for two strings. The struct is global. Right now, whenever an object is initialized, I put all the values to defaults, one by one myobjects[index].angle = 0; myobjects[index].speed = 0; like that. It d...

A way to find the size and location of padding in a struct?

I'm trying to write a tool that will take as input some C code containing structs. It will compile the code, then find and output the size and offset of any padding the compiler decides to add to structs within it. This is pretty straightforward to do by hand for a known struct using offsetof, sizeof, and some addition, but I can't figur...

How to define a struct whose members can be accessed by any internal classes but can only be set by a particular (internal) class?

How to define a struct whose members can be accessed by any internal classes via properties but can only be set by a particular internal class? (If it is possible.) If it is not possible, is there a work-about to achieve the similar requirement? Edit 1: below Reason: I have a class A with two members _x and _arrB. _x can be read but ...

copy two structs in C that contain char pointers

what is the standard way to copy two structs that contain char arrays? Here is some code #include stdio.h> #include string.h> #include stdlib.h> typedef struct { char* name; char* surname; } person; int main(void){ person p1; person p2; p1.name = (char*)malloc(5); p1.surname = (char*)malloc(5); str...

Adding members to a C-style struct and backwards compatibility

Say I have a struct defined somewhere deep in low-level code used all over the place in the most crazy and unknown ways: struct T { unsigned short name_len; char d_name[LENGTH]; } With accompanying functions that fill d_name with whatever needs to be put there, like struct T* fill( somethingOrOther* X) And I would like to e...

Linux C: Easy & 'pretty' dump/printout of structs (like in gdb) - from source code?

I am having a slight problem with some structs in a kernel module I'm building, so I thought it would be nice if there was an easy way to print out structs and their values - and below is a small userland example of what I mean. Say we have the simple C example as below (given in form of a bash commands): FN=mtest cat > $FN.c <<EOF #i...

Objective-C Custom Class with Struct tutorial.

Hello. I'm coming from AS3 to Obj-C, and classes are confusing me. I want to create a ball class as a test, with colour, radius etc. Looking through other people's code I've discovered they use structs to implement them, and this seems like a much nicer method. I've searched but am unable to find a really clear explanation of what st...

Writing structure into a file in C

I am reading and writting a structure into a text file which is not readable. I have to write readable data into the file from the structure object. Here is little more detail of my code: I am having the code which reads and writes a list of itemname and code into a file (file.txt). The code uses linked list concept to read and write d...

Mimic classes in ANSI C

Hi To make a struct's members "private" to the outside I know that I can do this. In the .h file typedef struct Obj Obj; In the .c file you then struct Obj { int a; int b; } This will keep the knowledge of the existense of a and b from beeing known. But all the "member" functions in the .c file will now about them and can oppera...

How to use enum within a struct in ANSI C?

Following code has to be used in the main-function, but I don't know how it is used. struct SomeItem { enum {MOVIE, MUSIC} itemType; union { struct Movie* movie; struct Music* music; }; }; this struct is used in a dynamic linked list with previous/item/next pointer, but I don't know how you can set the enum...

Freeing variable-sized struct in C

I am using a variable-sized C struct, as follows: typedef struct { int num_elems; int elem1; } mystruct; // say I have 5 elements I would like to hold. mystruct * ms = malloc(sizeof(mystruct) + (5-1) * sizeof(int)); ms->num_elems = 5; // ... assign 5 elems and use struct free(ms); Will this last free() free every...

How to correctly fix "zero-sized array in struct/union" warning (C4200) without breaking the code?

I'm integrating some code into my library. It is a complex data structure well optimized for speed, so i'm trying not to modify it too much. The integration process goes well and actually is almost finished (it compiles). One thing is still bothering me. I'm getting the C4200 warning multiple times: warning C4200: nonstandard extension ...

Advantages of using union when same thing can be done using struct - C

I have difficulty in understanding the use of union in C. I have read lot of posts here on SO about the subject. But none of them explains about why union is preferred when same thing can be achieved using a struct. Quoting from K&R As an example such as might be found in a compiler symbol table manager, suppose that a constant...

Using Python class as a data container

Sometimes it makes sense to cluster related data together. I tend to do so with a dict, e.g., self.group = dict(a=1, b=2, c=3) print self.group[a] One of my colleagues prefers to create a class class groupClass(object): def __init__(a, b, c): self.a = a self.b = b self.c = c self.group = groupClass(1, 2, ...

Why I initialise a struct in this way make my phone crashed?

I defined a struct: typedef struct myStruct{ int32 iMem1; int16 sMem2; int32 iMem3; }myStruct; And initialise it: void main(){ myStruct s1 = {0, 1, 0}; return 0; } When I run it in my phone, it crashed my phone. If I initialise it another way: void main(){ myStruct s1 = {0}; return 0; } Everything is ok! ...

Handling a variable number of arguments at runtime in a struct

Hello I have to classes, an Executer with these methods: Executer() struct Execute(string s) Lookup(string name, int module, int num, ...) and a Parser: Parser() struct Parse(string s) The Exectuers Execute method calls the Parsers Parse method. The Parser then chucks the string into smaller bits (it explodes the string on the ...

Why using a typedef *after* struct definition?

Both this style: struct _something { ... }; typedef struct _something someting; and that style: typedef struct _something { ... } something; are correct typedef declarations in C. Note that the presence of the structure declaration in the header file is made on purpose: I need to have access to the inner components of the stru...

Will structs and value types (like C#'s) be included in Java 7?

Will structs and value types (like C#'s) be included in Java 7? ...

Storing struct instances in a std::map

I'm trying to map some structs to some other instances, like this: template <typename T> class Component { public: typedef std::map<EntityID, T> instances_map; instances_map instances; Component() {}; T add(EntityID id) { T* t = new T(); instances[id] = *t; return *t; }; }; Then I use ...