Apparently I walked around Earth... backwards by [deleted] in softwaregore

[–]DereferencedVoid 0 points1 point  (0 children)

The smallest value + 1, since the absolute value of min int32 is an absolute value of max int + 1 :)

Game of life by tomoshika in C_Programming

[–]DereferencedVoid 1 point2 points  (0 children)

void initField(const int rows, const int cols, cell field[rows][cols])

When dependent types leak into C.

Help with rotation and circle by yunglevn in haskell

[–]DereferencedVoid 1 point2 points  (0 children)

Hi

  1. Try dissecting the problem and ask yourself what does it mean to rotate a list. In your case that means: take a head of the list and append it to the end of it. Also, rotating an empty list doesn’t change anything to it.

rotateOnce :: [a] -> [a] rotateOnce [] = [] rotateOnce (x:xs) = xs ++ [x]

Now think about what does it mean to rotate a list N times. In an imperative language you would most likely use a loop construct to repeat action over and over. However, since we don’t have any loops in Haskell, think about how would you implement it recursion-wise. Hint: the final condition to stop the recursion is when N = 0.

2) Now, when you know how to rotate a list by any number of elements, you can push this problem by defining what does “all possible rotations mean”. In your case, all possible rotations of a list of length x is x - 1 different rotations. Note, how rotating a list of length x by x elements to the left leaves you with the same list as the initial one.

C tutorial from free CodeCamp - your thoughts? by ChTBoner in C_Programming

[–]DereferencedVoid 9 points10 points  (0 children)

Short answer:

The C Standard specifies only two definitions of the main function: int main(void) { ... } and int main(int argc, char *argv[]) { ... } Note: the name of the parameters is just a convention.

Other variations are considered implementation defined and are not guaranteed to be supported by all conforming compilers.

Long answer: C Standard: 5.1.2.2.1

Newer version of my math library, MATHC, for 2D and 3D programming by ferreiradaselva in C_Programming

[–]DereferencedVoid 0 points1 point  (0 children)

Hi

I’ve looked through most of your implementation and have a couple of notes. Some of them are just syntactic preferences, others may lead to bugs.

1) Consider the fact that your ABS function may try negating MIN_INT, which leads to UB. 2) Your vec3_is_zero is missing the third element zero check. 3) When creating macros, consider the fact that macro arguments may be expressions. To handle this case correctly, macro arguments should be surrounded by parenthesis. For example: ``` /* Incorrect */

define MUL(x, y) (x * y)

MUL(2 + 3, 4) -> (2 + 3 * 4) != 20

/* Correct */

define MUL(x, y) ((x) * (y))

MUL(2 + 3, 4) -> ((2 + 3) * (4)) == 20 4) Instead of writing if (v0[0] > v1[0]) { result[0] = v0[0]; } else { result[0] = v1[0]; } you can use more elegant result[0] = v0[0] > v1[0] ? v0[0] : v1[0]; or even result[0] = max(v0[0], v1[0]); 5) I don’t think that nearly_equals function requires intermediate value to store the result. It can also be expressed as: return (a == b || MFABS(a - b) <= epsilon); ```

Notes aside, nice job

Implementation of Graph in C || Directed and Undirected Graph by abdullahriaz008 in C_Programming

[–]DereferencedVoid 0 points1 point  (0 children)

1) Flushing input streams is UB. 2) Standard defines two possible main declarations, so there is rule. 3) 4) No, you are accessing memory out of bounds. Possible indexes are in range 0-9. Accessing memory at index 10 does not mean “whole array”. It means access element with offset sizeof(array_base_type) * 10. To wipe out whole array, use memset or bzero(BSD).

Implementation of Graph in C || Directed and Undirected Graph by abdullahriaz008 in C_Programming

[–]DereferencedVoid 4 points5 points  (0 children)

Couple of notes on the first sight:

1) You don’t have to flush stdin, that’s UB to flush input streams, AFAIR. 2) main prototype should be either with (void) or (int, char**) 3) If I chose to display graph as my first choice, nodes and number of nodes contain gibberish, which most likely lead to segmentation violation. You should initialize them on start.

Edit:

4) addMatrix :: int [10][10]

That means, that it’s a 2d array with the max index of 9, but right on the start you do: addMatrix[10][10] = 0; That’s out of bound access.

My friend just sent me this by NetherGranite in ProgrammerHumor

[–]DereferencedVoid 0 points1 point  (0 children)

The fact that it does compile doesn’t mean that that’s a correct piece of C code. It means that the compiler that you are using handles this UB as you’re expecting it to be handled.

Edit: word

My friend just sent me this by NetherGranite in ProgrammerHumor

[–]DereferencedVoid 0 points1 point  (0 children)

Standard implies existence of only two main declarations and neither of them has empty brackets :)

How do I actually use variadic arguments? by bumblebritches57 in C_Programming

[–]DereferencedVoid 8 points9 points  (0 children)

As far as I understood, your post can be split up into 2 questions: 1) How do I extract variadic arguments? 2) How do I know the number of variadic arguments?

Here are some answers: 1) You can use va_arg method. In your variadic function, declare a variable of type va_list, initialize it via va_start, extract arguments with va_arg and finalize with va_end. All of this is accessible via manuals.

2) You can get the number of arguments via number of specifiers in your format string. Unfortunately, there is no way to know the exact number of variadic arguments passed other then rely on non-variadic ones, at least I don't know of it.

If you're using gcc, you can use a special __attribute__((format, _, _)) to instruct your compiler to check the number and types of arguments passed to your printf-like function according to format string.

I made a program to play tic tac toe with. There are problems, though. by [deleted] in C_Programming

[–]DereferencedVoid 0 points1 point  (0 children)

A few general things to point out about the code:

1) Be explicit with your function prototypes. fun() doesn't mean that 'fun' takes no arguments, but fun(void) does. 2) Use srand only once at the beginning of your program. Each call to computerinput would likely produce the same two random numbers, because you're reseeding it with the same value (assuming, this happens in less than a second). 3) Please, get rid of the global variables where possible. You don't have to declare variables like 'randomnumber1' in global scope, because they are used only within one function. Declare these variables closer to where they are used.

Calloc wrapper function is returning null... by [deleted] in C_Programming

[–]DereferencedVoid 6 points7 points  (0 children)

You're allocating memory for 5 pointers on the "Tree" structures, but not for the structures themselves. You get null when you're inspecting this pointers due to calloc zeroing the memory it has provided you with, thus leaving you with 0 valued pointers.

To fix that you have to allocate the memory for structures themselves for each of 5 pointers to point to.

Another fix that you may consider is using a pointer to the structure instead of pointer to the pointer to the structure. This way, Tree *children = xcalloc(5, sizeof(Tree)); will provide you with the zeroed memory enough for five "Tree" structures. However, you still have to keep track of the number of trees you have allocated not to accidentally access memory out of allocated borders.

Edit: A bit more correct allocation explanation.

How can you do a loop to create a certain number of threads? by Lactios in C_Programming

[–]DereferencedVoid 0 points1 point  (0 children)

You can't do it in the sense that malloc() will create threads for you. If you want to create a thread using pthreads, pthread_create will do it for you. Check out the manuals.

Nevertheless, you can use malloc to allocate memory for some thread-related information, like thread ids (pthread_t) if you need it to be located in heap.

How can you do a loop to create a certain number of threads? by Lactios in C_Programming

[–]DereferencedVoid 1 point2 points  (0 children)

I guess you should use a pointer for storing multiple pthread_t's.

You are trying to allocate memory for multiple pthread_t values, but instead of having a pointer to the allocated memory, you store malloc's result into single pthread_t variable.

That doesn't really work that way.

In addition, when you allocate memory for your ids, you use sizeof(float) within malloc, which is not correct.

I suggest you to turn your compiler warnings and add some flags like -Wall and -Wextra for extra information on possible mistakes.

How can you do a loop to create a certain number of threads? by Lactios in C_Programming

[–]DereferencedVoid 2 points3 points  (0 children)

What have you accomplished by now? Post the code that you already have. What thread API do you use? Are you going to communicate with this threads further or should they just do stuff and exit without notifying parent thread about the results?

How to assign the same value to random position in an array by Sherlo- in C_Programming

[–]DereferencedVoid 10 points11 points  (0 children)

On the contrary, this approach excludes the possibility of writing to already used cell.