Skip to contentSkip to conversation

Question about Malloc

DISCUSSION Edward Parrilla 4 participants 6 messages Oct 10, 2004 – Oct 19, 2004

lore
Share & tools
lore
#0 lore raw

Hi all,
I got the following structure:
typedef struct {

 	char *file;
	char *ip_addres;
	}TABLE;

then define a pointer to it:

typedef TABLE *Recpointer;
Recpointer r;

I'm trying to use malloc as:

r=malloc(Recpointer);
but it gives me error.

What should be the right sintax?

Any help would be appreciated.
Thanks in advance

Ed

#1 lore raw
Quoted 11 lines · Edward Parrilla

I got the following structure:

typedef struct  {
        char *file;
        char *ip_addres;
        }TABLE;

then define a pointer to it:

typedef TABLE *Recpointer;
Recpointer r;

Quoted 6 lines

I'm trying to use malloc as:

r=malloc(Recpointer);
but it gives me error.

What should be the right sintax?

The word in "syntax". (A "sin tax" would be too expensive.)

"man malloc" says (in part):

Quoted 8 lines
Quoted 8 lines
        void *calloc(size_t nmemb, size_t size);
        void *malloc(size_t size);

        calloc()  allocates memory for an array of nmemb elements of 

size bytes each and returns a pointer to the allocated memory. The memory is set to zero.

malloc() allocates 'size' bytes and returns a pointer to the
allocated memory. The memory is not cleared.

If you want to allocate exactly one TABLE struct, it should be something more like:

        r=malloc(sizeof(TABLE));
        assert(NULL != r);

If you want to allocate an array of TABLE structs (which the name implies), use something like:

        #define TABLE_MAX       100
        r = calloc(TABLE_MAX, sizeof(TABLE));
        assert(NULL != r);
Quoted 1 line

Any help would be appreciated.

RTFM: Read the manpage. Or, "Use the manpage, Luke!" It wouldn't hurt to read K&R 2nd edition either.

--
Jeff Woods <[email protected]>

#2 lore raw
In reply to Edward Parrilla

On Sun, 2004-10-10 00:31:05 -0500, Edward Parrilla <[email protected]> wrote in message <[email protected]>:

Quoted 9 lines

Hi all,
I got the following structure:
typedef struct {

 	char *file;
	char *ip_addres;
	}TABLE;

then define a pointer to it:

Quoted 7 lines

typedef TABLE *Recpointer;
Recpointer r;

I'm trying to use malloc as:

r=malloc(Recpointer);
but it gives me error.

...which is correct. malloc() allocates memory, byte-wise, not complex types. So you tell it how many bytes you want and it tries to do exactly that. It's not C++! Additionally, your newly created Recpointer type is a pointer, so if you'd do exactly the above correct, you'd not need to malloc sizeof(Recpointer), but sizeof(TABLE).

But do you see the general mistake here? You typedef'ed your structures twice, which completely hides you all the details. _This_ is what leads you to wrong programming in just this case.

If you'd done it like this

struct table_entry {
	char *file;
	char *ip_address;
};

...
struct table_entry *entry;
entry = malloc (sizeof (struct table_entry));

you'd easily see the flow of types here. It doesn't hide the defails from your eyes, thus gives you the chance to see what's happening. After a number of typedefs, you usually don't know any more what is what...

MfG, JBG

Signature · 4 lines
Jan-Benedict Glaw       [email protected]    . +49-172-7608481             _ O _
"Eine Freie Meinung in  einem Freien Kopf    | Gegen Zensur | Gegen Krieg  _ _ O
 fuer einen Freien Staat voll Freier Bürger" | im Internet! |   im Irak!   O O O
ret = do_actions((curr | FREE_SPEECH) & ~(NEW_COPYRIGHT_LAW | DRM | TCPA));
#3 lore raw

It's not safer to make:

Recpointer r;
r=(Recpointer)malloc(Recpointer);

Quoted 19 lines · Jan-Benedict Glaw

On Sun, 2004-10-10 00:31:05 -0500, Edward Parrilla <[email protected]> wrote in message <[email protected]>:

Quoted 16 lines

Hi all,
I got the following structure:

typedef struct  {
      char *file;
      char *ip_addres;
      }TABLE;

then define a pointer to it:

typedef TABLE *Recpointer;
Recpointer r;

I'm trying to use malloc as:

r=malloc(Recpointer);
but it gives me error.

Signature · 5 lines
Matías Aguirre
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#4 lore raw

On Tue, 2004-10-19 09:41:49 -0300, Matías Aguirre <[email protected]> wrote in message <[email protected]>:

Quoted 4 lines

It's not safer to make:

Recpointer r;
r=(Recpointer)malloc(Recpointer);

malloc is void, while r isn't. So you don't need the cast here. However, notice that your malloc call is actually wrong: the argument isn't a number (or sizeof(something)), but only a type.

General rule of thumb:

The "output" of malloc should be assigned to a variable whose type is a pointer to whatever you give to sizeof(xxx) as the malloc argument.

or in short:

One star more on the left than on the right.

Of course, if you play with types, this doesn't apply any longer...

MfG, JBG

Signature · 4 lines
Jan-Benedict Glaw       [email protected]    . +49-172-7608481             _ O _
"Eine Freie Meinung in  einem Freien Kopf    | Gegen Zensur | Gegen Krieg  _ _ O
 fuer einen Freien Staat voll Freier Bürger" | im Internet! |   im Irak!   O O O
ret = do_actions((curr | FREE_SPEECH) & ~(NEW_COPYRIGHT_LAW | DRM | TCPA));
#5 lore raw

i agree with that, i didn't fix inside de malloc, i only suggested about the cast

but my big error was speak about c++ and not about c, in c++ the compiler (g++ in this case) warn you about that conversion....

sorry

Quoted 35 lines · Jan-Benedict Glaw

On Tue, 2004-10-19 09:41:49 -0300, Matías Aguirre <[email protected]> wrote in message <[email protected]>:

Quoted 4 lines

It's not safer to make:

Recpointer r;
r=(Recpointer)malloc(Recpointer);

malloc is void, while r isn't. So you don't need the cast here. However, notice that your malloc call is actually wrong: the argument isn't a number (or sizeof(something)), but only a type.

General rule of thumb:

The "output" of malloc should be assigned to a variable whose type is a pointer to whatever you give to sizeof(xxx) as the malloc argument.

or in short:

One star more on the left than on the right.

Of course, if you play with types, this doesn't apply any longer...

MfG, JBG

--

Jan-Benedict Glaw       [email protected]    . +49-172-7608481             _ O _
"Eine Freie Meinung in  einem Freien Kopf    | Gegen Zensur | Gegen Krieg  _ _ O

fuer einen Freien Staat voll Freier Bürger" | im Internet! | im Irak! O O O
ret = do_actions((curr | FREE_SPEECH) & ~(NEW_COPYRIGHT_LAW | DRM | TCPA));

Signature · 5 lines
Matías Aguirre
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html