c - Realloc, char**, segfault -


is a function, it is add_lexem and adds an element (parentheses) at the end of the specified array. If no one left the memory, it allocates some extra memory (100 * size * (* four *). This function is caused by segfault, which is the problem.

  #include & lt ; String.h & gt; #include & lt; stdlib.h & gt; #include & lt; stdio.h & gt; zero add_lexem (core ** lexam, int * lexmsac, int * lexem_en, const char * Lexem) {if (* lexem_n & gt; = * lexemsc) {lexems = realloc (Lexmas, Psychof (four *) * (* Lexmsi + 100)) * Lexmsk + = 100;} char * for_adding = malloc (size Character) * Strangel (lexem); Strcpy (for_addin G, lexem; lexem [lexem_en] = for_ adding; (* lexem_n) ++;} int main (zero) {char ** d = malloc (sizeof (char *) * 2); int lexmsis = 2; int lexem_n = 0; ad_cllex (D, and lexmsac, and lexem_en, "MEEE"); Printf ("% s \ n", D [0]); Ad_cllex (D, and Lexmsac, and Lexem_N, "Bark"); Printf "% S \ n", D [1]); // in this place lexem_n is equal to lexemsc add_lexem (D, and lexamsak, and lexem_en, "corkk"); printf ("% s \ n", d [2] ]); Return 0; }   

should be output

  meve burk quaker   

but this is

 < code> Mayo Burke segmentation fault (core dump)    

Three-star-programming: one Insert whatever you have inside the straight:

struct wordbag {size_t size; Size_ty used; Four ** bag; }; Zero add_lexem (structure wordbug * wb, const char * lexem) {if (wb-> used> gt; = wb- & gt; size) {wb- & gt; bag = realloc (wb-> bag, (wb-> size + 100) * size size * wb-> bag); Wb- & gt; Size + = 100; } Wb-> Bag [wb-> gt; ++] = stdup (lexeme); }

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -