C - malloc in a loop with struct with pointer -


I am starting and I am doing some practice to learn something.

I have work on dynamic memory allocation with strings and pointers I have this structure:

struct fact_entry {/ * Definition of each table entry / int n; Long time lli_fact; / * 64-bit integer * / four * string_fact; };

And this is my main code:

  int main (int argc, char * argv []) {int n; Int i; Struct fact_entry * fact_table; If (argc! = 2) panic ("wrong parameter"); N = Atoi (RGR [1]); If (n & lt; 0) panic ("too small"); If (n & gt; LIMIT) panic ("n too large"); / * Your code starts here * / int p; Fact_table = (struct fact_entry *) malloc (n * sizeof (struct fact_entry)); If (fact_table == NULL) {fprintf (stderr, "Out of memory, escaping \ n"); Exit (1); } For (i = 0; i & lt; = n; i ++) {fact_table [i] .n = i; P = i; Fact_table [i] .lli_fact = 1; While (P & gt;) {Fact_table [i] .lli_fact = p * fact_table [i] .lli_fact; P--; } P = (int) log10 (fact_table [i] .lli_fact) +1; Fact_table [i] .str_fact = malloc (p); If (fact_table-> gt; str_fact == faucet) {fprintf (stderr, "Out of Memory, Exiting \ n"); Exit (1); } Sprintf (fact_table [i] .str_fact, "% lld", fact_table [i] .lli_fact); } / * Your code ends with * / (i = 0; i & lt; = n; i ++) {printf ("% d% lld% s \ n", fact_table [i] .n, Fact_table [i] .lli_fact, fact_table [i] .str_fact); } Return 0; }  

The idea is to fill an array of 20 rows, then there are 3 columns in each row, in the first column it represents the number of "i" in the second row, which is the number of lines Factorial is, in the same in the third, but in the string format.

I use the log-in to know how long the string will be.

The execution of the program shows:

  0 1 | G! 2 1 1 2 2 2 3 6 6 24 24 5 120 120 6 720 720 7 5040 5040 8 40320 40320 9 362880 362880 10 3628800 3628800 11 39916800 39916800 12 479001600 479001600 13 6227020800 6227020800 14 87178291200 87178291200 15 1307674368000 1307674368000 16 20922789888000 20922789888000 20922789888000 17 355687428096000 355687428096000 18 6402373705728000 6402373705728000 1 9 121645100408832000 121645100408832000 20 2432902008176640000 should show 2432902008176640000  

line 1 the third column, what happens? It looks something wrong with MoloL.

Thank you!

  fact_table = malloc (sizeof (struct fact_entry));  

Why? In the first case, I am only reserving memory for one line, is it true? Why is no division fault generating error?

Correct but bad code does not mean an accident You will write on the memory that you were not expected to allocate. But this is your memory! Depending on the implementation, a page size block of malloc may be allocated. This is likely to mean 4096 bytes. If you can afford it may crash when you overwrite it or if you have an allocation which comes from the pages you see problems. Second, why can I fill in the third column? I memory can not assign .. I do not know that I can do to refer to each row, because I have a strait with the pointer in a loop ... and I do not know how to define iterations malloc .

Not sure I follow them. You can allocate all the memory to one single maulock at one time. Then manually loop to properly address str_fact in that block of memory, but it is unnecessarily complicated, however, means that you have only one award was given by Aiomo answer right way.


Comments