Can anyone please explain why the 1st method of accessing a nested struct element inside an union in a struct works and the 2nd does not?
typedef struct element Node;
struct element
{
int type;
union
{
int value;
Node *child[2];
} u;
};
int main()
{
Node n;
Node *p;
n.type = 0;
p = n.u.child[0];
p->type = 10; // 1st method
(n.u.child[1])->type = 24; // 2nd method
return 0;
}
Try the following:
int main()
{
Node n;
Node *p;
n.type = 0;
// allocate memory for child nodes
n.u.child[0] = (Node *)malloc(sizeof(Node));
if (n.u.child[0] == NULL)
{
return 1;
}
n.u.child[1] = (Node *)malloc(sizeof(Node));
if (n.u.child[1] == NULL)
{
free(n.u.child[0]);
return 1;
}
p = n.u.child[0];
p->type = 10; // 1st method
(n.u.child[1])->type = 24; // 2nd method
// release dynamically allocated memory
free(n.u.child[0]);
free(n.u.child[1]);
return 0;
}
NOTE: Don't modify n.u.value of a Node if you've already assigned its child[] pointers. You will overwrite one of the pointers and leak that memory as well as crash if you try to access the child[] array after that. Unions are tricky -- best to avoid this sort of arrangement.
Either of those methods should be fine for accessing a nested struct element inside a union, the issue here is that you haven't allocated memory for the nodes referred to by child[0] or child[1]. (I'm surprised your "1st method" doesn't fail, too.)
Your problem has not much to do with the fact that there are unions involved.
Accessing uninitialized pointers just gives you random behavior. Sometimes it does work sometimes not. For your first access probably something just luckily happened to be in the place that you access.
Just initialize, à la C99:
Node n = { .type = 0 };
or
Node n = { 0 };
à la C89,
instead of your assignment statement. This has the advantage to initialize all components that are not mentioned to 0, thus your pointers. Then your test code should segfault happily ever after.
Related
typedef struct cache_line {
char valid;
mem_addr_t tag;
struct cache_line* next;
} cache_line_t;
typedef cache_line_t* cache_set_t;
typedef cache_set_t* cache_t;
/* The cache we are simulating */
cache_t cache;
void initCache()
{
cache = malloc (S * sizeof(cache_set_t));
for (int i = 0; i < S; i ++ ){
cache[i]= malloc(sizeof(cache_line_t));
cache_line_t *temp = *(cache+i);
temp -> valid = 0;
temp -> tag = 0;
cache_line_t* curr = *(cache+ i );
for (int j = 1; j < E; j++){
curr.next = malloc(sizeof(cache_line_t));
curr = curr.next;
curr.valid=0;
curr.tag=0;
}
curr.next = NULL;
}
}
So my head is swimming trying to remember all the details of pointers and structs and I've been up for a while trying to finish this project. What I'm trying to do here is allocate an array of this structure (a linked list) on the heap and I keep having issues with type mismatches and whatnot (sorry if its messy I keep on trying new things and recompiling). Any help on where I'm going wrong would be appreciated.
Well, the fact the code is terribly obfuscated with abuse of typedef probably goes a long way towards both yours and the compilers problems. I wouldn't have a single typedef in this program myself. It serves no real abstraction here. Here's what I'd suggest (with some omission of error checking):
struct cache_line {
char valid;
mem_addr_t tag;
struct cache_line* next;
};
struct cache_line** cache;
void initCache()
{
cache = malloc (sizeof(*cache) * S);
for (int i = 0; i < S; i ++ ){
struct cache_line** curr_p = &cache[i];
for (int j = 1; j < E; j++){
*curr_p = malloc(sizeof(**curr_p));
(*curr_p)->valid = 0;
(*curr_p)->tag = 0;
(*curr_p)->next = NULL;
curr_p = &(*curr_p)->next;
}
}
}
Key things to note:
I removed all the typedefs. They served no real purpose here but an attempt to save typing. And they did it at the cost of code quality. I also removed it from the struct, since I believe the previous statement applies to it as well.
I allocated memory canonically. By writing malloc(sizeof(*pointer_variable)), you allocate enough memory regardless of what pointer_variable points at. It's somewhat type agnostic.
I traverse the linked list with the "link traversal" idiom. Instead of keeping track of the "node", I keep track of the pointer that is pointing at the node. In the beginning it's cache[i], and at every iteration it becomes the pointer inside the newly allocated node.
I have the following structure:
typedef struct TRIE_NODE
{
char* word;
struct TRIE_NODE* node[26];
}TRIE_NODE;
I create a node called head, TRIE_NODE *head = NULL;, and then i try to initialize this node using the following function:
void initialize_node(TRIE_NODE *current_node)
{
int MAX = 25;
current_node = malloc(sizeof(TRIE_NODE));
for(int i = 0; i < MAX; i++)
{
current_node->node[i] = NULL;
if(current_node->node[i] == NULL)
printf("\n -- \n");
}
}
However, i get a segmentation fault whenever i try to even read current_node->node[i]. Does anyone have any idea of what's going on? Considering current_node->node is a pointer, that points to another pointer of type TRIE_NODE, shouldn't i be able to access it's values through bracket notation? (I've tried dereferencing it too, it doesn't compile)
You do everything correctly, except this line
current_node = malloc(sizeof(TRIE_NODE));
which modifies the local copy of current_node. The pointer in the caller remains unchanged.
To fix this problem, pass a pointer to pointer, and assign with an indirection operator:
void initialize_node(TRIE_NODE **current_node_ptr) {
...
*current_node_ptr = malloc(sizeof(TRIE_NODE));
...
}
Here is the code for a bucket sort program.
typedef struct node_struct {
double d;
struct node_struct *next;
} node;
I'm using insertion sort to sort the values
void insert(double value, int index, node *B[]) {
node *t;
if (B[index] == NULL) {
t = (node *)malloc(sizeof(node));
t->d = value;
t->next = NULL;
B[index] = t;
} else {
node *p0, *p1;
p0 = B[index];
p1 = p0 -> next;
while(p1 != NULL) {
if (p1 -> d > value) {
break;
}
p1 = p1->next;
p0 = p0->next;
}
t = (node *)malloc(sizeof(node));
t->d = value;
t->next = p1;
p0->next = t;
}
void Bucket_Sort(double A[], int n) {
int j, index;
double B[n];
node *B1;
B1 = (node *)malloc(sizeof(node));
for (int i = 0; i < n; i++) {
B[i] = 0;
}
B1->d = A[0];
B1->next = NULL;
for (int i = 1; i <= n; i++) {
index = (int) floor(n * A[i]);
insert(A[i], index, B1); // This part of the program is where I'm going wrong
}
for (int = 0; i < n; i++) {
printf("%f \n", B[i]);
}
}
When I try calling the insert function, an error occurs saying "expecting struct node ** but the argument is of type struct node *"
But if I call insert function as follows:
insert(A[i],index,&B1);
Then no error is given while compiling but when I run the program it leads to a segmentation fault. Can someone help me with the confusion?
Your insert function is indicating b is an array of pointers to node objects.
But you're not passing in an array of pointers, you're calling it with &b1, which is a pointer to a single node (not an array). And when you uses arrays like that generally you need to pass a count of elements, whereas with pointers to linked list elements you usually use null to indicate the end of the list.
If I were you I would just pass pointers around to deal with everything, and get rid of the [] since you're really not passing things properly for an array. E.g. Rather than passing in an index, just pass a pointer to the object of interest. In more tricky cases you can use ** pointer to pointer but that requires a very solid understand of what you're doing.
Google linked-list example to get ideas how to deal with that properly with pointers. You'll get the idea.
Otherwise be consistent in the way you pass an interpret arrays and pass a count in, and use the count variable in your loops. I'd advise not to try to mix the [] form and * or ** paradigms until you get really comfortable with each separately.
typedef struct node_struct {
double d;
struct node_struct *next;
} node;
void insert(double value, int index, node *b[]) {
node *t;
if (b[index] == NULL) {
t = (node *)malloc(sizeof(node));
t->d = value;
t->next = NULL;
b[index] = t;
} else {
node *p0, *p1;
p0 = b[index];
p1 = p0 -> next;
while (p1 != NULL) {
if (p1 -> d > value) {
break;
}
p1 = p1->next;
p0 = p0->next;
}
t = (node *)calloc(sizeof(node), 1);
t->d = value;
t->next = p1;
p0->next = t;
}
void Bucket_Sort(double a[], int n) {
int j, index;
double b[n];
node *b1 = (node *)calloc(sizeof(node), 1);
a1->d = a[0];
b1->next = NULL;
for (int i = 1; i <= n; i++) {
index = (int) floor(n * a[i]);
insert(a[i], index, b1);
}
for (int = 0; i < n; i++) {
printf("%f \n", b[i]);
}
}
I formatted the program in your question, and went further below. This is more how I see code written in professional code bases and when I do peer code reviews, etc...
Some notes:
• If you use calloc() instead of malloc, and your buffer is automatically zeroed. People usually use bzero(), or memset() to zero arrays rather than a for() loop.
• You can declare an assign a variable such as B1 at the same time and save space/clutter;
• You can declare the variable type inside a for-loop and it will be scoped to just that for-loop. Makes it clear and saves vertical space.
• Don't get too idiosyncratic with your formatting. The programming community is just frustrated by it. There are coding standards at just about any respectable C coding place, and very strict so the code looks clean, readable, easy to understand and maintain, and consistent. If everyone applies their own twist a large coding base becomes an ugly maintenance nightmare.
• Don't add spaces before and after pointers -> no one does it and it's harder to read by experienced programmers. Leave spaces after commas, for the same reason you do when writing - separates items visually better - easier to debug, etc...
• Capitals are used for constants. Camel-case (first letter lower-case and subsequent words first letter capital for example thisIsMyVariable), for variables, or underscores this_is_my_variable, in C. Naming arrays with capital letters is tacky and you almost never see it in professional code.
I wrote a hashtable and it basically consists of these two structures:
typedef struct dictEntry {
void *key;
void *value;
struct dictEntry *next;
} dictEntry;
typedef struct dict {
dictEntry **table;
unsigned long size;
unsigned long items;
} dict;
dict.table is a multidimensional array, which contains all the stored key/value pair, which again are a linked list.
If half of the hashtable is full, I expand it by doubling the size and rehashing it:
dict *_dictRehash(dict *d) {
int i;
dict *_d;
dictEntry *dit;
_d = dictCreate(d->size * 2);
for (i = 0; i < d->size; i++) {
for (dit = d->table[i]; dit != NULL; dit = dit->next) {
_dictAddRaw(_d, dit);
}
}
/* FIXME memory leak because the old dict can never be freed */
free(d); // seg fault
return _d;
}
The function above uses the pointers from the old hash table and stores it in the newly created one. When freeing the old dict d a Segmentation Fault occurs.
How am I able to free the old hashtable struct without having to allocate the memory for the key/value pairs again?
Edit, for completness:
dict *dictCreate(unsigned long size) {
dict *d;
d = malloc(sizeof(dict));
d->size = size;
d->items = 0;
d->table = calloc(size, sizeof(dictEntry*));
return d;
}
void dictAdd(dict *d, void *key, void *value) {
dictEntry *entry;
entry = malloc(sizeof *entry);
entry->key = key;
entry->value = value;
entry->next = '\0';
if ((((float)d->items) / d->size) > 0.5) d = _dictRehash(d);
_dictAddRaw(d, entry);
}
void _dictAddRaw(dict *d, dictEntry *entry) {
int index = (hash(entry->key) & (d->size - 1));
if (d->table[index]) {
dictEntry *next, *prev;
for (next = d->table[index]; next != NULL; next = next->next) {
prev = next;
}
prev->next = entry;
} else {
d->table[index] = entry;
}
d->items++;
}
best way to debug this is to run your code against valgrind .
But to you give some perspective :
when you free(d) you are expecting more of a destructor call on your struct dict which would internally free the memory allocated to the pointer to pointer to dictEntry
why do you have to delete the entire has table to expand it ? you have a next pointer anyways why not just append new hash entries to it ?
Solution is not to free the d rather just expand the d by allocating more struct dictEntry and assigning them to appropriate next.
When contracting the d you will have to iterate over next to reach the end and then start freeing the memory for struct dictEntrys inside of your d.
To clarify Graham's point, you need to pay attention to how memory is being accessed in this library. The user has one pointer to their dictionary. When you rehash, you free the memory referenced by that pointer. Although you allocated a new dictionary for them, the new pointer is never returned to them, so they don't know not to use the old one. When they try to access their dictionary again, it's pointing to freed memory.
One possibility is not to throw away the old dictionary entirely, but only the dictEntry table you allocated within the dictionary. That way your users will never have to update their pointer, but you can rescale the table to accomodate more efficient access. Try something like this:
void _dictRehash(dict *d) {
printf("rehashing!\n");
int i;
dictEntry *dit;
int old_size = d->size;
dictEntry** old_table = d->table;
int size = old_size * 2;
d->table = calloc(size, sizeof(dictEntry*));
d->size = size;
d->items = 0;
for (i = 0; i < old_size; i++) {
for (dit = old_table[i]; dit != NULL; dit = dit->next) {
_dictAddRaw(d, dit);
}
}
free(old_table);
return;
}
As a side note, I'm not sure what your hash function does, but it seems to me that the line
int index = (hash(entry->key) & (d->size - 1));
is a little unorthodox. You get a hash value and do a bitwise and with the size of the table, which I guess works in the sense that it will be guaranteed to be within (I think?) [0, max_size), I think you might mean % for modulus.
You are freeing a pointer which is passed in to your function. This is only safe if you know that whoever's calling your function isn't still trying to use the old value of d. Check all the code which calls _dictRehash() and make sure nothing's hanging on to the old pointer.
What does dictCreate actually do?
I think you're getting confused between the (fixed size) dict object, and the (presumably variable sized) array of pointers to dictEntries in dict.table.
Maybe you could just realloc() the memory pointed to by dict.table, rather than creating a new 'dict' object and freeing the old one (which incidentally, isn't freeing the table of dictentries anyway!)
How is an array of structures created in C without knowing the eventual amount of structures to be stored in the array?
I would like to loop in a for loop, create a tempStruct set its variables, add this to an array and then loop again, creating a new tempStruct and adding it to the array.
Im having some issues wrapping my head around how this is done in C while trying to relate from objective C.
Dynamically allocated arrays (using malloc) can be reallocated (using realloc).
Therefore the solution will look something like this:
malloc initial array (arbitrary size)
while still space in array, add structures
when array full, realloc to bigger size
goto 2
You could create a double linked list which points to parent and child
struct list{
list* next;
list* prev;
special_data* data;
}
easy to do and flexible
You can't create an array in C without knowing number of it's members up front.
Your options for adding are:
(Faster) Create new array with +1 element, copy entire array and add new element to the end
(Better) Create your own implementation of linked list (Linked list) which will dynamically allocate memory for each new member.
You can use malloc to create your structure.
Edit: The following demonstrates one way to do what you're asking by creating a linked list:
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int data;
void* next;
} tempStruct;
#define NUM_STRUCTS 4
int main(void) {
tempStruct* cur_ptr;
tempStruct* root_ptr;
int i;
root_ptr = malloc(sizeof(tempStruct));
root_ptr -> data = 0;
root_ptr -> next = NULL;
cur_ptr = root_ptr;
for (i = 1; i < NUM_STRUCTS; i ++ ) {
tempStruct* new_ptr = malloc(sizeof(tempStruct));
new_ptr -> data = i;
new_ptr -> next = NULL;
cur_ptr -> next = new_ptr;
cur_ptr = cur_ptr -> next;
}
cur_ptr = root_ptr;
while (cur_ptr != NULL) {
printf("cur_ptr -> data = %d\n", cur_ptr -> data);
cur_ptr = cur_ptr -> next;
}
return 0;
}
If you really want to create something that acts more like an array, you'll need to allocate all your memory at the same time, using something like:
the_data = malloc(NUM_STRUCTS * sizeof(tempStruct);
Then you'll have to access the data with the dot operator (i.e. '.' (no quotes in your code).
struct foo {int bar;};
size_t i = 0, n = 8;
struct foo *arr = malloc(n * sizeof *arr);
int bar;
while ((bar = get_next_bar()) != -1) {
if (++i == n) { // no room for new element; expand array
arr = realloc(arr, n *= 2);
if (arr == NULL) abort; // see note below.
}
arr[i] = (struct foo){bar};
}
The number of assigned elements in the array is i+1. Don’t forget to free() the array when you’re done with it.
Note: In real programs you generally do not do p = realloc(p, s) directly. Instead you assign the result of realloc() to a new pointer, then do error detection & handling before clobbering your original pointer.