Earlier quoted context omitted.
char* points to some mutable buffer of char's that is supposed to be allocated somewhere. In this particular case they are assigned string literals, but in general it would be quite easy for someone to break consistency of allocations/deallocations in such code.
Given all unioned members are char*, where would the issue come from?
Resume in C
61–70 of 115 posts
Re: Resume in C
#62When I saw the title, I thought it was going to be about pausing and resuming a thread of execution or some stack. Could an editor put the slanty thing over the e in the title?
Re: Resume in C
#63Earlier quoted context omitted.
Surely it should be `thing_t* thing`!
thing_t* thing_ptr, thing imo, the star should be with the variable name. because, in this case, a reader could think that both thing_ptr and thing are pointer to a thing_t "thing_t thing_ptr, thing, other_thing_ptr" is imo more readable
thing_t* ptr1;
thing_t* ptr2;
not thing_t* ptr1, ptr2 /* Re: Resume in C
#64Wow, I didn't know this was valid C code, it's pretty convenient. school_t uiuc = { .school = "University of Illinois at Urbana-Champaign", .location = "Urbana, IL", .program = "BS Computer Science", .started = 1251158400, .left = 1336608000, .accomplishments = { "Minor in International Studies in Engineering, Japan", "Focused on systems software courses", NULL } };
Re: Resume in C
#65Earlier quoted context omitted.
Given all unioned members are char*, where would the issue come from?
Say, somebody allocated one of the strings with malloc or strdup and put a free(), but mixed up when to call free() and when not.
Re: Resume in C
#66Earlier quoted context omitted.
There are three data structures unsystematically blended into one (that is, one cannot unambiguously tell which fields go into which "virtual" type, and cannot formally check the correctness). The name thing_t is indicative that you indeed cannot really tell what that entity is. This kind of data structure design begs for errors, while being conceptually wrong.
> There are three data structures unsystematically blended into one The point is that they're not three completely separate data structures, they're a single data structure with context-dependent field labelling.
typedef struct {
union {
company_t * company;
school_t * school;
project_t * project;
};
union {
address_t * location;
url_t * url;
};
etc.Re: Resume in C
#67--- a/resume.c +++ b/resume.c @@ -118,7 +118,7 @@ void print_job(job_t * job) { } } -int main(int argc, char * argv) { +int main(int argc, char ** argv) { int i = 0; while (jobs[i]) { @@ -127,4 +127,4 @@ int main(int argc, char * argv) { } return 0; -} \ No newline at end of file +}
Re: Resume in C
#68Re: Resume in C
#69Re: Resume in C
#70--- a/resume.c +++ b/resume.c @@ -118,7 +118,7 @@ void print_job(job_t * job) { } } -int main(int argc, char * argv) { +int main(int argc, char ** argv) { int i = 0; while (jobs[i]) { @@ -127,4 +127,4 @@ int main(int argc, char * argv) { } return 0; -} \ No newline at end of file +}
For those who don't write C, could you explain what is significant about this change? I'm guessing the earlier version was some kind of subtle bug?
char * argv
is a pointer to a string (or a single character). char ** argv
is a pointer to an array of pointers to strings. The latter is used since it allows for multiple arguments in (the number being `int argc`).This picture shows the second example in terms of pointers: