Skip site navigation (1) Skip section navigation (2)

Re: problem with memory allocation

From: Kjetil Haaland <kjetil(dot)haaland(at)student(dot)uib(dot)no>
To: Stephan Szabo <sszabo(at)megazone(dot)bigpanda(dot)com>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: problem with memory allocation
Date: 2004-11-28 13:33:13
Message-ID: (view raw, whole thread or download thread mbox)
Lists: pgsql-novice
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

In response to

pgsql-novice by date

Next:From: Jeff FlowersDate: 2004-11-28 16:20:13
Subject: Re: how to unsubscribe
Previous:From: malini buDate: 2004-11-28 13:06:38
Subject: how to unsubscribe

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group