2014-10-03 3 views
0

우리가 사용하고있는 코드에 오류가있어 누군가 디버깅하는 데 도움이되는지 궁금합니다. malloc 오류가 발생하는 것 같습니다. 감사.구조체 배열로 단어를 읽는 기능

void readWords(char norm_word[MAXSIZE], Word ** array) { 
int i = 0; 
bool found = false; 
int result = 0; 

Word * current_pointer = malloc (sizeof(Word*));//creates a temporary variable for each pointer in the array 

for (i=0; i<word_counter; i++) { 
    current_pointer = *(array+i); //accesses the current pointer 
    result = strcmp(norm_word, (current_pointer -> word)); //compares the string to each stored string 
    if (result == 0) { 
     found = true; 
     (current_pointer->freq)++; 
     break; 
    } 
} 

if(!found) { 
    if(pointer_counter == word_counter) { 
     array = realloc(array, sizeof(array)*2); 
     pointer_counter*=2; 
    } 

    Word * new_pointer = (Word*) malloc (sizeof(Word*)); 
    strcpy(new_pointer -> word, norm_word); 
    *(array + (pointer_counter - 1)) = new_pointer; 
    word_counter++; 
} 
; 
} 
+4

넌 아닌 'Word' 구조, A * 포인터 *의'sizeof' 메모리 할당된다 : 여기

짧은 구현이다. 두번. – usr2564301

+0

그래서 단어 current_pointer = malloc (sizeof (char) * 512); – user48944

+1

'sizeof (Word)'이면'예 '입니다. 나도 몰라, 당신은 샘플 코드에 포함시키지 않았다. – usr2564301

답변

1

모든 포인터의 크기는 시스템에서 동일합니다. 따라서 sizeof은 항상 포인터에 대해 동일한 크기를 반환합니다. 구조체에 할당하고자하므로 별표없이 이름에 sizeof을 사용해야합니다. malloc 나중에 그 메모리 블록에 대한 포인터를 반환합니다.

#include <iostream> 
#include <string> 

typedef struct 
{ 
    int num; 
    int numnum; 
}numbers; 


int main(int argc, char ** argv) 
{ 

    numbers* n = (numbers*)malloc(sizeof(numbers)); 

    n->num = 1; 
    n->numnum = 2; 

    free(n); 

    return 0; 
} 
0
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <stdbool.h> 

#define MAXSIZE 64 

typedef struct word { 
    char word[MAXSIZE]; 
    int freq; 
} Word; 

int word_counter = 0; 
size_t pointer_counter = 16;//Number of pointers that ensure 

void readWords(char norm_word[MAXSIZE], Word ** array) { 
    int i = 0; 
    bool found = false; 

    Word *current_pointer = *array; 

    for (i=0; i<word_counter; i++) { 
     if(strcmp(norm_word, current_pointer->word) == 0){ 
      found = true; 
      current_pointer->freq++; 
      break; 
     } 
     ++current_pointer; 
    } 

    if(!found) { 
     if(pointer_counter == word_counter) { 
      pointer_counter *= 2; 
      *array = realloc(*array, sizeof(Word)*pointer_counter); 
     } 
     Word *new_pointer = *array + word_counter; 
     new_pointer->freq = 1; 
     strcpy(new_pointer->word, norm_word); 
     ++word_counter; 
    } 
} 

int main(void){ 
    Word *vocabulary = calloc(pointer_counter, sizeof(Word)); 
    char norm_word[MAXSIZE]; 
    while(1==scanf("%s", norm_word)){ 
     readWords(norm_word, &vocabulary); 
    } 
    { 
     int i; 
     for(i = 0; i < word_counter; ++i){ 
      printf("%s(%d)\n", vocabulary[i].word, vocabulary[i].freq); 
     } 
    } 
    free(vocabulary); 
    return 0; 
} 
+0

제 생각에는 단어에 대한 2 차원 포인터 배열을 만들고 싶지 않습니다. 단어 ** 어휘 = calloc (포인터 _ 카운터, 크기 (Word *)); 과 내가 Heres는 내 모든 기능 – user48944

+0

내 주요 ** int 주 (int argc, CONST의 char *의 변수는 argv []) { 경우에 통과하고있어 무엇 (는 argc! = 3) { 의 printf ("당신 올바른 수의 명령 행 인수를 입력하지 않았습니다. "); return 1; } const char * infilename = argv [1]; const char * outfilename = argv [2]; Word * 어휘 = calloc (pointer_counter, sizeof (Word *)); readfile (infilename, & vocabulary); sortarray (& vocabulary); printHist (& vocabulary); 읽기 전용 파일 (infilename, & vocabulary); return 0; ** ** – user48944

+0

@ user48944 눈을 뜨세요. '단어 * 어휘 = calloc (pointer_counter, sizeof (Word *));''단어 * 어휘 = calloc (pointer_counter, sizeof (Word)); – BLUEPIXY

관련 문제