I take everyone's opinion seriously. I can't learn if I don't.I don't think you're going to take anyone else's opinions seriously either
I guess it does seem like that with the straw man you've built up. I'm saying that in my program there was no need to free the memory when the program is 1) a simple example, 2) prints something and ends right away, and 3) my OS frees the memory automatically. The reason I said I don't think you're taking me seriously is you keep acting like I'm pushing a practice of never freeing memory, period.since you seem to sneeze at freeing memory
Anyway, I don't like confrontation, so I'll always free memory in my examples from now on to avoid being attacked. I'll try to conform to the "best practices" you've decided on as I learn what's expected on this board. My deepest apologies for having an independent thought.
Independent thoughts are good. But your attitude seemed like it was unnecessary.
I would say it's a requirement, but I can't force anyone to do anything, so what you code for your own is for you to decide, but when posting examples on the board, it's usually better to avoid bad practices.
Now let's be friends again
Whoa it seems like today there were swords drawn. I'd really like to apologize myself for the silly mistake on that other thread with the floating point thing. I was really doing something else at that point and missed a spot.
I'll drink to that! As far as i am concerned with memory management, when i learned Java straight after C, i felt like i was flying on the clouds. No free? NO FREE? I AM FREE? But then again, after sometime i started feeling that Java was taking away from me something very precious. So i said: Give me back my MEMORY! It's mine, my own my PRRRREEECCIOUS!!!Now let's be friends again
Cheers all! Have a nice day/night whatever.
A typical example of ...cheap programming practices.Code:... goto johny_walker_red_label; johny_walker_blue_label: exit(-149$); johny_walker_red_label : exit( -22$);
The function would need to know the current number of keys stored in the array.
Code:void add_key(row *_row, char *key,int n_keys) { _row->keys = realloc( (n_keys+1)*sizeof(*_row->keys) ); if(key!=NULL) { _row->keys[n_keys] = malloc(3 * sizeof(char)); memcpy(_row->keys[n_keys], key, 3); } else { _row->keys[n_keys] = NULL; }
A typical example of ...cheap programming practices.Code:... goto johny_walker_red_label; johny_walker_blue_label: exit(-149$); johny_walker_red_label : exit( -22$);
You are missing proper #include
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
Really ? I don't know what else I need to include..
Code:#include <stdio.h> #include <string.h> typedef struct { char *name; char **keys; } row; void add_key(row *_row, char *key, int n_keys, int columns) { _row->keys = realloc( (n_keys+1)*sizeof(*_row->keys) ); if(key!=NULL) { _row->keys[n_keys] = malloc(columns * sizeof(char)); memcpy(_row->keys[n_keys], key, columns); } else { _row->keys[n_keys] = NULL; } } int main() { row temp; int i = 0; int n_keys = 0; int rows = 10; int columns = 5; temp.keys = malloc(rows * sizeof(char*)); add_key(&temp, "somekey", n_keys, columns); for ( i = 0; i < rows; ++i ) { int j; for ( j = 0; j < columns; ++j ) { putchar(temp.keys[i][j]); } putchar('\n'); } getchar(); return 0; }
You should use the <stdlib.h> include for the malloc/realloc calls.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
And to pick up on those errors, you just include some warning flags.
If you're using gcc or mingw, -Wall and -ansi is enough I believe.
Note that the errors won't say "You forgot header file x", but rather something like "Implicit declaration of function x" when you try and call a function that hasn't been explicitly declared anywhere, but is still being implicitly declared to exist when you call it.
Indeed.
Also note that it's good practice to include header files that you KNOW you need, even if the compiler doesn't complain about them - for example you may find that stdio.h or string.h included stdlib.h IN YOUR case, but using a different set of header files (belonging to a different compiler or C library) the includes done by those header files DO NOT include stdlib.h - thus making it fail on a different system.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Sorry for the type damn it!
...
Add the blue thing to the function.Code:_row->keys = realloc(_row->keys, (n_keys+1)*sizeof(*_row->keys) );
void *realloc(void *p, size_t new_size):
where p must be either a pointer, previously initialized with malloc, or calloc or NULL.
It is necessary to store the return of realloc to the previously allocated pointer as it might return a different address, not enough continual memory to satisfy the request will force a clone of the previously allocated area to a new memory section.
When including the <stdlib.h> header, it shouldn't compile without correcting it.
A typical example of ...cheap programming practices.Code:... goto johny_walker_red_label; johny_walker_blue_label: exit(-149$); johny_walker_red_label : exit( -22$);
Many thanks
One more thing..I add three elements to the array. How is it that it has 4 ? I even cannot access the fourth element..
Code:#include <stdio.h> #include <string.h> #include <stdlib.h> typedef struct { char *name; char **keys; } row; void add_key(row *_row, char *key, int n_keys, int columns) { if (n_keys == 0) _row->keys = malloc(sizeof(char*)); else _row->keys = realloc(_row->keys, (n_keys+1)*sizeof(*_row->keys) ); if(key!=NULL) { _row->keys[n_keys] = malloc(columns * sizeof(char)); memcpy(_row->keys[n_keys], key, columns); } else { _row->keys[n_keys] = NULL; } } int main() { row temp; int i = 0; int n_keys = 0; int rows = 10; int columns = 5; add_key(&temp, "key0", 0, columns); add_key(&temp, "key1", 1, columns); add_key(&temp, "key2", 2, columns); double si = sizeof(temp.keys); printf("%s\n", temp.keys[0]); printf("%s\n", temp.keys[1]); printf("%s\n", temp.keys[2]); //printf("%s\n", temp.keys[3]); doesnt excist ? printf("Size: %lf", si); // PRINTS 4 not 3 as expected getchar(); return 0; }