user3682928
user3682928

Reputation: 93

malloc allocate memory to a non-pointer type

How to allocate memory to a type which is not a pointer ?

I'd searched on the Internet and my textbook then got nothing about this.

All the example come from those like that

char *test;
test = malloc (STRING_SIZE * sizeof (char));

Which in pointer form, but what if a structure don't even need a pointer type to do that job as follow

struct _test_t {
  char *name;
  int flag;        //  <-- Here
};

or this is deprecated ? What I can do is only declare as a pointer

...
int *flag;
...

then allocate as usual ?

struct _test_t *t;
t->flag = malloc (sizeof (int));

or any way out ?

Upvotes: 4

Views: 4530

Answers (2)

Medo42
Medo42

Reputation: 3821

You seem to have some confusion about pointers and where things end up in memory. The fields of a struct are part of the struct's memory. In your case, the struct consists of a char pointer and an int. You don't need to allocate those separately once you have an instance of the struct, because they are already part of the struct.

struct _test_t *t = malloc(sizeof(struct _test_t));
t->flag = 123;
t->name = NULL;

Note that name doesn't point to anything interesting yet. Let's change that:

t->name = malloc(100);

Note the distinction between the pointer name and the memory that it points to. Only the pointer itself is part of the struct.

It is also possible to create an instance of the struct without using malloc, by allocating it on the stack:

struct _test_t t;
t.flag = 123;
t.name = malloc(100);

t is now allocated on the stack and will automatically be freed again when you leave the scope that it was declared in. However, name still points to memory on the heap which does not get freed with the struct, so you'd have to free that manually once you no longer need it.

Edit: You mention that you want to allocate memory to something that is not a pointer. That doesn't really make sense to me though. The function of malloc is to reserve some memory for your use and return a pointer to that memory. It needs to return the address of the memory that it has allocated because otherwise you wouldn't know where it is. And a pointer is really just an address value.

You can allocate space for an int with malloc, but you will get a pointer to an int that you have to use to access the actual int:

int *i = malloc(sizeof(int));
*i = 123;

In C, there is no way to refer to that int directly without going through a pointer, which might be what you want. In C++ you could use a reference to refer to the int value directly:

int *i = (int*)malloc(sizeof(int));
*i = 123;
int &j = *i;
cout << j << endl;

This would print "123", showing that j directly refers to the integer value. However, C does not have that feature, so you will always have to use the pointer.

Upvotes: 3

lucasg
lucasg

Reputation: 11012

malloc is used to allocate memory for objects whose size is not known at compile-time. That's why it returns a pointer, since it's the C representation for addresses (here of the memory area allocated on the heap).

Using malloc for another type does not make any sense, since their size are known (apart from templates but that's C++). Your _test_t struct (resp struct _test_t *t) is allocated resp. on the stack or the heap when declared, no need for you to manually allocate each members.

Upvotes: 0

Related Questions