Re: [newbie] strcpy, strtok and strcat problem...
- From: Michael Mair <Michael.Mair@xxxxxxxxxxxxxxx>
- Date: Thu, 31 Aug 2006 21:46:59 +0200
Une bévue schrieb:
Michael Mair <Michael.Mair@xxxxxxxxxxxxxxx> wrote:
You have uninitialised pointers, which may contain a random
valid address or a null pointer or something not a valid pointer
representation at all.
right ! i've found that in between...
Um, "in between" what?
Do you mean that you have found out this in the meantime?
I am not giving the language teacher (at least not for
English), I just do not understand what you mean.
target_path_cpy = strcpy(target_path_cpy,
StringValuePtr(target_path));
target_path_cpy2 = strcpy(target_path_cpy2,
StringValuePtr(target_path));
Now you try to copy something to a storage location you
_cannot_ own.
There are non-Standard-C extensions like strdup() which
duplicate a string and provide the necessary memory; these
can be used instead of the above.
Or you can get the memory yourself previous to the call to
strcpy():
char *target_path_cpy, *target_path_cpy2;
char *temp = StringValuePtr(target_path);
size_t size = strlen(temp) + 1;
i've put a constant size 512 for a full path and 256 for a relative one,
even if target_path_cpy does have the same length, at start, than
target_path it might be longer (or shorter afterwards.
Note: Fixed path lengths probably _will_ give you trouble at some
point in the future as there is no "large enough" buffer to hold
all possible paths.
If you want some room for change, you can just use
size_t size = INITIAL_FULL_PATH_ADJUSTMENT + strlen(temp) + 1;
and realloc() if necessary.
target_path_cpy = malloc(size);
target_path_cpy2 = malloc(size);
if (NULL == target_path_cpy
|| NULL == target_path_cpy2) {
free(target_path_cpy);
free(target_path_cpy2);
return INVALID_VALUE; /* or whatever you do to indicate failure */
}
strcpy(target_path_cpy, temp);
strcpy(target_path_cpy2, temp);
There you go...
And at the end of m_raliasrecord_init(), you call
free(target_path_cpy);
free(target_path_cpy2);
before returning the VALUE return value.
yes, thanks a lot, however i've a question about freeing memory the
skeleton of my prog being :
VALUE m_raliasrecord_init(VALUE self, VALUE from_path, VALUE
target_path)
{
computes all the internal values
rb_iv_set(self, "@from_path", from_path);
//<the same for all the values>
return self;
}
BUT the values as from_path are used elsewhere like that :
VALUE m_from_path(VALUE self) {
return rb_iv_get(self, "@from_path");
}
(rb_iv_set/get are specific to ruby.h)
part of ruby.h :
VALUE rb_iv_get(VALUE, const char*);
VALUE rb_iv_set(VALUE, const char*, VALUE);
am i sure that, after deallocating memory for from_path in
"m_raliasrecord_init" , for example, i'll get the correct value in
"m_from_path" ???
You did not give enough information.
Does rb_iv_get() create a copy of the passed source VALUE's
respective property?
Or does it create a copy of some pointer within the source VALUE
and store this pointer in the destination VALUE?
The same for the strings:
Will it store a copy of the string?
Find some property by using the string?
Store a copy of the string's address?
All of this should be told to you by the documentation.
If not pointers are stored, then you can and should free() whatever
you allocated in m_raliasrecord_init(). If only pointers are stored,
then you must not free() these pointers in m_raliasrecord_init()
but should free a VALUE's property pointers when deleting the last
VALUE storing them.
Hopefully, the former is the case.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
.
- References:
- [newbie] strcpy, strtok and strcat problem...
- From: Une bévue
- Re: [newbie] strcpy, strtok and strcat problem...
- From: Michael Mair
- Re: [newbie] strcpy, strtok and strcat problem...
- From: Une bévue
- [newbie] strcpy, strtok and strcat problem...
- Prev by Date: Re: Convert 00010000 to 11110000......how?
- Next by Date: Re: is the C library a wrapper for windows API?
- Previous by thread: Re: [newbie] strcpy, strtok and strcat problem...
- Next by thread: Convert 00010000 to 11110000......how?
- Index(es):
Relevant Pages
|