irimiab
09-16-2003, 08:58 AM
Just today I have met a problem:
When I needed to allocate a large bunch of memory, I used the following code:
temp=(char*)MALLOC(pApp->dataBufferLength*sizeof(char));
I needed several instructions like this one, and dataBufferLength was over 5000. The problem appeared at the seventh or eigth instruction like the one above. The solution was to enlarge the memory of the device (I used the Windows heap). But I tested the code using
temp=(char*)MALLOC(pApp->dataBufferLength*sizeof(char*));
instead of the first instruction. Sizeof(char*) returns 4 and sizeof(char) returns (obviouselly) 1. But the code works ONLY with the second instruction. Why does this happen ? The memory allocated should grow four times. If it doesn't work with the first instruction, why does it work with the second one ? :confused:
The code looks like this:
temp=(char*)MALLOC(pApp->dataBufferLength*sizeof(char*));
STRCPY(temp,pApp->dataBuffer);
temp2=temp;
//here I have several instructions that alters the temp pointer (the string) -i'm doing a string parsing
data1=(char*)MALLOC(STRLEN(temp)*sizeof(char*));
STRCPY(data1, temp);
FREEIF(temp2);
temp=NULL;
Is this ok, or there's a problem in my logic ?
I tried with sizeof(char), but the problem with the memory emerges (at the seventh block like the one above it crashes - doesn allocate the memory, or it freezes)
Thank You !
When I needed to allocate a large bunch of memory, I used the following code:
temp=(char*)MALLOC(pApp->dataBufferLength*sizeof(char));
I needed several instructions like this one, and dataBufferLength was over 5000. The problem appeared at the seventh or eigth instruction like the one above. The solution was to enlarge the memory of the device (I used the Windows heap). But I tested the code using
temp=(char*)MALLOC(pApp->dataBufferLength*sizeof(char*));
instead of the first instruction. Sizeof(char*) returns 4 and sizeof(char) returns (obviouselly) 1. But the code works ONLY with the second instruction. Why does this happen ? The memory allocated should grow four times. If it doesn't work with the first instruction, why does it work with the second one ? :confused:
The code looks like this:
temp=(char*)MALLOC(pApp->dataBufferLength*sizeof(char*));
STRCPY(temp,pApp->dataBuffer);
temp2=temp;
//here I have several instructions that alters the temp pointer (the string) -i'm doing a string parsing
data1=(char*)MALLOC(STRLEN(temp)*sizeof(char*));
STRCPY(data1, temp);
FREEIF(temp2);
temp=NULL;
Is this ok, or there's a problem in my logic ?
I tried with sizeof(char), but the problem with the memory emerges (at the seventh block like the one above it crashes - doesn allocate the memory, or it freezes)
Thank You !