malloc stumper

J

J Krugman

Suppose that I have some custom type foo that I know is defined as
a "pointer to something", but I don't know what "something" is.
I want to use malloc to allocate some space for a variable of type
foo to point to. I.e. I want to use malloc to allocate space for
a "something" and assign malloc's return value to a variable of
type foo. But to do this, malloc needs to know the size of a
"something", and I'm stumped. Is there any way for the code to
determine the size of a "something" from foo?

Thanks,

jill
 
G

Gordon Burditt

Suppose that I have some custom type foo that I know is defined as
a "pointer to something", but I don't know what "something" is.

If you don't know what "something" is, how did you declare foo?
pointer to something? void *?
I want to use malloc to allocate some space for a variable of type
foo to point to. I.e. I want to use malloc to allocate space for
a "something" and assign malloc's return value to a variable of
type foo. But to do this, malloc needs to know the size of a
"something", and I'm stumped. Is there any way for the code to
determine the size of a "something" from foo?

If foo is declared as a pointer to something, you can use:

foo = malloc(sizeof(*foo));

If foo is a void * or other pointer type, this approach will not work.

Gordon L. Burditt
 
M

Malcolm

J Krugman said:
Suppose that I have some custom type foo that I know is defined as
a "pointer to something", but I don't know what "something" is.
I want to use malloc to allocate some space for a variable of type
foo to point to.

typedef struct
{
int x;
}foo;

....
foo *fooptr = malloc(N * sizeof(foo));


What C does not have is a "type" variable, that holds a type to be
determined at runtime. However you can usually get round this with a
combination of void *s and sizes. Let's say we want to store some foos in a
hashtable.

typedef struct
{
size_t elsize;
void *entries;
} HASH;

HASH hashable(size_t element_size)
{
entries = malloc(element_size * TABLESIZE);
elsize = element_size;
}

void store_entry(HASH *h, void *data, int (*hash)(void *data))
{
int slot = hash(data);
/* we need a bit of complexity to make sure slot is free, will skate over
this */
unsigned char *ptr = h->entries;
memcpy(ptr + slot * h->elsize, data, elsize);
}
 
F

Flash Gordon

Suppose that I have some custom type foo that I know is defined as
a "pointer to something", but I don't know what "something" is.
I want to use malloc to allocate some space for a variable of type
foo to point to. I.e. I want to use malloc to allocate space for
a "something" and assign malloc's return value to a variable of
type foo. But to do this, malloc needs to know the size of a
"something", and I'm stumped. Is there any way for the code to
determine the size of a "something" from foo?

foo *bar = malloc( n * sizeof *bar);

The will allocate space for n items of the type bar points to. It is
also easy to maintain since you could have
foo *bar;
/* lots of code */
bar = malloc( n * sizeof *bar);
and you don't have to find where bar is defined to see if the malloc
allocates enough space.
 
T

Tim Rentsch

J Krugman said:
Suppose that I have some custom type foo that I know is defined as
a "pointer to something", but I don't know what "something" is.
I want to use malloc to allocate some space for a variable of type
foo to point to. I.e. I want to use malloc to allocate space for
a "something" and assign malloc's return value to a variable of
type foo. But to do this, malloc needs to know the size of a
"something", and I'm stumped. Is there any way for the code to
determine the size of a "something" from foo?

If 'foo' is an opaque pointer type, the standard malloc idioms for
allocating storage for variables of type foo don't work. One obvious
way to get around this is for the opaque type module to also export a
function that gives the size:

extern size_t sizeof_foo_object( void );

...

size_t
sizeof_foo_object(){
foo f = 0;
return sizeof( *f );
}

What's probably a better way is for the opaque type module to
export a function that does the actual allocation. Here is one
that also allows allocating an array of 'foo' objects:

extern foo new_foo( unsigned int how_many );

...

foo
new_foo( unsigned int n ){
return malloc( n * sizeof( *new_foo(1) ) );
}

Note that the argument to 'sizeof' is there only to guarantee
consistency between the size of the elements being allocated and the
return type of the function - it doesn't actually invoke new_foo
recursively.
 
R

Richard Tobin

Gordon Burditt said:
If foo is declared as a pointer to something, you can use:

foo = malloc(sizeof(*foo));

Provided you have a declaration in scope for whatever whatever *foo
is. If you're using a .h file that declares

typedef struct footype_struct *footype;

without providing a declaration of struct xtype_struct, you're out
of luck. In such a case you're presumably not meant to allocate
your own, or else some constructor function is provided.

-- Richard
 
R

Richard Tobin

If foo is declared as a pointer to something, you can use:

foo = malloc(sizeof(*foo));

Provided you have a declaration in scope for whatever whatever *foo
is. If you're using a .h file that declares

typedef struct footype_struct *footype;

without providing a declaration of struct footype_struct, you're out
of luck. In such a case you're presumably not meant to allocate your
own, or else some constructor function is provided.

-- Richard
 
B

bd

J said:
Suppose that I have some custom type foo that I know is defined as
a "pointer to something", but I don't know what "something" is.
I want to use malloc to allocate some space for a variable of type
foo to point to. I.e. I want to use malloc to allocate space for
a "something" and assign malloc's return value to a variable of
type foo. But to do this, malloc needs to know the size of a
"something", and I'm stumped. Is there any way for the code to
determine the size of a "something" from foo?

I'm assuming you mean something like:
foo *x = malloc(?)

There are two ways:

sizeof foo /* this will evaluate to the size of foo, in bytes */

foo *x;
sizeof *x /* return the size of whatever x points to. */

The latter is generally recommended for mallocing, as it makes changing the
type easier if you need to later, since the malloc won't depend on it:
int *x;
x = malloc(sizeof *x);
if (!x) abort();

If you need to change x to a long later, you only need to change one place.
 
A

Al Bowers

bd said:
J Krugman wrote:




I'm assuming you mean something like:
foo *x = malloc(?)

There are two ways:

sizeof foo /* this will evaluate to the size of foo, in bytes */

foo *x;
sizeof *x /* return the size of whatever x points to. */

In the first of the two ways, foo is a type-name, thus it should be
sizeof(foo)

6.5.3 Unary operators
Syntax
1 unary-expression:
postfix-expression
++ unary-expression
-- unary-expression
unary-operator cast-expression
sizeof unary-expression
sizeof ( type-name )
 
D

Dave Thompson

On Sat, 4 Dec 2004 18:27:09 -0000, "Malcolm"
What C does not have is a "type" variable, that holds a type to be
determined at runtime. However you can usually get round this with a
combination of void *s and sizes. Let's say we want to store some foos in a
hashtable.

typedef struct
{
size_t elsize;
void *entries;
} HASH;

HASH hashable(size_t element_size)
{
entries = malloc(element_size * TABLESIZE);
elsize = element_size;
}
C is not C++ with an implicit class instance. You need
HASH h;
h.entries =
h.elsize =
return h;
or in C99
HASH h = {malloc(...), ...};
or even
HASH h = {.entries = malloc(...), .elsize = ...};

Also 'hashable' is an odd though legal name. I would make it at least
'hashtable' and probably something like 'create_hashtable'.
void store_entry(HASH *h, void *data, int (*hash)(void *data))

You generally need to use the same hash function(s) for all entries in
a given table so it makes more sense to set it/them once in the header
rather than redundantly specify on each call.


- David.Thompson1 at worldnet.att.net
 

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

Similar Threads

malloc 40
malloc and maximum size 56
array-size/malloc limit and strlen() failure 26
Malloc question 9
malloc() 6
restrictions with malloc??? 15
struct/malloc failure 3
Which allocator used by malloc 11

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top