LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-22-2018, 03:09 PM   #1
haidvash
LQ Newbie
 
Registered: Apr 2018
Posts: 2

Rep: Reputation: Disabled
double free or corruption (out) C error . Eclipse code .


https://www.yogile.com/2et68g4vlfw/2...?vsc=NmY3MWIyM

Hey guys im having an error while running my code in c Eclipse.

so IN THE LINK THERE IS THE CODE WITH COULORS SO IT WILL BE EASY TO READ ....

ill be happy if you can look at the photo and tell me whats the problem with my code . thanks . btw sometime i also get this error
double free or corruption (out)




AND THATS THE FULL CODE :

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void FreeBuffer();
void FreeBuffer() {
while (getchar() != '\n')
;
}
int main() {
int *size_of_def_for_each_word, Number_of_words, Number_of_def, i, j;
char ***Dictionary;
printf("Dear Sir or Miss, please Enter"
" Number of words you would like to add to your dictionary :\n");
while (1) {

if (scanf("%d", &Number_of_words) != 1) {
FreeBuffer();
printf("Incorrect Input\n");
printf("Dear Sir or Miss, please Enter Number of words "
"you would like to add to your dictionary: \n ");
} else {
break;
}
} //while(1)

printf("OK Sir So the numbers of words you chose to put in your "

"dictionary is: %d \n", Number_of_words);

//So until now we check the valid of user input and now will build the Dynamic Array.

size_of_def_for_each_word = (int*) malloc(Number_of_words * sizeof(int)); //here we Build Dynamic array that will keep the number of def in every word

// Dictionary = malloc(Number_of_words * sizeof(char**)); //here we Build the big array

for (i = 0; i < Number_of_words; i++) {
printf("How many Definition there for the %d word : \n ", i + 1);
while (1) {

if (scanf("%d", &Number_of_def) != 1) {
FreeBuffer();

printf("Incorrect Input\n");

printf(
"Dear Sir or Miss, please Enter Number of Definition there for the %d word \n",
i);
} else {

break;
}
} //while(1)
size_of_def_for_each_word[i] = Number_of_def;
printf("OK Sir So the numbers of Definition's you chose is: %d \n",
size_of_def_for_each_word[i]);

//on the next step for every word we will build Dynamic Array by number of definitions+ the word.

} //for num_of_words

/*
OK so until here size_of_def_for_each_word got basically number of definitions for each word
For example size_of_def_for_each_word[0]=3 > the first word got 3 expression .
*/
for (int i = 0; i < Number_of_words; i++) {

printf("%d ", size_of_def_for_each_word[i]);

printf("\n");
}

Dictionary = malloc(Number_of_words * sizeof(char **));
for (i = 0; i < Number_of_words; i++) {
Dictionary[i] = malloc((size_of_def_for_each_word[i]) * sizeof(char *));
for (j = 0; j < size_of_def_for_each_word[i]+1; j++) {
if (j == 0) { //here we enter the word in the array.
printf("please enter the %d word! : \n", i + 1);
FreeBuffer();
char input_Word[80];
Dictionary[i][j] = (char *) malloc((strlen(input_Word) + 1) * sizeof(char));
scanf("%s", Dictionary[i][j]);
printf(" Word number %d is : %s \n ", i + 1, Dictionary[i][j]);
}

else { //here we enter the definition of every word one by one .
printf("please enter the %d definition! : \n", j);
FreeBuffer();
char Input_Definition[200];
Dictionary[i][j] = (char *) malloc((strlen(Input_Definition) + 1) * sizeof(char));
scanf("%[^\t\n]s", Dictionary[i][j]);
printf(" Definition number %d is : %s \n ", j, Dictionary[i][j]);

}
}
}
for (i = 0; i < Number_of_words; i++) {
for (j = 0; j < size_of_def_for_each_word[i]+1; j++) {
if (j==0) {
printf("word is %s , it's definitions are ", Dictionary[i][j]);
free(Dictionary[i][j]);
} else {
printf("%s\t", Dictionary[i][j]);
free(Dictionary[i][j]);
}
}
free(Dictionary[i]);
printf("\n");
}
free(Dictionary);
printf("finished \n");
return 0;

} //Main

Last edited by haidvash; 04-22-2018 at 03:14 PM.
 
Old 04-23-2018, 12:11 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,878
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
Please read (and learn by heart) these:
https://www.linuxquestions.org/quest...gs-4175464257/
https://stackoverflow.com/help/mcve

Last edited by NevemTeve; 04-23-2018 at 12:12 AM.
 
Old 04-23-2018, 12:52 AM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,997

Rep: Reputation: 7338Reputation: 7338Reputation: 7338Reputation: 7338Reputation: 7338Reputation: 7338Reputation: 7338Reputation: 7338Reputation: 7338Reputation: 7338Reputation: 7338
yes, please use code tags and please describe your problem better. http://catb.org/~esr/faqs/smart-ques...html#beprecise
Please post all the relevant info, like: what was the exact error message, how did you compile your code...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
double free or corruption error DKSL Linux - Software 1 07-01-2012 03:48 AM
runtime error ( double free or corruption (out)) mid way through reading in file quantum_leaf Programming 1 03-02-2010 01:46 PM
What does this error mean? double free or corruption nanxy Programming 6 08-10-2008 11:26 AM
Urgent: glibc detected *** double free or corruption error when creating ms_newMapobj SteevN Linux - General 0 04-01-2008 07:52 AM
*** glibc detected *** double free or corruption - ERROR bandwidthjunkie Programming 2 03-19-2007 09:36 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:50 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration