1

I have this enum type:

enum Cell { ALIVE='X', DEAD='O' };

And I allocate an array with it:

h_board = (Cell*) malloc(width*height*sizeof(char));

I assume I'm not doing this wrong since Cell values are chars (I'd like to stay with char sized data but I care for readability, that's why I used the enum.)

Upon free(h_board); an exception is thrown. In debugging mode I can see a heap corruption warning. I guess I'm freeing more memory than I'm allocating, but I can't see why. I also tried free((char*)h_board); trying to enforce char size deallocation, but the problem persists.

How can I fix this?

kaoD
  • 1,534
  • 14
  • 25
  • You're overwriting something somewhere. Show some code ? – cnicutar Feb 22 '12 at 13:11
  • What is the type of h_board ? I assume it's `Cell*`. What does Valgrind tell you ? Could you put the minimal code needed to recreate this problem and show us what happens to h_board ? – Eregrith Feb 22 '12 at 13:11
  • I don't see anything wrong yet, but you'd better use `sizeof(Cell)` instead of `sizeof(char)`. – kennytm Feb 22 '12 at 13:12
  • Cell values are not chars they are ints, better to use `sizeof(Cell)` – Naveen Feb 22 '12 at 13:12
  • @cnicutar @Eregrith The minimal code is pretty much that. Define `h_board` as `Cell*`, put it in a `main()` and it runs. – kaoD Feb 22 '12 at 13:13
  • @Naveen I see, I thought definining them as 'char' made them chars. Can't I enforce enum's size to char? I guess not and the int is just an index, am I right? – kaoD Feb 22 '12 at 13:14
  • Just because the `enum` values are `char` doesn't mean `sizeof(Cell) == sizeof(char)`: it's not. – Ernest Friedman-Hill Feb 22 '12 at 13:14
  • Would the downvoter care to explain? Even if I'm wrong, it's still a valid question... (if I were right I wouldn't ask!) – kaoD Feb 22 '12 at 13:23

2 Answers2

4

First of all, Cell is it's own datatype (which doesn't necessarily map to char), so use malloc with that (plus, we don't cast malloc in C):

Cell *h_board = malloc(width * height * sizeof(Cell));

Second, we need the full code to be able to help you accurately. This should run without any errors, perhaps you can build from here:

#include <stdlib.h>

int main(int argc, char **argv) {
    Cell *h_board;

    h_board = malloc(20 * 30 * sizeof(*h_board));
    free(h_board);

    return 0;
}
orlp
  • 112,504
  • 36
  • 218
  • 315
  • I'm not sure I need to post more code, that's all I needed. Thanks. I just wanted to enforce char size for enum... but I guess I just can't. – kaoD Feb 22 '12 at 13:16
  • 2
    kaoD: the whole purpose of an enum is to make type-safe easy numerical constants that will have good performance. It's more than logical that the compiler chooses `int`, because that is usually the word-size on implementations, and word-size is the fastest to process for the CPU. If you want constants with the type `char` you can still write `const char ALIVE = 'x';`, etc. – orlp Feb 22 '12 at 13:18
  • By the way, not casting to `Cell*` won't work. To be honest, I'm not sure if this is C or C++ (I'm progamming for CUDA) but I need to cast it. – kaoD Feb 22 '12 at 13:19
  • 1
    @kaoD: then you are writing C++, but you tagged your question C. – orlp Feb 22 '12 at 13:21
  • 1
    @kaoD: in C++ you can use `new[]` and `delete[]` instead of `malloc` and `free`. – Naveen Feb 22 '12 at 13:30
2

It should be sizeof(Cell). Making any assumptions about the size of the enum can be dangerous. Just for your reference : What is the size of an enum in C?

Community
  • 1
  • 1
eminemence
  • 721
  • 1
  • 6
  • 21