I have encountered a problem with the following function when I use it. I do not know what is going on, and would like to ask for advise. The program compiles without any error, but when I execute it, it just freezes and exits.

typedef long long unsigned int lluint;
typedef long long int llint;
typedef short unsigned int suint;

llint *genNumArray (const llint start, const llint end, const lluint byWhat){
	llint diff = start < end ? start - end : (start == end ? 1 : end - start), *genList = calloc (diff, sizeof (llint));
	for (llint i = 0;i != labs (diff) + 1;){
		*(genList + i) = i * byWhat * (diff > 0 ? 1 : 0 - 1) + start;
		if (start < end){
			i++;
		}else if (start > end){
			i--;
		}else {
			break;
		}
	}
	return genList;
	free (genList);
}

The reason why I say this is strange is because if I change it a bit, it works perfectly. The change is this.

typedef long long unsigned int lluint;
typedef long long int llint;
typedef short unsigned int suint;

llint *genNumArray (const llint start, const llint end, const lluint byWhat){
	llint diff = labs (start < end ? start - end : (start == end ? 1 : end - start)), *genList = calloc (diff, sizeof (llint));
	for (llint i = 0;i != diff + 1;){
		*(genList + i) = i * byWhat * (diff > 0 ? 1 : 0 - 1) + start;
		if (start < end){
			i++;
		}else if (start > end){
			i--;
		}else {
			break;
		}
	}
	return genList;
	free (genList);
}

This works however if I do this then "*(genList + i) = i * byWhat * (diff > 0 ? 1 : 0 - 1) + start;" will not work as well since diff will always be larger than 0. I have a hard coded solution for this problem, but will not use it unless it is absolutely necessary. My first question is, what is the real problem here, not the lines where it is, but the actual behavior of the compiler? And how would I be able to fix this without much change to the code? My version of mingw is 5.1.6. This is not a homework question, it is just a self project which is bothering me. And yes the header file above is in the same folder as my c file. Thank you for any help.

Oh yeah, sorry i forgot to show the c source of how I use the function. Here it is:

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include "myheader.h"

int main (){
	llint *result = genNumArray (0, 100, 2);
	for (lluint i = 0;i < 100 + 1;i++){
		printf ("%3llu - %3llu\n", i, *(result + i));
	}
	return 0;
}

Recommended Answers

All 5 Replies

First thing I'd do is simplify. These two statements llint diff = labs (start < end ? start - end : (start == end ? 1 : end - start)), *genList = calloc (diff, sizeof (llint)); *(genList + i) = i * byWhat * (diff > 0 ? 1 : 0 - 1) + start; are a convoluted mess. Break them into smaller statements so you can see what's really happening. Then if something doesn't work you can display individual components of the equation and see what's going on.

for (llint i = 0;i != labs (diff) + 1; ){

or

llint diff = labs (start < end ? start - end : (start == end ? 1 : end - start)), *genList = calloc (diff, sizeof (llint));
for (llint i = 0;i != diff + 1; ){

In both these cases, i goes from zero to some non-negative terminating value.

But by the condition :

else if (start > end){
            i--;

i will always become negative and wont reach to that non-negative terminating value .

So your program freezes when start is greater than end.

Hello and thanks for your help both of you.

@WaltP - In a way I see what you mean by convoluted mess. However I do have to point out that the example you used is the one that works. I see no reason why I should breaking them down, since I do not change that equation at all besides to move the labs () function. I did break it down nevertheless, but it did not help in any way. Thanks for your help.

@muditAggarwal - I know it might freeze when the start is larger than the end. But that is the exact reason why I needed help. I do not really understand what you mean since diff is not related to i. I might be wrong but I still do not think you are right about this. Please explain in more detail.

Thanks and sorry if I might seem impolite, but your answers are not working.

Ohh I get it. Thank you, muditAggarwal. Unfortunately, this does not solve my initial problem, but thank you for pointing this out it completely passed me. Now I still get the same freezing problem. This is the modified code that works when start is smaller than end:

typedef long long unsigned int lluint;
typedef long long int llint;
typedef short unsigned int suint;

llint *genNumArray (const llint start, const llint end, const lluint byWhat){
	llint diff = labs (start < end ? start - end : (start == end ? 1 : end - start)), *genList = calloc (diff, sizeof (llint));
	for (llint i = 0;i != diff + 1;i++){
		*(genList + i) = i * byWhat * (diff > 0 ? 1 : 0 - 1) + start;
	}
	return genList;
	free (genList);
}

I am still getting my window frozen when I use this next function. As you might see this one uses labs () in the for loop and I think this is the issue. Can someone explain why? Here is the code:

typedef long long unsigned int lluint;
typedef long long int llint;
typedef short unsigned int suint;

llint *genNumArray (const llint start, const llint end, const lluint byWhat){
	llint diff = start < end ? start - end : (start == end ? 1 : end - start), *genList = calloc (diff, sizeof (llint));
	for (llint i = 0;i != labs (diff) + 1;i++){
		*(genList + i) = i * byWhat * (diff > 0 ? 1 : 0 - 1) + start;
	}
	return genList;
	free (genList);
}

Ok I solved it, I forgot to type cast it. Here is what it should be for anyone who might encounter this in the future.

llint *genNumArray (const llint start, const llint end, const lluint byWhat){
	llint diff = labs (start < end ? end - start : (start > end ? start - end : 1)), diff2 = (llint)labs ((long int) diff), *genList = calloc (diff, sizeof (llint));
	for (llint i = 0;i != diff2 + 1;i++){
		*(genList + i) = i * byWhat * (diff > 0 ? 1 : 0 - 1) + start;
	}
	return genList;
	free (genList);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.