5

The goal of my program is to read a file, and output the word with the max appearances, as well as the number of appearances. But I'm having issues with malloc and the syntax of it. This is the structure which malloc refers to:

struct Word_setup {
    char word[max_length];
    int count;
};

This section of my main helped me find out that this was my error:

    printf("Pre-Allocation Test");

    struct Word_setup *phrase;

    phrase = (struct Word_setup *) malloc(SIZE);

    if (phrase == NULL)
        {printf("Failure allocating memory"); return 0;}

It only seems to print out, Pre-Allocation Test, and then freezes. As I said before, I'm unclear how to fix this issue, but I've isolated it.

*Incase you're wondering what SIZE is:

#define SIZE (sizeof(phrase))


Edit:

For those curious about compiler version/OS/etc.: Windows 7 64bit, GCC 4.9.2

If you would like any more information on that just let me know.

1 Answers1

5
phrase = (struct Word_setup *) malloc(SIZE);

should be

phrase =  malloc(sizeof(struct Word_setup));

What you have is

#define SIZE (sizeof(phrase)) 

will give you size of pointer not size of structure. You can also use a more generic method of allocating memory

type *p = malloc(sizeof(*p));
Gopi
  • 19,784
  • 4
  • 24
  • 36
  • 2
    Minor style issue: `()` not needed. Could use `malloc(sizeof *p)` – chux - Reinstate Monica Apr 20 '15 at 17:02
  • If i do this, when I build the program in sublime, it tells me that there was an error converting the void pointer to the struct – krazibiosvn Apr 20 '15 at 17:03
  • "You can also use a more generic method of allocating memory" you can do pretty much nothing with a `void*` and you have to cast it to something to give it a meaning, hence getting back to [this](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc?rq=1). – edmz Apr 20 '15 at 17:04
  • @chux But we are bound to using sizeof() is both the ways so I think it should be fine from OP's perspective – Gopi Apr 20 '15 at 17:05
  • 1
    Using `()` is certainly OK.- simply pointing out a possible simplification. – chux - Reinstate Monica Apr 20 '15 at 17:07
  • @black Yes you are right !! wanted to give an idea of this approach.. Just to be generic I used `void` .. I will replace it by type – Gopi Apr 20 '15 at 17:07
  • @krazibiosvn This is a perfect code I don't see any C compiler complaining about this .. How are you compiling? – Gopi Apr 20 '15 at 17:08
  • @Gopi Honestly I'm not sure why. Its been pissing me off, its not like the program I'm working on is THAT difficult and I've been working on it for 15 hours and counting.... about 10 of those hours have been spent on this stupid error. When I'm compiling, I'm forcing it to compile into C using `-x c`. Are there any IDE's that you would suggest for Windows 7, because I have a love hate relationship with gcc and sublime right now. – krazibiosvn Apr 20 '15 at 17:12
  • Follow these steps and compile using gcc 1. Write your code in .c file 2. Compile with `cc filename.c -o file` 3. It should compile without any errors .. If it doesn't come back – Gopi Apr 20 '15 at 17:14
  • @Gopi you forgot the `g` but, that was what I was doing before, yet it still froze every time I would attempt to compile. – krazibiosvn Apr 20 '15 at 17:21
  • @krazibiosvn What's freezing, the compiler or the code? – edmz Apr 20 '15 at 17:25
  • @black The compiler (cmd), it freezes and I need to close the window. It doesn't allow me to type anything when it freezes either. – krazibiosvn Apr 20 '15 at 17:27
  • @krazibiosvn Strange. Can you post some info about your station (e.g. OS, compiler version, ...)? – edmz Apr 20 '15 at 17:28
  • @black From "*It only seems to print out, Pre-Allocation Test, and then freezes*" it is clear that it is not compiler freezing. – user3125367 Apr 20 '15 at 19:43