Re: [newbie] strcpy, strtok and strcat problem...



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...


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.

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" ???

--
une bévue
.



Relevant Pages

  • Re: Proper deletion question.
    ... When the function starts it is assumed that internalData holds a valid pointer to valid ... Unluckily the system hasn't needed the memory you ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Proper deletion question.
    ... is no arrow pointing to them, which means: you leaked memory. ... It does not copy the content of where temp points to to where internalData points to! ... The pointer internalData ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Giving an address value to a pointer
    ... void* temp = 0x80100000; ... used to initialize an entity of type "void *" ... How can i assign this memory address to a pointer in C? ...
    (comp.lang.c)
  • Re: realloc zero bytes?
    ... realloc with a size argument of 0. ... return a valid pointer to a zero-sized object.. ... "The realloc function returns a pointer to the new object (which may ... But if size is 0, there won't be any problems allocating he memory, so ...
    (comp.lang.c)
  • Giving an address value to a pointer
    ... void* temp = 0x80100000; ... used to initialize an entity of type "void *" ... How can i assign this memory address to a pointer in C? ...
    (comp.lang.c)