Thread: problem with memory allocation
Hello all Sorry for asking so many question about this topic, but i don't get it to work. The problem is when i allocate memory for char pointers (char*). I have a function that i run many times in my db. The first time i allocate memory for the char* it is ok, and it is set to the length that i say it should have. In the next round, i give a smaller input and tries to allocate memory for this. The first problem is when i try to print out the pointer after the allocation, but before the insert it is what i inserted the last time i used the function. The second problem, coming from the first, is that it has the length from the first allocation, not the length that i set it to have. Here is some of the code i use: alignres *align = (alignres *) PG_GETARG_POINTER(0); char *first = NULL; char *second = NULL; int secondStart=align->secondString; char tempBuffer[strlen(align->stringBuffer)+1]; elog(NOTICE, "before allocating:first=%s, second=%s", first, second); first = (char*) palloc(sizeof(char)*secondStart); second = (char*) palloc(sizeof(char)*(strlen(align->stringBuffer) -secondStart+1)); elog(NOTICE, "after allocating:first=%s, second=%s", first, second); elog(NOTICE, "length: first=%d, second=%d", strlen(first), strlen(second)); snprintf(tempBuffer, sizeof(tempBuffer), "%s", align->stringBuffer); snprintf(first, secondStart, "%s", tempBuffer); first[strlen(first)] = '\0'; int j=0; for(i=secondStart-1; i < (strlen(align->stringBuffer)); i++) { second[j] = tempBuffer[i]; j++; } second[strlen(second)] = '\0'; pfree(first); pfree(second); I am i doing the allocation correct? Is there any other way to free the memory so first and second is not set to the value that the memory was last used for? thanks -Kjetil
On Fri, 26 Nov 2004, Kjetil Haaland wrote: > Hello all > Sorry for asking so many question about this topic, but i don't get it to > work. The problem is when i allocate memory for char pointers (char*). I have > a function that i run many times in my db. The first time i allocate memory > for the char* it is ok, and it is set to the length that i say it should > have. In the next round, i give a smaller input and tries to allocate memory > for this. > > The first problem is when i try to print out the pointer after the allocation, > but before the insert it is what i inserted the last time i used the > function. > The second problem, coming from the first, is that it has the length from the > first allocation, not the length that i set it to have. That's because AFAICS you're misusing the output of palloc as a string when you haven't put a string into it. After allocation, it seems you have two pointers, one which has secondStart bytes allocated, the other which has strlen(align->stringBuffer)-secondStart+1 bytes allocated. The values of the bytes inside those allocated bytes are technically indeterminate, so using %s or strlen on them is invalid. More fundamentally, it feels like you're trying to use strlen() to get the size of the allocated space [in statements like first[strlen(first)]='\0'] which is not what strlen does. Strlen generally moves through memory starting at the pointer looking for a '\0' character and if one does not exist within the size you've allocated, the behavior is undefined. first[strlen(first)]='\0' is pretty much a no op, since either it found a \0 character within the buffer (in which case that should already be a \0 or went off the end of the buffer (in which case you can't rely on strlen returning something meaningful, and even if it did, it's not in the object, so you shouldn't be writing to it anyway).
On Friday 26 November 2004 17:52, Stephan Szabo wrote: > That's because AFAICS you're misusing the output of palloc as a string > when you haven't put a string into it. After allocation, it seems you > have two pointers, one which has secondStart bytes allocated, the other > which has strlen(align->stringBuffer)-secondStart+1 bytes allocated. The > values of the bytes inside those allocated bytes are technically > indeterminate, so using %s or strlen on them is invalid. > > More fundamentally, it feels like you're trying to use strlen() to get the > size of the allocated space [in statements like first[strlen(first)]='\0'] > which is not what strlen does. Strlen generally moves through memory > starting at the pointer looking for a '\0' character and if one does not > exist within the size you've allocated, the behavior is undefined. > first[strlen(first)]='\0' is pretty much a no op, since either it found a > \0 character within the buffer (in which case that should already be a > \0 or went off the end of the buffer (in which case you can't rely on > strlen returning something meaningful, and even if it did, it's not in the > object, so you shouldn't be writing to it anyway). Hello again Thanks a lot for a great answer! The problem was as you said my misuse of strlen and the insertion of '\0'. I used the numbers that i used when i allocated the space to insert the '\0' and now it works! - Kjetil