C help - where did this error come from??

Soldato
Joined
22 Oct 2005
Posts
2,883
Location
Moving...
I've got an array in my program defined using :

Code:
distanceHoriz = (double*)malloc((count*heightSize) * sizeof(double));

and I add data to it using:

Code:
distanceHoriz[(j*WIDTH+i)] = temp;



Now this was working fine until a few hours ago when I started making some changes to a completely seperate part of the program (it was to do with making other arrays smaller), like a say, this was completely seperate functions, arrays, variables etc. I've checked and double checked that I havn't accidentally altered any code related to the above.

Now when I run the code I get the error:

Code:
Unhandled exception at 0x00414355 in Heightmap.exe: 0xC0000005: 
Access violation writing location 0x00000000.

After a little Googling it's related to a reading/writing from/to a NULL location.

Where has this error come from? I havn't changed anything!!!:confused:

Any ideas?
Thanks
 
Hmm, well after comparing it to an older version, I've found that the line:

Code:
distanceHoriz = (double*)malloc((count*heightSize) * sizeof(double));

behaves differently depending on the value of 'count' and 'heightSize'. In the older version the values are both 301 (of type integer). In my new version they are 37. After executing the above statement, in the old version the value in all the array locations was:
-6.2774385622041925e+066 double
However with my new version, after execution it reads:
CXX0030: Error: expression cannot be evaluated

Now if I put 301 into my newer version it works fine. Why isn't it working with smaller numbers??!!

Thanks.

**edit** - After a bit of fiddling it seems as tough the function doesn't work unless it's using numbers bigger than or equal to 255 :confused:
 
Last edited:
Hmm, well after comparing it to an older version, I've found that the line:

Code:
distanceHoriz = (double*)malloc((count*heightSize) * sizeof(double));

behaves differently depending on the value of 'count' and 'heightSize'. In the older version the values are both 301 (of type integer). In my new version they are 37. After executing the above statement, in the old version the value in all the array locations was:
-6.2774385622041925e+066 double
However with my new version, after execution it reads:
CXX0030: Error: expression cannot be evaluated

Now if I put 301 into my newer version it works fine. Why isn't it working with smaller numbers??!!

Thanks.

**edit** - After a bit of fiddling it seems as tough the function doesn't work unless it's using numbers bigger than or equal to 255 :confused:

K its been a while since I did any C,

Ok so in your new version your allocating less memory. Now because your arrays a different size I think your writing/indexing off the memory allocated.

CXX0030: Error: expression cannot be evaluated

What does this refer to? How you reading the value? You even sure that the big - number you got is actually a result and not just some dirty memory? (malloc does not 0 the memory allocated) Sounds like you have some undefined behaviour happening.
 
K its been a while since I did any C,

Ok so in your new version your allocating less memory. Now because your arrays a different size I think your writing/indexing off the memory allocated.

CXX0030: Error: expression cannot be evaluated

What does this refer to? How you reading the value? You even sure that the big - number you got is actually a result and not just some dirty memory? (malloc does not 0 the memory allocated) Sounds like you have some undefined behaviour happening.

Thanks for your reply Una.

I dont think I'm writing off the memory allocated - because I cant write anything at all!! I cant write to the first element, middle element, last element or anything.

As for the values, I'm reading the values from the debugger. I'm far from a pro but I'm pretty sure the "-6.2774385622041925e+066 double" is the default value for all array locations that have been allocated memory succesfully, the "CXX0030: Error: expression cannot be evaluated" means that something has gone wrong and the space han't been created properly. I'm just unsure why it hasn't been allocated when in essence its exactly the same code.:confused:
 
Have you checked the malloc call isnt actually returning null? (i.e can't allocate?). Don't think that's the prob.

With malloc, "malloc() allocates size bytes and returns a pointer to the
allocated memory. The memory is not cleared." So whatever was in that space before is left as it is. If that's random gunk maybe the debugger can't read it. Thus your "CXX0030: Error: expression cannot be evaluated"
 
Have you checked the malloc call isnt actually returning null? (i.e can't allocate?). Don't think that's the prob.

With malloc, "malloc() allocates size bytes and returns a pointer to the
allocated memory. The memory is not cleared." So whatever was in that space before is left as it is. If that's random gunk maybe the debugger can't read it. Thus your "CXX0030: Error: expression cannot be evaluated"

Turns out it is returning null (I really should put some error handling in my code :o). Why could this be? I could understand if I was making the array bigger and there wasn't enough space, but I'm making it smaller so there surely must be space:confused:. I've restarted Visual Studio and restarted my computer and the problem still occurs.

Thanks again.
 
check all values are initialised, also calculate the size you are allocation prior to calling malloc, and see how much memory you are asking for.

What do you mean by "check all values are initialised"? Do you mean the 'count' and 'heightSize' i'm using in the malloc call? If so then they are definately initialised, i've tried hard coding in numbers at that still doesn't work - unless the value of both numbers are above 255.
 
Weird, try print out what errno is after that malloc call it might give you something more to work with (<errno.h> on linux dunno about windows).
 
Weird, try print out what errno is after that malloc call it might give you something more to work with (<errno.h> on linux dunno about windows).
Error number is 998. Got from the following code:

Code:
double* test = (double*)malloc((count*heightSize) * sizeof(double));
	if (test == NULL)
		fprintf(stderr, "Error number %d.\n", GetLastError());

Havn't had chance to look yet, i'm off to lectures now. Will have a look when I get back.
 
I've ran the program using a larger file meaning the variables count and heightSize are larger. This worked fine. I then went back to the old file (with the smaller variables that wasn't working earlier) and now its all working fine and dandy.

Very confusing :confused:
 
Back
Top Bottom