Click here to Skip to main content
15,888,323 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I read recently about the use of the size_t variable in C99.
I sow many people using it in dynamic arrays , some people for loop and others everywhere (eg in function vars).

My main question is this:

I know that size_t takes unsigned values only and that can used in replace of usigned long variable type.

I recently used it and I can't understand if I took the main idea. Can I use size_t everywhere I need a unsigned int or unsigned long type of variable?

My piece of code:

C
//in main function
FILE *input = fopen("input.txt","r");
size_t size = read_number(input);
int *array = malloc(size * sizeof *array);

//read the size of dynamic allocation 
size_t read_number(FILE *input) {
size_t size = 0;
fscanf(input, "%d",&size);

return size;
}
Posted

This is not a "variable", this is a type: http://www.cplusplus.com/reference/cstring/size_t[^].

You should use this type to calculate data sizes, not unsigned long or anything else.
Also, sizeof *array is a size of an element, not a size of array. To get a size of array, multiply element size by the number of elements. (You actually correctly get the size of the data to allocate.)

—SA
 
Share this answer
 
v3
Comments
[no name] 13-Jan-15 11:45am    
so my code is false?
Sergey Alexandrovich Kryukov 13-Jan-15 11:47am    
I would say, it's wrong. :-)
—SA
[no name] 13-Jan-15 11:50am    
ok thank you. I need to learn how to use the size_t. Maybe when I'll really need it! :) For now the classic unsigned int helps!
[no name] 13-Jan-15 11:51am    
Using int my code isn't stable? And replace int *array = malloc(size * sizeof *array); with int *array = malloc(size * sizeof(int)); ?
Sergey Alexandrovich Kryukov 13-Jan-15 12:06pm    
Sorry, I probably overlooked something in your code. The size of allocated memory is calculated correctly, assuming the size value is correct.
—SA
size_t will usually be the biggest unsigned integer type on your system. If you use it wherever you're dealing with sizes of objects (duh, clue in the name!) and loop indices your code will probably not go horribly wrong on you and you might be protected from size of integer related portability problems.

However it can open cans of worms if you don't know what you're doing. Your code for example tells fprintf that you're trying to read a signed integer into a size_t - not necessarily what you wanted! Add to that on 32 bit systems a signed integer is likely to be half the size of a size_t so you've got a potential problem on your hands.

As you're using C99 (the //comments give it away) then you can get over the printf family disaster by using the (IIRC) %z format specifier which maps to whatever size_t is defined as.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 13-Jan-15 11:55am    
5ed.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900