I'm trying to store strings into dynamical allocated memory. I'm able to break the strings down and store them within the members of the struct and print them perfectly within the function readFile, but when it comes to printing it in main, it's only printing the last scan and everything else is null. I'm thinking that maybe I'm not allocating the array of structures correctly. Here's my program.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#ifdef _MSC_VER
#include <crtdbg.h> // needed to check for memory leaks (Windows only!)
#endif
#define MEM_ERROR printf("Not enough memory\n")
#define FLUSH while( getchar() != '\n' )
typedef struct
{
char id[5];
char *name;
int *sales;
int total;
int low;
int high;
}PERSON;
typedef struct
{
int worker;
int weeks;
PERSON *pAry;
}HEADER;
// Function Declaration
void valiFile(char nameIn[]);
FILE* openFile(char nameIn[]);
void getHeader(FILE* fpFile, HEADER *pHead);
PERSON* aloPerson(int workers);
void readFile(FILE* fpFile, HEADER *pHead);
char* aloName(HEADER *pHead, int strCount);
void repeat(char nameIn[]);
int main ( void )
{
// Local Declaration
FILE* fpFile;
char nameIn[25];
char *endPro = "end";
HEADER *pHead = (HEADER*)calloc(1, sizeof(HEADER));
// Statement
printf("Please select file to to open.\nsales or sales_2: ");
scanf("%s", nameIn);
FLUSH;
do
{
valiFile(nameIn);
fpFile = openFile(nameIn);
getHeader(fpFile, pHead);
readFile(fpFile, pHead);
//printf("%s\n", pHead->pAry[0].id);
//free(pHead);
repeat(nameIn);
}
return 0;
}// main
/* ========== valiFile ==========
========== */
void valiFile(char nameIn[])
{
// Local Declaration
char *file = "sales";
char *file2 = "sales_2";
int i;
int check = 0;
// Statement
do
{
for(i = 0; nameIn[i]; i++)
{
nameIn[i] = tolower(nameIn[i]);
}
if(strcmp(file, nameIn) != 0)
{
if(strcmp(file2, nameIn) != 0)
{
printf("\nPlease enter a valid file.\n");
printf("sales or sales_2: ");
scanf("%s", nameIn);
FLUSH;
}
else
check = 1;
}
else
check = 1;
}
while(check != 1)
;
return;
}// valiFile
/* ========== openFile ==========
========== */
FILE* openFile(char nameIn[])
{
// Local Declaration
FILE* fpFile;
char *strSale = "sales";
// Statement
if(strcmp(strSale, nameIn) == 0)
{
fpFile = fopen("sales.txt", "r");
if(fpFile == NULL)
{
printf("File didn't read correcty.\n");
exit(100);
}
}
else
{
fpFile = fopen("sales_2.txt", "r");
if(fpFile == NULL)
{
printf("File didn't read correcty.\n");
exit(100);
}
}
return fpFile;
}// openFile
/* ========================= getHeader ========================
============================================================*/
void getHeader(FILE* fpFile, HEADER *pHead)
{
// Local Declaration
int worker, salesWeek, i;
PERSON *list;
// Statement
fscanf(fpFile, "%d %d", &worker, &salesWeek);
list = aloPerson(worker);
HEADER header = {worker, salesWeek, list};
*pHead = header;
return;
}// getHeader
/* aloPerson
*/
PERSON* aloPerson(int worker)
{
// Local Declaration
PERSON *list;
// Statement
list =(PERSON*)calloc(worker, sizeof(PERSON));
if(list == NULL)
{
MEM_ERROR, exit(103);
}
return list;
}// aloPerson
/* readFile
*/
void readFile(FILE* fpFile, HEADER *pHead)
{
// Local Declaration
char temp[50];
int strCount = 0;
char *loc;
char *ptr;
int i;
// Statement
fscanf(fpFile, "%*d %*d");
for(i = 0; i < pHead->worker; i++)
{
while(fgets(temp, sizeof(temp), fpFile))
{
ptr = temp;
loc = strchr(temp, ' ');
strncpy(pHead->pAry[i].id, temp, (loc - ptr));
ptr += (loc - temp);
*ptr++;
loc = strchr(temp, ';');
strCount = (loc - ptr);
pHead->pAry[i].name = aloName(pHead, strCount);
strncpy(pHead->pAry[i].name, ptr, (loc - ptr));
ptr += (loc - ptr);
printf("%s\n", pHead->pAry[i].name);
}
}
return;
}// readFile
/* aloName
*/
char* aloName(HEADER *pHead, int strCount)
{
// Local Declaration
char *names;
// Statement;
names = malloc((strCount + 1)*sizeof(char));
return names;
}
Related
Alright sorry for creating another question but the last one got overwhelmed and chaotic.
So I'm making a hash table which inserts words from a file (tokens) and after I have inserted them I need to sort them. The program template was given, the only functions that weren't complete were : insert_ht() , clear_ht() and compare. Even though I've done tons of search about qsort with compare, the program doesn't sort the frequencies (number of times each word was inserted) . I want em sorted from the highest to lowest.
Here is the code : "note that i shouldn't change any function except insert_ht() , clear_ht() and compare
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define HTABLE_SIZ 1001
#define MAX_LINE_SIZ 1024
/* Hash Table */
typedef struct node* link;
struct node { char *token; int freq; link next; };
link htable[HTABLE_SIZ] = { NULL }; /* Table of lists (#buckets) */
int size = 0; /* Size (number of elements) of hash table */
unsigned int hash (char *tok );
void insert_ht (char *data);
void clear_ht ( );
void print_ht ( );
void Process(FILE *fp);
int main(int argc, char *argv[])
{
int i;
FILE *fp;
printf("prin tin for \n");
for (i=1; i < argc; i++)
{
printf("prin tin fopen \n");
fp = fopen(argv[i],"r");
if (NULL == fp)
{
fprintf(stderr,"Problem opening file: %s\n",argv[i]);
continue;
}
printf("prin tin process \n");
Process(fp);
fclose(fp);
}
print_ht();
//clear_ht();
return 0;
}
void Process(FILE *fp)
{
const char *seperators = " ?!'\";,.:+-*&%(){}[]<>\\\t\n";
char line[MAX_LINE_SIZ];
char *s;
while((fgets(line,MAX_LINE_SIZ, fp)) != NULL)
{
for (s=strtok(line,seperators); s; s=strtok(NULL,seperators)){
printf("prin tin insert %s \n",s);
insert_ht(s);
}
}
}
/* Hash Function */
unsigned int hash(char *tok)
{
printf("bike stin hash \n");
unsigned int hv = 0;
while (*tok)
hv = (hv << 4) | toupper(*tok++);
printf("VGAINEIIIIIIIIIIIIII %d \n",hv);
return hv % HTABLE_SIZ;
}
void insert_ht(char *token)
{
printf("bike stin insert %s \n",token);
unsigned int hashval = hash(token);
struct node *new_list;
if (htable[hashval]==NULL){
printf("mesa stin prwti if %u %s \n",hashval,token);
//token = strdup(token);
new_list = malloc(sizeof(link));
new_list->token = strdup(token) ;
new_list->freq = 1;
new_list->next = htable[hashval];
htable[hashval] = new_list;
size++;
}else {
htable[hashval]->freq++;
}
printf("ta evale epitixws \n");
}
void clear_ht()
{
int i;
for(i=0; i<HTABLE_SIZ; i++) {
while(htable[i]->token!=NULL) {
htable[i]->token=NULL;
htable[i]->freq=NULL;
free(htable[i]);
}
}
}
int compare(const void *elem1, const void *elem2)
{
const struct node *p1 = elem1;
const struct node *p2 = elem2;
if (p1->freq > p2->freq)
return(+1);
else if (p1->freq < p2->freq)
return(-1);
else
return(0);
}
void print_ht()
{
int i, j=0;
link l, *vector = (link*) malloc(sizeof(link)*size);
for (i=0; i < HTABLE_SIZ; i++)
for (l=htable[i]; l; l=l->next)
vector[j++] = l;
qsort(vector,size,sizeof(link),compare);
for (i=0; i < size; i++)
printf("%-50s\t%7d\n",vector[i]->token,vector[i]->freq);
free(vector);
}
Ι found the solution. Apparently for some reason my compare function was wrong.
I still haven't figured out why but here is the correct one, hopefully someone else will find this post helpful!
int compare(const void *elem1, const void *elem2)
{
return (*(link*)elem2)->freq - (*(link*)elem1)->freq;
}
This is an incomplete code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define W 1031
#define B 256
struct FileCoordinates{
int x; /*line number*/
int y; /*word number*/
struct FileCoordinates *next;
};
struct FileStruct{
char *filename;
struct FileCoordinates *coordinates;
struct FileStruct *next;
};
struct WordStruct{
char *word;
struct WordStruct *left;
struct WordStruct *right;
struct FileStruct *files;
};
typedef struct FileCoordinates *CoorPtr;
typedef struct FileStruct *FilePtr;
typedef struct WordStruct *WordPtr;
WordPtr HashTable[W];
long int power(int a, long b){
long int pow, i;
pow = 1;
for (i = 0; i < b; i++){
pow = pow*a;
}
return pow;
}
int hashvalue (char *word){
long int i, value=0, n;
n = strlen(word);
for (i=0; i<n; i++){
value = value + power(B,n-i-1) * word[i];
}
return(value%W);
}
void putPosition(int x, int y, FilePtr *currfile){
CoorPtr currcors = (*currfile)->coordinates;
while (currcors!=NULL){
currcors = currcors->next;
}
currcors = (CoorPtr)malloc(sizeof(struct FileCoordinates));
currcors->x=x;
currcors->y=y;
}
void putFile(char *filename, WordPtr *currWord, int x, int y){
FilePtr currfile = (*currWord)->files;
while(currfile != NULL && strcmp(currfile->filename,filename)!=0){
currfile=currfile->next;
}
if (strcmp(currfile->filename,filename)==0){
putPosition(x, y, &currfile);
}
else{
currfile = (FilePtr)malloc(sizeof(struct FileStruct));
currfile->filename = filename;
putPosition(x, y, &currfile);
}
}
void insert(char *word, WordPtr *leaf, char *filename, int x, int y)
{
if( *leaf == NULL )
{
*leaf = (WordPtr) malloc( sizeof( struct WordStruct ) );
(*leaf)->word = word;
putFile(filename, &(*leaf), x, y);
/* initialize the children to null */
(*leaf)->left = 0;
(*leaf)->right = 0;
}
else if(word < (*leaf)->word)
{
insert( word, &(*leaf)->left, filename, x, y);
}
else if(word > (*leaf)->word)
{
insert( word, &(*leaf)->right, filename, x, y);
}
else if(word == (*leaf)->word){
putFile(filename, &(*leaf), x, y);
}
}
int main(int argc, char *argv[]){
int i, words, lines, value;
char *filename, *word, c;
FILE *fp;
word = (char *)malloc(21*sizeof(char));
if (argc<2){
perror("no files were inserted");
}
for (i=1; i<argc; i++){
words=1;
lines=1;
fp = fopen(argv[i], "r");
if (fp==NULL){
printf("Could not open file named %s! \n", argv[i]);
return 2;
}
filename = malloc( strlen( argv[i] ) + 1 );
strcpy( filename, argv[i] );
fscanf(fp, "%s", word);
value=hashvalue(word);
c=getc(fp);
insert(word, &HashTable[value], filename, lines, words);
if (c==' '){
words = words+1;
}
else if(c=='\n'){
lines=lines+1;
words=1;
}
}
system("PAUSE");
return 0;
}
And debugger gives me segmentation fault at this part:
while(currfile != NULL && strcmp(currfile->filename,filename)!=0){
currfile=currfile->next;
}
The reason of the code is to get text files as arguments, sort the words into binary trees placed in the hashtable and then by searching the keyword it shows you the coordinates it appears.
Anyway, i know this is a very novice code but im trying to understand.
You forget to set currfile->next to NULL when you create the object:
currfile = (FilePtr)malloc(sizeof(struct FileStruct));
currfile->filename = filename;
Reserve space with calloc instead of malloc, or add:
currfile->next = NULL;
Where do you initialize files? (Hint: you don't). This means it is an undefined value (not necessarily NULL). You need to initialize files before calling putFile.
As noted in the comment, once you fix that one, you need to do the same for next in both FileCoordinates and FileStruct and for coordinates in FileStruct.
Also as noted in the comments, you reuse the word buffer, meaning that all the nodes in the tree will have the same string. You should allocate a new buffer for the string when it is stored in the tree. Perhaps use strdup.
(*leaf)->word = strdup(word);
Once that is fixed, you also need to work on your string comparison. if(word == (*leaf)->word) compares the pointers, not their contents. You need to use strcmp if you want to compare the actual string data.
else if(strcmp(word, (*leaf)->word) == 0){
I'm trying to make a queue that dynamically increases by it's size by 3 when it reaches the size of 3 the first time. I haven't used malloc or realloc that much, but afaik they should be correct in the code
**Header:**
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
typedef char Titem;
/*The interface of queue */
#define MAXN 3
typedef enum {NOT_OK, OK } Tboolean;
typedef struct {
Titem array[MAXN];
int number_of_items;
} Tqueue;
void initialize_queue (Tqueue *Pqueue);
Tboolean enqueue( Tqueue *p, Titem item);
Tboolean dequeue( Tqueue *p, Titem *Pitem);
void print_queue(const Tqueue *Pqueue);
**Queue functions:**
#include "jono.h"
void initialize_queue ( Tqueue *Pqueue) {
int size = 0;
Pqueue->number_of_items = 0;
*Pqueue->array = (Titem) calloc (MAXN, sizeof(MAXN));
size = sizeof (Pqueue->array);
printf ("%d\n", size);
}
Tboolean enqueue( Tqueue *Pqueue, Titem item) {
int size = 0;
if (Pqueue->number_of_items >= MAXN) {
*Pqueue->array = (Titem) realloc (Pqueue->array, sizeof(Pqueue->array) + MAXN);
size = sizeof (Pqueue->array);
printf ("\%d", size);
Pqueue->array[Pqueue->number_of_items++] = item;
return(OK);
}
else {
Pqueue->array[Pqueue->number_of_items++] = item;
return (OK);
}
}
Tboolean dequeue( Tqueue *Pqueue, Titem *Pitem) {
int i;
if (Pqueue->number_of_items == 0)
return(NOT_OK);
else {
*Pitem = Pqueue->array[0];
for (i = 0 ; i < Pqueue->number_of_items-1 ; i++)
Pqueue->array[i] = Pqueue->array[i+1];
Pqueue->number_of_items--;
return (OK);
}
}
void print_queue (const Tqueue *Pqueue) {
int i;
printf("\nQueue now: \n\n");
for (i = 0 ; i < Pqueue->number_of_items ; i++ ) {
printf(" %c ", Pqueue->array[i]);
}
printf("\n\n");
}
**Main:**
#include "jono.h"
int main(void) {
Tqueue queue;
Tboolean succeed;
char chr;
initialize_queue(&queue);
printf("\nEnter a letter to be queued ");
printf("\nor digit 1 to dequeue a letter");
printf("\nor Return to quit a program\n");
chr = _getche();
while (chr != 10 && chr != 13) {
if (isalpha(chr)) {
succeed=enqueue(&queue, chr);
print_queue(&queue);
if (!succeed)
printf("\n Enqueue operation failed\n");
}
if (chr == '1') {
succeed = dequeue(&queue, &chr);
if (succeed) {
printf("\na letter dequeued %c ", chr);
print_queue(&queue);
}
else printf("\nDequeue operation failed\n ");
}
chr = _getche();
}
}
typedef struct {
Titem array[MAXN]; /* char array[3] */
int number_of_items;
} Tqueue;
*Pqueue->array = (Titem) calloc (MAXN, sizeof(MAXN));
You can not reserve space for an array (only pointers can use (m/c/re)alloc)
Can anyone give me some indication as to why array of structs doesn't print out properly ?
I think its something to do with the memory I have allocated to the struct I am unsure !!
Using mac osx mountain lion xcode 4 gcc
Thanks for any help completely stuck!!
(Please have patience I am only a student !)
#include <stdio.h>
#include <limits.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
typedef struct{
char* one;
char* two;
} Node;
Node *nodes;
int count = 0;
//-----------------------------------------------------------------------
void add(char *one,char*two){
char x[40];
char y[40];
printf("reached..\n");
strcpy(x,one);
strcpy(y,two);
printf("--> X: %s\n",x);
printf("--> Y: %s\n",y);
Node newNode;
newNode.one = x;
newNode.two = y;
nodes[count]= newNode;
count++;
}
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
void print(){
int x;
for (x = 0; x < 10; x++)
{
printf("%d : (%s, %s) \n",x,nodes[x].one, nodes[x].two);
}
}
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
void check(char **arg)
{
if(strcmp(*arg, "Add") == 0)
{
add(arg[1],arg[2]);
}else if(strcmp(*arg,"print") == 0)
{
print();
}else{
printf("Error syntax Enter either: \n Add [item1][item2]\n OR \n print\n");
}
}
//-----------------------------------------------------------------------
void readandParseInput(char *line,char **arg)
{
if (fgets (line, 512, stdin)!= NULL) {
char * pch;
pch = strtok (line," \n\t");
int count = 0;
arg[0] = pch;
while (pch != NULL)
{
count++;
pch = strtok (NULL, " \n\t");
arg[count] =pch;
}
}else{
printf("\n");
exit(0);
}
}
//-----------------------------------------------------------------------
int main()
{
int size = 100;
nodes = calloc(size, sizeof(Node));
int i;
for(i = 0;i <100; i++){
printf("%s , %s \n",nodes[i].one,nodes[i].two );
// nodes[i].one = ".";
// nodes[i].two = ".";
}
char line[512]; /* the input line */
char *arg[50]; /* the command line argument */
while (1)
{
readandParseInput(line,arg);
if(arg[0] != NULL){
check(arg);
}
}
return(0);
}
You're keeping pointers to the following automatic variables:
char x[40];
char y[40];
These go out of scope when add() returns, leaving you with dangling pointers.
You either have to turn Node::one and Node::two into arrays, or allocate memory for them on the heap.
In you add() function, you cannot assign one struct to another via an = operator... you would have to copy it...
memcpy( &nodes[count], &newNode )
#include <stdio.h>
#include <stdlib.h>
typedef struct {
char *fn;
}NAME;
#define NAME_LEN 20
int main()
{
NAME name;
name.fn = (char *) calloc(NAME_LEN, sizeof(char));
strcpy(name.fn, "Namco");
printf("Name: %s\n", name.fn);
free(name.fn);
return 0;
}
you can't just assign a string like this in c
newNode.one = x;
newNode.two = y;
what is newNode.one referring to???
at Function add
newNode.one = x;
newNode.two = y;
to
newNode.one = strdup(x);
newNode.two = strdup(y);
I'm writing a program in C that's supposed to take several string of text from a file and put them inside a dynamic list.
For some reason (probably memory allocation) I get this error every time I try to put more than fifteen strings into the structure:
*** glibc detected *** ./driver: realloc(): invalid next size: 0x000000000241e250 ***
The code is as follows:
dlist.h
struct dlist
{
int size;
int maxSize;
char item[1][1024];
};
#define INITSIZE 6
#define INCRSIZE 9
#define DLISTSIZE(n) ((size_t)(sizeof(struct dlist) + (n*1024)))
struct dlist *initDlist(int num);
int insDlist(char data[], struct dlist **p);
void printDlist(struct dlist *p);
void debugDlist(struct dlist *p);
int stringCmp(const void *a, const void *b);
dlist.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dlist.h"
struct dlist *initDlist(int num)
{
struct dlist *p;
p = malloc( DLISTSIZE(num) );
if(p == NULL)
return(NULL);
p->size = 0;
p->maxSize = num;
return(p);
}
int insDlist(char data[], struct dlist **p)
{
struct dlist *q;
//printf(" DEBUG: Checking remaining memory.\n");
if ((*p)->size == (*p)->maxSize)
{
//printf(" DEBUG: Out of memory, reallocating now...\n");
q = realloc(*p, DLISTSIZE((*p)->maxSize + INCRSIZE));
if(q == NULL)
return(-1);
q->maxSize += INCRSIZE;
*p = q;
}
//printf(" DEBUG: Space available.\n");
int i;
(*p)->size++;
//adding data to the list
for(i = 0; i < 1024; i++)
(*p)->item[(*p)->size][i] = data[i];
return(0);
}
void printDlist(struct dlist *p)
{
int i;
for(i = 0; i <= p->size; i++)
printf("%s", p->item[i]);
}
void debugDlist(struct dlist *p)
{
int i;
fprintf(stderr, "\nDynamic List Debug Data\n\n");
fprintf(stderr, " size = %d\n", p->size);
fprintf(stderr, " maxSize = %d\n", p->maxSize);
for(i = 0; i <= p->maxSize; i++)
fprintf(stderr, " %s\n", p->item[i]);
}
int stringCmp(const void* a, const void* b)
{
const char *ia = (const char *)a;
const char *ib = (const char *)b;
return strncmp(ia, ib, 1023);
}
driver.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "dlist.h"
int main(int argc, char *argv[])
{
printf("\n");
FILE *fp;
char text[1024];
//check the command line
if(argc != 2)
{
fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
return(-1);
}
//open file given on command line
fp = fopen(argv[1], "r");
if(fp == NULL)
{
perror(argv[1]);
return(-1);
}
//initialize the dynamic list
struct dlist *p;
p = initDlist(INITSIZE);
if(p == NULL)
{
perror("Unable to malloc dlist");
return(-1);
}
//read each line then store it in the dynamic list
while(fgets(text, 1024, fp) != NULL)
{
//printf("DEBUG: Preparing to insert data.\n");
if( insDlist(text,&p) == -1)
{
perror("Unable to realloc dlist");
return(-1);
}
//printf("DEBUG: Data inserted successfully.\n\n");
}
//debugDlist(p);
printDlist(p);
//printf("\nNow sorting...\n\n");
//qsort(&(p->item), p->size, 1, stringCmp);
//debugDlist(p);
//printDlist(p);
return(0);
}
Any help is appreciated, thanks in advance.
The issue is almost definitely that you are incrementing the size of the list before copying the data:
(*p)->size++;
//adding data to the list
for(i = 0; i < 1024; i++)
(*p)->item[(*p)->size][i] = data[i];
You should reorder these statements:
//adding data to the list
for(i = 0; i < 1024; i++)
(*p)->item[(*p)->size][i] = data[i];
(*p)->size++;
Also, if you're allowed, the following is equivalent:
// adding data to the list
memcpy( (*p)->item[(*p)->size],
data,
1024 );
(*p)->size++;
To elaborate, the indices are zero-based. As an example, when you allocate 6 arrays, you copy into the indexes [1], [2], [3], [4], [5], and [6].
You want to copy into indexes [0], [1], ... [5].
Additionally, the reason that you see the error only after allocating some specific number has to do with the heap allocator.