Pointers, return values, memory usage. 
Author Message
 Pointers, return values, memory usage.

Hi,

I've got a question about passing and returning pointers. Sometimes
you see that strings (for example char*) returned by functions are
copied into a local array. Why is that? Is it only neccessary when you
don't know the implementation of the function (don't know whether the
array returned by the function is static or not), or is it always
advisable?

Suppose I have the following:

char* some_function();

... and I know that the pointer returned by this function points to
space allocated by "new", and that this space won't be deleted until
the application terminates. Can I use this pointer safely? Or may the
space this pointer points to be re-allocated by some memory-manager
(or whatever)?

Couldn't find it in the FAQ.

Thanks in advance,
Marcel



Mon, 13 Sep 2004 21:19:26 GMT  
 Pointers, return values, memory usage.

Quote:

<snip>

> Suppose I have the following:

> char* some_function();

> ... and I know that the pointer returned by this function points to
> space allocated by "new",

malloc

Quote:
> and that this space won't be deleted

freed

Quote:
> until
> the application terminates. Can I use this pointer safely?

Sure.

Quote:
> Or may the
> space this pointer points to be re-allocated by some memory-manager
> (or whatever)?

No. If you know it's been allocated, and as long as you don't exceed its
bounds, it's okay to write into that space, and indeed read from it (if
you're sure it has been initialised with legal values).

Your hints about new and delete lead me to think you may be using C++,
in which case you might care to check whether the answer I gave also
applies to C++, which you can do by asking in comp.lang.c++

--

"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton



Mon, 13 Sep 2004 21:33:08 GMT  
 Pointers, return values, memory usage.

Quote:

> I've got a question about passing and returning pointers. Sometimes
> you see that strings (for example char*) returned by functions are
> copied into a local array. Why is that? Is it only neccessary when you
> don't know the implementation of the function (don't know whether the
> array returned by the function is static or not), or is it always
> advisable?

If you are going to alter the string for internal (to the
function) use, and don't know where it came from, then this is
advisable.  It allows the function to be passed non-alterable
strings, such as:

    dofunction("non-alterable string");

where the string may well be stored in read-only memory.  However
if the function prototype is:

    void dofunction(const char *s);

then you know the function won't alter the string, and no copying
is needed.  If you violate this constraint in writing the function
code any decent compiler should holler at you for it.

--

   Available for consulting/temporary embedded and systems.
   (Remove "XXXX" from reply address. yahoo works unmodified)



Tue, 14 Sep 2004 02:32:10 GMT  
 Pointers, return values, memory usage.
I thank you both for your replies. I was indeed talking about C++, so
this is actually the wrong group. :)

But what I was aiming at, was use of the pointers themselves. If a
memory-manager would re-allocate the memory for whatever reason (maybe
because a new piece of memory needs to be allocated, but doesn't fit,
so the memory-manager reorganises the memory), are the pointers
updated to point to the new address? Or will that never happen (during
the lifetime of the pointer, will it always point to the same
address)?

I need to know this, because I am passing pointers all over the place
:) and I need to be sure that those pointers are valid (knowing that
the memory has not been freed yet).

Thanks,
Marcel



Tue, 14 Sep 2004 16:09:41 GMT  
 Pointers, return values, memory usage.

Quote:

> But what I was aiming at, was use of the pointers themselves. If a
> memory-manager would re-allocate the memory for whatever reason (maybe
> because a new piece of memory needs to be allocated, but doesn't fit,
> so the memory-manager reorganises the memory), are the pointers
> updated to point to the new address? Or will that never happen (during
> the lifetime of the pointer, will it always point to the same
> address)?

The memory manager won't move around memory except at your
request by way of realloc().  If realloc() moves a memory block,
then it is your responsibility to update all pointers to point to
the new location.
--
"Debugging is twice as hard as writing the code in the first place.
 Therefore, if you write the code as cleverly as possible, you are,
 by definition, not smart enough to debug it."
--Brian Kernighan


Wed, 15 Sep 2004 00:53:05 GMT  
 Pointers, return values, memory usage.
Thanks.. That was exactly was I wanted to know.. :)

Marcel



Thu, 16 Sep 2004 00:31:50 GMT  
 Pointers, return values, memory usage.

Quote:

> Thanks.. That was exactly was I wanted to know.. :)

What was?  Somehow the quoted portion didn't make it across the
net.

--

   Available for consulting/temporary embedded and systems.
   (Remove "XXXX" from reply address. yahoo works unmodified)



Thu, 16 Sep 2004 01:55:56 GMT  
 Pointers, return values, memory usage.

Quote:


> > Thanks.. That was exactly was I wanted to know.. :)

> What was?  Somehow the quoted portion didn't make it across the
> net.

Euhm.. Read the entire thread maybe? :)


Fri, 17 Sep 2004 07:25:18 GMT  
 Pointers, return values, memory usage.

Quote:


>> > Thanks.. That was exactly was I wanted to know.. :)

>> What was?  Somehow the quoted portion didn't make it across the
>> net.
> Euhm.. Read the entire thread maybe? :)

First of all, the entire thread might not have been transmitted across
all news servers, so not everyone CAN read the entire thread.
Second of all, having to jump to previous messages to know the context,
and then back to the current message to know the new information, is a
bit tedious. Quoting makes it so much easier, since you can read one
message at a time.

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/
"A bicycle cannot stand up by itself because it's two-tyred."
   - Sky Text



Fri, 17 Sep 2004 15:30:42 GMT  
 Pointers, return values, memory usage.

Quote:

> I thank you both for your replies. I was indeed talking about C++, so
> this is actually the wrong group. :)

> But what I was aiming at, was use of the pointers themselves. If a
> memory-manager would re-allocate the memory for whatever reason (maybe
> because a new piece of memory needs to be allocated, but doesn't fit,
> so the memory-manager reorganises the memory), are the pointers
> updated to point to the new address? Or will that never happen (during
> the lifetime of the pointer, will it always point to the same
> address)?

OK lets say on my implementation I have this pointer

{
 int* pt = malloc( sizeof(int) * 5 );
 int* pt1 = pt;
 ////////
 getchar();
 if ( pt == pt1 )
       printf("This is unreal");

Quote:
}

Then, as usual I go to sleep[before getchar]and I have several task
configured in my computer and they run. If there is a configuration of
address then how is the above supposed to work. But, I won't rule that
either, sounds stupid but I think this is allowed, but the fact would
be that all the arithmetic on pointers which would be addition,
substartion of pointers needs to be defined like if I did

int* pt = (int*) 0x0055;

then

if ( (int*) pt == (int*)0x0055 )
/*
  DOUBT:::: is int* correct or should I do void *
/*
   printf("Really");

This has to print "Really".

[OT]
Indeed this is what happens[IMO] because it would generally be the
processor which would have to swap the memory that would not be needed
immediately, the new *actual* addressing might be different, but the
relative addressing would remain the same.
[/OT]

<snip>



Fri, 17 Sep 2004 19:51:28 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. Class object, returning pointers, dynamic memory, memory leaks

2. Thanks: pointer to array - memory usage

3. pointer to array - memory usage

4. Thanks: pointer to array - memory usage

5. Help returning pointer value from function

6. Function pointer as a return value of a function

7. function pointers as return values

8. Pointer to function returning pointer to function returning...

9. Return Memory Pointer from Capture Window

10. Free memory, memory usage

11. pointer-to-pointer usage

12. Pointers: return of pointer to array of pointers to main

 

 
Powered by phpBB® Forum Software