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



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



Relevant Pages

  • Re: Problem in writing structure to Binary file in C lang
    ... You allocate just enough memory ... to store a pointer, but rather likely not enough memory to store ... a string. ... pointer to a string then you write over memory yiu don't own. ...
    (comp.lang.c)
  • Re: A little ASM 6809 program
    ... CMPA MEM; is it smaller the the current min value ... yes, store new min value ... and the pointer to the byte in the string ...
    (alt.lang.asm)
  • Re: A little ASM 6809 program
    ... CMPA MEM; is it smaller the the current min value ... yes, store new min value ... and the pointer to the byte in the string ...
    (alt.lang.asm)
  • Re: "Mastering C Pointers"....
    ... A pointer is a kind of variable that can "point to" some object. ... has a type (pointer to int), and a value of some kind. ... You may know that you can access these integers by using array notation ... The function will take one argument, a string, and will return the length ...
    (comp.lang.c)
  • Re: pesky Pointers !!
    ... > and the function takes it as a reference instead of a copy. ... function may access the string passed directly, ... > *px dereferences the pointer to get the value ... If pTest is a pointer-to-string, *pTest is the string it points to ...
    (alt.comp.lang.learn.c-cpp)