0

Below I have a method that constructs a permutated string of a given string (str). I don't really know why but sometimes while debugging I receive the following exception:

Unhandled exception at 0x01282665 in test.exe: 0xC0000005: Access violation 
writing     location 0x00000000.

when trying to assign ('u') at index 0 in ret_str (ret_str[l]=elem[0])

unsigned char* getPermStr(long length,unsigned char* strt,unsigned char* elem){
    unsigned char* ret_str;
    long l = 0;
    ret_str = (unsigned char*) calloc(length,sizeof(unsigned char));
    while(l < length){
        if(elem < (strt+length-1)){
            ret_str[l]=elem[0];  // ACCESS VIOLATION HERE
            elem+=1;
        }else{
            ret_str[l]=elem[0];
            elem = strt;
        }
        l+=1; 
    }
    return ret_str;
}

I don't see why the access violation occurs... I'm within the bounds of my ret_str so what is wrong? BTW: The string ret_str is free'd after the function call.

UPDATE: There was no problem with elem. The reason was that I allocated memory while there was no memory left on the heap for dynamic allocation (due of lots of memory leaks) so calloc returned a NULL pointer. That's why the error occured.

5
  • If I were you, I'd check if ret_str and elem are not NULL ;) Commented Oct 19, 2012 at 12:07
  • It's telling you that ret_str is null. Think about why that might be. Commented Oct 19, 2012 at 12:07
  • So that means that calloc didn't allocate memory. Elem is not NULL in this case... Commented Oct 19, 2012 at 12:11
  • The root cuase for the error most probably lies in the memory referred by elem. Commented Oct 19, 2012 at 16:57
  • There was no problem with elem. The reason was that I allocated memory while there was no memory left on the heap for dynamic allocation (due of lots of memory leaks) so calloc returned a NULL pointer. That's why the error occured. Commented Oct 20, 2012 at 15:26

2 Answers 2

1

You need to check whether elem is null. If it is null your function should return an error code.

Sign up to request clarification or add additional context in comments.

2 Comments

If not NULL elem most certainly points to memory not belonging to the process.
There was no problem with elem. The reason was that I allocated memory while there was no memory left on the heap for dynamic allocation (due of lots of memory leaks) so calloc returned a NULL pointer. That's why the error occured.
0

ret_str = (unsigned char*) calloc(length,sizeof(unsigned char)); Change this line to

ret_str = malloc(length * sizeof(unsigned char));
if(ret_str == NULL){ return "" ;}
//--whatever
while(l < length){
        if(elem < (strt+length-1)){
            ret_str[l]=elem[0];  // ACCESS VIOLATION HERE
            elem+=1;
        }else{
            ret_str[l]=elem[0];
            elem = strt;
        }
        l+=1; 
    }

Also make sure, elem is accessible. Chances are, elem isn't initialised.

6 Comments

It's giving the error on write, so it's gotta be ret_str not elem.
In C, it is preferable not to use explicit typecasting when using malloc/calloc.
no its not preferable nor necessary. Also there are certain problems using "malloc"
@askmish stackoverflow.com/questions/4993327/… read this to know why its not preferred to typecast malloc.
I wanted to say that there's no problem in calloc or malloc in OP's code.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.