[Newbie] changing allocated memory

  • Thread starter =?ISO-8859-1?Q?Une_b=E9vue?=
  • Start date
?

=?ISO-8859-1?Q?Une_b=E9vue?=

the only arg to a class is a string :

VALUE target_path

internally i redefine this input var sometime, not always.

if it's redefine i get something like that :

/Users/yvon/Desktop/raliasrecord-0.0.1 <= input
/Users/yvon/work/Ruby/Native/raliasrec <= output

where the output should be :
/Users/yvon/work/Ruby/Native/raliasrecord-0.0.1

this string being passed by a pointer, how do i reallocate memory for it
?

using another internal var ?

is their a cleaner/clever way ?
 
S

Spiros Bousbouras

Une said:
the only arg to a class is a string :

VALUE target_path

internally i redefine this input var sometime, not always.

if it's redefine i get something like that :

/Users/yvon/Desktop/raliasrecord-0.0.1 <= input
/Users/yvon/work/Ruby/Native/raliasrec <= output

where the output should be :
/Users/yvon/work/Ruby/Native/raliasrecord-0.0.1

this string being passed by a pointer, how do i reallocate memory for it
?

using another internal var ?

is their a cleaner/clever way ?

Assume that you didn't know anything about your
programme (kind of like the people here from whom
you ask help don't know anything about your programme)
and you read what you wrote above , do you think you
would have any idea whatsoever about what the problem
is or what the programme tries to achieve or whether you
even have the correct newsgroup ? Because I as sure as
hell don't.
 
E

Ed Prochak

Une said:
the only arg to a class is a string :

VALUE target_path

internally i redefine this input var sometime, not always.

if it's redefine i get something like that :

/Users/yvon/Desktop/raliasrecord-0.0.1 <= input
/Users/yvon/work/Ruby/Native/raliasrec <= output

where the output should be :
/Users/yvon/work/Ruby/Native/raliasrecord-0.0.1

this string being passed by a pointer, how do i reallocate memory for it
?

using another internal var ?

is their a cleaner/clever way ?

Please post C++ questions to the C++ newgroup.
 
W

Walter Roberson

the only arg to a class is a string :

Are you sure that you have the right newsgroup? The C language does
not have a 'class'.

VALUE target_path
internally i redefine this input var sometime, not always.
if it's redefine i get something like that :
/Users/yvon/Desktop/raliasrecord-0.0.1 <= input
/Users/yvon/work/Ruby/Native/raliasrec <= output
where the output should be :
/Users/yvon/work/Ruby/Native/raliasrecord-0.0.1
this string being passed by a pointer, how do i reallocate memory for it

If what is being passed in is a literal string, and you want to
change it "in place", then you cannot portably do that in C, as C
explicitly permits literal strings to be stored in read-only memory.
You could, however, construct a new string and return a pointer to it.

If what is being passed in is the pointer to an array of char
that is a local or global variable, then there is no way for you
to extend it "in place": the best you can do in such a case is
return a pointer to the new string.

If what is being passed in is a pointer to a malloc()'d block of
memory, then you can use realloc() to get a bigger storage area.
However, when you use realloc(), if it does not happen to have
enough room to extend the memory where it happens to sit, then
realloc() will find another chunk of memory somewhere that is
big enough for the new size, and will copy the contents of the
current chunk over there, and will destroy the old chunk of
memory and will return a pointer to the new location. If
realloc() just -happened- to be able to fit the bigger string
"in place" then you are okay, but if realloc() had to move the chunk
then you have a problem, in that the calling routine knows only
the old address for the chunk before it was moved -- and there is
no mechanism in C to alter a passed-in pointer and have the change
noticed in the levels above. Thus again you need to return a pointer
to the new string.


Alternately, if what gets passed to you is not the pointer to the
string, but is instead a pointer *to* the pointer to the string,
then you can return the new pointer value through that.

For example,

#include <stdio.h>
void newstring( char **oldstringptr ) {
*oldstringptr = "Bye!";
}
int main(void) {
char *avariable = "Hello!";
puts(avariable);
newstring(&avariable);
puts(avariable);
return 0;
}
 
?

=?ISO-8859-1?Q?Une_b=E9vue?=

Walter Roberson said:
If what is being passed in is a literal string, and you want to
change it "in place", then you cannot portably do that in C, as C
explicitly permits literal strings to be stored in read-only memory.
You could, however, construct a new string and return a pointer to it.

ok, many thanks, i'll do that because i've a Boolean saying the
target_path is the same or not.
[...]

Alternately, if what gets passed to you is not the pointer to the
string, but is instead a pointer *to* the pointer to the string,
then you can return the new pointer value through that.

i known this is a pointer, not a pointer to another pointer.
 
?

=?ISO-8859-1?Q?Une_b=E9vue?=

Spiros Bousbouras said:
you think you
would have any idea whatsoever about what the problem
is or what the programme tries to achieve or whether you
even have the correct newsgroup ?

YES definitely because i've assumed, in the subject it's a basic memory
allocation prob ;-)
 
K

Keith Thompson

the only arg to a class is a string :

VALUE target_path

internally i redefine this input var sometime, not always.

if it's redefine i get something like that :

/Users/yvon/Desktop/raliasrecord-0.0.1 <= input
/Users/yvon/work/Ruby/Native/raliasrec <= output

where the output should be :
/Users/yvon/work/Ruby/Native/raliasrecord-0.0.1

this string being passed by a pointer, how do i reallocate memory for it
?

using another internal var ?

is their a cleaner/clever way ?

Show us some actual code, and we might be able to help you.

(If you want to expand an allocated block of memory, look at the
realloc() -- but be aware that it will only work if the original block
was allocated by malloc(), calloc(), or realloc().)
 
?

=?ISO-8859-1?Q?Une_b=E9vue?=

Keith Thompson said:
Show us some actual code, and we might be able to help you.

here it is :

VALUE m_raliasrecord_init(VALUE self, VALUE target_path)
--------------------------------------------^^^^^^^^^^^^---> input
{
[...]
CFStringRef path = CFStringCreateWithCString(kCFAllocatorDefault,
StringValuePtr(target_path), kCFStringEncodingUTF8);
---------------^^^^^^^^^^^^---------------------------------> first use
[...]
if (resolvedUrl != NULL) {
CFStringRef theResolvedAliasPath =
CFURLCopyFileSystemPath(resolvedUrl, kCFURLPOSIXPathStyle);
Boolean success = CFStringGetCString(theResolvedAliasPath,
StringValuePtr(target_path), 256, kCFStringEncodingUTF8);
---------------^^^^^^^^^^^----------------------------------> output
[...]
return self;
}

notice :
In C, variables have types and data do not have types. In contrast, Ruby
variables do not have a static type, and data themselves have types, so
data will need to be converted between the languages.

Data in Ruby are represented by C type `VALUE'. Each VALUE data has its
data-type.

this small piece of C will be an extension to Ruby, it makes use of :
#include <CoreFoundation/CoreFoundation.h>
#include <Carbon/Carbon.h>
#include <CFURL.h>
#include <ruby.h>

specific to MacOS X except ruby.h

remember the prog compiles and runs but the output of "target_path" as
the same length that the input, not the same value...
 
?

=?ISO-8859-1?Q?Une_b=E9vue?=

CBFalconer said:
There are no classes in the C language. comp.lang.c++ is down the
hall.

i forgot to mention this c writes a class extension to Ruby, it is a
class for ruby.
 
C

CBFalconer

Une said:
i forgot to mention this c writes a class extension to Ruby, it is a
class for ruby.

There is no Ruby in the C language, unless you define it yourself.
You didn't.
 
E

Ed Prochak

Une said:
this C not C++...

Sorry, your mention of "class" and the short code snippits in the
original post mislead me to think you were doing C++. My apologies.

Bon jour,
Ed
 
C

CBFalconer

Ed said:
Sorry, your mention of "class" and the short code snippits in the
original post mislead me to think you were doing C++. My apologies.

Well, they certainly were not C, so they are off-topic here. No
apologies needed.
 
?

=?ISO-8859-1?Q?Une_b=E9vue?=

CBFalconer said:
There is no Ruby in the C language, unless you define it yourself.

right, it's ruby written in C, the reason for me to write a C extension
to Ruby, called, from ruby as a class :

o=MyClass.new()
o.do_something...
 
?

=?ISO-8859-1?Q?Une_b=E9vue?=

CBFalconer said:
Well, they certainly were not C, so they are off-topic here.

definitely it is C, somehow special, i agree, but it is a C extension to
Ruby, itself written in C, having a ruby.h, and this C code produces a
class in the ruby usage i didn't show.
 
C

CBFalconer

Une said:
right, it's ruby written in C, the reason for me to write a C
extension to Ruby, called, from ruby as a class :

o=MyClass.new()
o.do_something...

Then, to discuss it here, you need to publish its code in the
article. That should not exceed roughly 200 lines. You may need
to do considerable work to cut it down to that size and still
exhibit whatever problems you are having.
 
?

=?ISO-8859-1?Q?Une_b=E9vue?=

CBFalconer said:
Then, to discuss it here, you need to publish its code in the
article. That should not exceed roughly 200 lines. You may need
to do considerable work to cut it down to that size and still
exhibit whatever problems you are having.

ok, i'll do that next time, i solved my prob using strcpy...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top