Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Problem with a simple C app. Calloc

Name: shaun 2010-06-04 8:43

I'm making this simple C program.  I'm using Calloc to allocate space to hold an array containing the number of instances of each letter in the file.
Source: http://pastebin.com/G46Pr6Tg

When I try to Calloc, or Malloc then memset the memory, one of the items in the array is not initialised to zero.  This is part of the array straight after initialisation:
......
Number of f: 0
Number of g: 0
Number of h: 134777
Number of i: 0
Number of j: 0
......

What did I do wrong?

Name: Anonymous 2010-06-04 8:46

>>1
calloc
Found your problem. Write your own memory allocator.

Name: Anonymous 2010-06-04 8:48

>>2
Thx, got any source for a better verison?

Name: Anonymous 2010-06-04 8:49

letters = (int *)calloc(arrayLength, sizeof(char));
should be
letters = calloc(arrayLength, sizeof(int));

Name: Anonymous 2010-06-04 8:52

>>4
You sir are a genius.  It was staring me in the face all along.  I think this problem must have started when I began using int instead of char.

Thank you very much

Name: Anonymous 2010-06-04 8:55

>>5
No problem. Fresh eyes, and all that

Name: Anonymous 2010-06-04 9:14

>>6
YOU HELPED HIM!

Name: Anonymous 2010-06-04 9:44

>>7
Problem?

Name: Anonymous 2010-06-04 9:45

>>8
No[1]

___________
1. >>6

Name: Anonymous 2010-06-04 10:51

You should also be aware that you're mixing declarations and code at the start of your main function for no apparent reason, and you have Sepples style comments.

(I'm assuming you're trying to write C89, otherwise your declaration style doesn't make a whole lot of sense.)

Name: Anonymous 2010-06-04 13:32

>>1,5
I think this problem must have started when I began using int instead of char.

This is why you should do this instead:

letters = calloc(arrayLength, sizeof(*letters));

or

letters = malloc(arrayLength * sizeof(*letters));
memset(letters, 0, (arrayLength * sizeof(*letters)));


This way if you change the type, the size will be correct.

Also if you cast to (int *) for C++ compatibility, you will get a compile error when you change the type, so there's no danger there either.

Name: Anonymous 2010-06-04 13:43

Not casting array pointers considered bad style.

Name: Anonymous 2010-06-04 13:47

>>2
Well since you said this, i am not sure how to alloc a page on linux, maybe you can help me?
I already have a nalloc function, but it only uses brk() and runs out of mem fast?

im not OP by the way.

Name: Anonymous 2010-06-04 13:55

>>13
mmap is how many mallocs do it.

Name: Anonymous 2010-06-04 14:14

>>14
Thank you, using the anonymous thingy flag right (im on winblows right now)?
And i also dont know how to check i my current program break is at the end of the page, should i be using a ASM instruction or is there a better way.

I may sound like a total noob for asking, but the page address is just a 16 bit addr stored in the xs registers, right?

I feel kind of bad for asking, i had some troubles with my function and troubles finding (understandable) information on x86 memory handling.

If anybody knows some good and somewhat short articles/tuts on x86 internals please give me a link.

Name: Anonymous 2010-06-04 14:18

>>15
Just look at a libc source. There's tons.

Name: Anonymous 2010-06-04 14:18

>>12
Is that still true? To be honest, I was starting to feel a bit dated by my continued practice of casting returns from malloc.

Name: Anonymous 2010-06-04 14:31

>>16
Will do, but it would still be nice to have some good documentation on x86 memory internals in practice.

I kind of looked at that kind of things, but i have the feeling i know less about x86 now then i did before.
Also most of the stuff on the net is quite theoretical, would be cool i had something that could give me an overview of the x86 internals and some ASM examples of that kind.

Name: Anonymous 2010-06-04 15:15

>>17
Casting your malloc/calloc/realloc calls is unnecessary

Name: Anonymous 2010-06-04 15:15

>>18
order those free manuals from intel.

Name: Anonymous 2010-06-04 15:22

>>20
"Free" as in "free .pdf".

Name: Anonymous 2010-06-04 15:27

>>20
"Free" as in "man 3 free"

Name: Anonymous 2010-06-04 17:15

>>19
It is necessary for C++ compatibility. You will probably want your code compilable by a sepples compiler at some point (especially since Microsoft's compiler and Visual Studio don't support C99). Add -Wc++-compat to your compiler options to catch these things with GCC/Clang.

Casting your malloc()s is just about the only major compatibility issue you run into with C99 vs C++, so you may as well just do it or make some macros to do it for you. (The only other real issue is trying to use enums as ints, but few people do that in practice.)

Name: Anonymous 2010-06-04 17:30

>>23
Sepples compatibility is a red herring. Casting malloc return values is a good idea because makes intent plainer and will help catch bugs. It's just good style.

Compiling code in one language with a compiler for another language, even if it's technically possible, is always a bad idea. It's not like there's a shortage of genuine C compilers on any platform, including broken toy OSes.

Name: >>11,23 2010-06-04 17:42

>>24
Honestly now. I'm a Linux man myself, but calling Windows a "broken toy OS"? Not everyone has the luxury of ignoring the world's largest computing platform, or of dictating which compilers will be used to compile our code.

Name: Anonymous 2010-06-04 17:46

>>23
Who wants to compile their C with a C++ compiler anyways? And I was speaking for C89, as C99 is utter shite compared.

>>24
The intent is plain based on the type of variable you're assigning to. If I have
int *i;
/* ... */
i = malloc(5 * sizeof(int));

it's pretty damn obvious what I'm allocating/doing. Adding the preceding (int *) is useless and adds clutter.

Name: Anonymous 2010-06-04 17:55

Don't cast malloc without also including stdlib.h

Name: Anonymous 2010-06-04 17:58

>>19
No shit, the question is whether it is still bad form to do so.

>>23
Regarding enums, I do it on occasion.

>>24
If you pass the type in the call to malloc the intent is obvious, rather than using some godawful practice like making the compiler detect the type from a variable.

Name: Anonymous 2010-06-04 18:03

>>26
And I was speaking for C89, as C99 is utter shite compared.
What? Why is C99 shite?

I imagine you don't like const and inline, but think about what you get for it: mixed variable declarations, complex, bool, restrict, VLAs, variadic macros, stdint, one-line comments...

Name: Anonymous 2010-06-04 18:48

>>29
Funny, I'm of the opinion there's nothing wrong with inline, but nearly all the other things you listed are abhorrent. Particularly mixed variable declaration and one-line comments.

Name: Anonymous 2010-06-04 18:58

>>25
Your argumentum ad populum does not refute the position that Windows is 1. broken, and 2. a toy.
And if your employer or project manager is insisting you compile your code with a compiler for a different language, he's dangerously incompetent and should not be catered to any more than someone advocating compiling Python with GHC should be.

Name: Anonymous 2010-06-04 19:06

>>30
Okay, then explain yourself. I don't know if I've ever heard anyone complain about mixed declarations and one-line comments. They are very easy for a compiler to implement, and mixed declarations are extremely useful. What is bad about them?

Name: Anonymous 2010-06-04 19:39

>>32
You must be new here.

Name: Anonymous 2010-06-04 19:48

>>32
Let's not have this whole significant whitespace argument again. It is tedious and you are wrong.

Name: Anonymous 2010-06-04 20:04

>>34
Whitespace is always significant.  You're a fool.

Name: Anonymous 2010-06-04 22:41

>>34
Whitespace is already significant in C by the preprocessor, and stripping comments is supposed to happen before even applying the preprocessor. Is this seriously your problem with one-line comments?

Don't change these.
Name: Email:
Entire Thread Thread List