memory block containing pointers, aka pointer to pointer i believe 
Author Message
 memory block containing pointers, aka pointer to pointer i believe

Hello,

My question is regards allocating a block of memory to store a list of
pointers to characters.  For instance, instead of allocating memory to
store,
say, 5 integers (int *memory = (int *)malloc(sizeof(int)*5);) I would like
the
block of memory to store, say, 5 pointers to characters.

This is what I've tried:

char *memory = (char *)malloc(sizeof(char *)*5);

And then to extract the 2nd pointer I would do something like this:

char *ptr;
memcpy(ptr, memory+sizeof(char *), sizeof(char *));

This doesn't work, obviously.  I've tried a number of different things, but
nothing works.  Anyone know what I should be doing??  Thanks a lot,

Brian



Wed, 05 May 2004 05:00:43 GMT  
 memory block containing pointers, aka pointer to pointer i believe


Quote:
> My question is regards allocating a block of memory to store a list of
> pointers to characters.  For instance, instead of allocating memory to
> store,
> say, 5 integers (int *memory = (int *)malloc(sizeof(int)*5);) I would

or simply:

int *memory = malloc(sizeof *memory * 5);

be sure you are using a C compiler and including <stdlib.h>

Quote:
> like the
> block of memory to store, say, 5 pointers to characters.
> This is what I've tried:

> char *memory = (char *)malloc(sizeof(char *)*5);

or

char *memory = malloc(sizeof *memory * 5);

as you can see, we have a minimum change here.

Quote:
> And then to extract the 2nd pointer I would do something like this:

Note that the pointers are not initialized.

Quote:
> char *ptr;
> memcpy(ptr, memory+sizeof(char *), sizeof(char *));

You are not aware of pointer arithmetics. The second location is at memory
+ 1 whatever the type.

   char *ptr;
   memcpy(ptr, memory + 1, sizeof(char *));
or
   memcpy(ptr, memory + 1, sizeof *ptr);

More simply, you can write

   char *ptr = *(memory + 1);

which is commonly noted

   char *ptr = memory[1];

The latter would be my choice. Don't forget that the pointers are still
unitialized. Initiazing a pointer with the value of an uninitialized
pointer is just a non-sense. Your compiler should complain about it.

--
-ed- emdel at noos.fr
c.l.c.-FAQ http://www.eskimo.com/~scs/C-faq/top.html
C-library: http://www.dinkumware.com/htm_cl/index.html
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/



Wed, 05 May 2004 05:25:28 GMT  
 memory block containing pointers, aka pointer to pointer i believe

Quote:

> Hello,

> My question is regards allocating a block of memory to store a list of
> pointers to characters.  For instance, instead of allocating memory to
> store,
> say, 5 integers (int *memory = (int *)malloc(sizeof(int)*5);) I would
> like the
> block of memory to store, say, 5 pointers to characters.

> This is what I've tried:

> char *memory = (char *)malloc(sizeof(char *)*5);

> And then to extract the 2nd pointer I would do something like this:

> char *ptr;
> memcpy(ptr, memory+sizeof(char *), sizeof(char *));

> This doesn't work, obviously.  I've tried a number of different things,
> but nothing works.  Anyone know what I should be doing??  Thanks a lot,

> Brian

If I'm reading this right, what you want isn't a pointer-to-char, but a
set of pointers-to-char, correct?  Okay, so try something like this:

/* NULL tests, etc, elided for brevity */
char **ptrlist = malloc( N * sizeof( char *) );

for ( i = 0; i < N; i++ )
   ptrlist[i] = malloc( size of desired char buffer )

now you can, say, strcpy( ptrlist[5], "Hello" ); assuming ptrlist[5] was
allocated to be large enough, etc.



Wed, 05 May 2004 05:27:24 GMT  
 memory block containing pointers, aka pointer to pointer i believe
typedef char          *CharPtr;
typedef unsigned long ULong;

const ULong kNElements = 5;

CharPtr *memory = NULL;

memory = malloc( sizeof( CharPtr ) * kNElements );

memory can be used as an array of 5 character pointers.

is this what you are looking for?

--
== Eric Gorr == http://www.*-*-*.com/ :9293199 ===
"Therefore the considerations of the intelligent always include both
benefit and harm." - Sun Tzu
== Insults, like {*filter*}, are the last refuge of the incompetent... ===



Wed, 05 May 2004 05:33:18 GMT  
 memory block containing pointers, aka pointer to pointer i believe
Have a look at the C FAQ.
6.16 and related questions may prove helpful
--
C-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
 "The C-FAQ Book" ISBN 0-201-84519-9
C.A.P. FAQ: ftp://cap.connx.com/pub/Chess%20Analysis%20Project%20FAQ.htm


Wed, 05 May 2004 05:34:49 GMT  
 memory block containing pointers, aka pointer to pointer i believe

Quote:



> > My question is regards allocating a block of memory to store a list of
> > pointers to characters.  For instance, instead of allocating memory to
> > store,
> > say, 5 integers (int *memory = (int *)malloc(sizeof(int)*5);) I would

> or simply:

> int *memory = malloc(sizeof *memory * 5);

> be sure you are using a C compiler and including <stdlib.h>

> > like the
> > block of memory to store, say, 5 pointers to characters.
> > This is what I've tried:

> > char *memory = (char *)malloc(sizeof(char *)*5);

> or

> char *memory = malloc(sizeof *memory * 5);

This gives a block of five characters, pointed to by memory. Not the
list of character POINTERS that was desired.

Quote:
> > And then to extract the 2nd pointer I would do something like this:

> Note that the pointers are not initialized.

Which pointers are those?

- Show quoted text -

Quote:
> > char *ptr;
> > memcpy(ptr, memory+sizeof(char *), sizeof(char *));

> You are not aware of pointer arithmetics. The second location is at memory
> + 1 whatever the type.
>    char *ptr;
>    memcpy(ptr, memory + 1, sizeof(char *));
> or
>    memcpy(ptr, memory + 1, sizeof *ptr);

> More simply, you can write

>    char *ptr = *(memory + 1);

> which is commonly noted

>    char *ptr = memory[1];

You just assigned a character to character pointer. Not good.

What the OP needs is this (assuming his problem description was
correct):

char **memory;
int i;
char *ptr;

memory = malloc (sizeof *memory * 5); /* now there are 5 pointers to
char */
/* error checking ignored, don't you forget it */

for (i = 0; i < 5; i++)
{
   memory[i] = malloc (80);  /* pull a string size out of my hat */

Quote:
}

ptr = malloc[1];

For a "ragged array" of differing string lengths, you don't use a fixed
memory allocation. Say if you were reading strings from a file, and
wanted exact lengths.

Brian Rodenborn



Wed, 05 May 2004 06:26:36 GMT  
 memory block containing pointers, aka pointer to pointer i believe

Quote:

> Hello,

> My question is regards allocating a block of memory to store a list of
> pointers to characters.  

This same message was also posted to comp.lang.c++. First of all, figure
out where the message belongs. You're going to get different answers
here than there (I'll bet they tell you to drop malloc and go with
new[]);

If you decide it needs to be in multiple groups, crosspost, don't
multipost. That way, the two groups know what's going on.

Brian Rodenborn



Wed, 05 May 2004 06:18:09 GMT  
 memory block containing pointers, aka pointer to pointer i believe


Quote:
>> char *memory = malloc(sizeof *memory * 5);

> This gives a block of five characters, pointed to by memory. Not the
> list of character POINTERS that was desired.

You are correct. I misread the question.

--
-ed- emdel at noos.fr
c.l.c.-FAQ http://www.eskimo.com/~scs/C-faq/top.html
C-library: http://www.dinkumware.com/htm_cl/index.html
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/



Wed, 05 May 2004 12:40:04 GMT  
 memory block containing pointers, aka pointer to pointer i believe


Forget that post, I misread the question. Sorry about that.

--
-ed- emdel at noos.fr
c.l.c.-FAQ http://www.eskimo.com/~scs/C-faq/top.html
C-library: http://www.dinkumware.com/htm_cl/index.html
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/



Wed, 05 May 2004 12:38:59 GMT  
 memory block containing pointers, aka pointer to pointer i believe


Quote:
>What the OP needs is this (assuming his problem description was
>correct):

>char **memory;
>int i;
>char *ptr;

>memory = malloc (sizeof *memory * 5); /* now there are 5 pointers to
>char */
>/* error checking ignored, don't you forget it */

>for (i = 0; i < 5; i++)
>{
>   memory[i] = malloc (80);  /* pull a string size out of my hat */
>}

>ptr = malloc[1];

       ^^^^^^

ITYM `ptr = memory[1]'.

dave

--

[T]he author of this code makes no warrenty as to its effectiveness.
Is his paranoia justified?
                                       --Chris Dollin in comp.lang.c



Wed, 05 May 2004 17:24:56 GMT  
 memory block containing pointers, aka pointer to pointer i believe


Quote:
>>for (i = 0; i < 5; i++)
>>{
>>   memory[i] = malloc (80);  /* pull a string size out of my hat */ }

>>ptr = malloc[1];
>        ^^^^^^

> ITYM `ptr = memory[1]'.

Geez!

--
-ed- emdel at noos.fr
c.l.c.-FAQ http://www.eskimo.com/~scs/C-faq/top.html
C-library: http://www.dinkumware.com/htm_cl/index.html
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/



Wed, 05 May 2004 18:23:44 GMT  
 memory block containing pointers, aka pointer to pointer i believe

Quote:

> This same message was also posted to comp.lang.c++. First of all, figure
> out where the message belongs. You're going to get different answers
> here than there (I'll bet they tell you to drop malloc and go with
> new[]);

Perhaps, but any answer which pertains to C++ is off-topic here, and I
wouldn't want to have people crosspost C++ answers back here.

Quote:
> If you decide it needs to be in multiple groups, crosspost, don't
> multipost. That way, the two groups know what's going on.

Assuming the OP did want to know how to do this in both C and C++ (two very
different languages), I'd say that the OP was right in multiposting in this
case. An answer which describes how to do it in C++ is quite off-topic here,
and while the answers given here probably also work in C++, they're also
probably not optimal or stylistic.

--
Simon.



Wed, 05 May 2004 19:52:37 GMT  
 memory block containing pointers, aka pointer to pointer i believe

Quote:



> >ptr = malloc[1];
>        ^^^^^^

> ITYM `ptr = memory[1]'.

Hehe. Yes of course. Too much typing, too little proofreading.

Brian Rodenborn



Thu, 06 May 2004 03:22:20 GMT  
 memory block containing pointers, aka pointer to pointer i believe

Quote:


> > This same message was also posted to comp.lang.c++. First of all, figure
> > out where the message belongs. You're going to get different answers
> > here than there (I'll bet they tell you to drop malloc and go with
> > new[]);

> Perhaps, but any answer which pertains to C++ is off-topic here, and I
> wouldn't want to have people crosspost C++ answers back here.

No, but the C++ people shouldn't be wasting time showing him how to do
it using new[] and delete[] when he really wants a C answer.

Quote:
> > If you decide it needs to be in multiple groups, crosspost, don't
> > multipost. That way, the two groups know what's going on.

> Assuming the OP did want to know how to do this in both C and C++ (two very
> different languages), I'd say that the OP was right in multiposting in this
> case.

What are the chances of that?

More likely, he didn't have any idea how to post it correctly. I think
my assumption is far more probable than yours, so my admonishment
stands.

Brian Rodenborn



Thu, 06 May 2004 03:26:18 GMT  
 
 [ 14 post ] 

 Relevant Pages 

1. memory leak: pointer->pointer->pointer->struct

2. memory Leak: pointer->pointer->pointer->struct

3. Dereferencing f-pointers, arrays of f-pointers, pointers to f-pointers

4. Pointers to Structure that contains pointer to Function

5. dereferencing pointer to a structure containing a pointer to int

6. memory allocation for pointer to pointer to char

7. free()'ing structs containing pointers to memory?

8. Pointer of Pointers was Pointer of arrays...

9. pointers pointers pointers!!!

10. Pointer Functions and Pointers to Pointer Functions

11. Pointer to Pointer to Pointer....

12. Question on pointer-to-pointer-to-pointer

 

 
Powered by phpBB® Forum Software