0

OK, so the problem is basically like the title. Couldn't find a question that got all bits of it, so I figured I'd ask.

Say I want an array of structs shaped like the following

typedef struct s_woo{
    char** n;
    char* x;
} t_woo;

So I believe I should do

t_woo* woos = malloc(num_woos * sizeof(*woos));

Seems simple enough (and should deter people from yelling at me for my habit of casting malloc).

Then I want to initialize the things in each of those structs.

So intuitively I do:

for(i = 0; i < num_woos; i++){

    num_ns = randomint1 / randomint2; //let's say num_ns is big, like 250-ish, average, and changes every round of the loop

    woos[i].n = malloc(num_ns * sizeof(char*));
    woos[i].x = malloc(num_ns * sizeof(char));
    for(j = 0; j < num_ns; j++){
        woos[i].n[j] = malloc(16 * sizeof(char)); // I just want 16 characters per char*
    }
}

This is the boiled down version of what I have in my code. I want to know what can possibly go wrong with what I've written - like any possible thing. I'm not looking for anything in particular, just general problems with the above, like memory/heap issues, pointer mistakes etc.

Leave out "Virtual Memory Exhausted". My code error checks for that using a wrapper function on malloc, so I'm very sure that's not it.

8
  • 1
    Looks fine from here. It would help avoid coding errors to use a define/constant instead of 16. I don't know if you intend to store strings in these arrays but if so, don't forget about the null terminator. Commented Mar 31, 2014 at 5:14
  • If you're going for robust design you could use a bit more encapsulation, i.e. turn t_woo into a self-contained object. This would involve putting num_ns insde t_woo, and having a function which does all the construction for a single t_woo (and another one that does the destruction later). Then you loop through your list of t_woo's calling the construction function on each one, as soon as possible after you allocate the memory for them. Commented Mar 31, 2014 at 5:17
  • and changes every round of the loop.. Are you deallocating memory before allocating in each round of loop? Maybe your outside loop runs so many times, that it gives a memory error. Commented Mar 31, 2014 at 5:35
  • You didn't store num_ns anywhere! Commented Mar 31, 2014 at 6:34
  • @MattMcNabb Thanks, I may do just that with the encapsulation. This works in C? Commented Mar 31, 2014 at 6:39

1 Answer 1

1

Even better:

 static const size_t woo_n_size = 16; 
  /* To make sure you use 16 everywhere,
   * also easier to change it
   */

 struct woo_item {
     char n[woo_n_size];
     char x;
 };

 struct s_woo {
     struct woo_item *items;
     size_t size; / * optinal, to keep track of item count */ 
 }

With the woo_item struct you can make sure there is no x without n[woo_n_size] allocated, and vice versa. You can remember the count of woo_items by having a designated null element to close each of your lists, or just store a size member in s_woo

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.