pmverma
pmverma

Reputation: 1703

Do every variable declared as pointer have to allocate memory?

Well, I am new to C. I would like to know as my title says.

Suppose I declare pointers as following,

char *chptr1;
char **chptr2;
int *i;
int **ii;
struct somestruct *structvar1;
struct somestruct **structvar2;

Then,

  1. Do I need to allocate memory for every variable, before storing data into them?
  2. Is there any special case when I do not need to allocate memory for them? for this I know one for char pointer, strdup() which allocate memory itself, we have not to care much about it.
  3. Any further suggestions are welcome.

Upvotes: 10

Views: 8827

Answers (4)

Devolus
Devolus

Reputation: 22094

Do I need to allocate memory for every variable, before storing data into them?

It depends on the use case.

Let's assume you have aprogram where you can enter a user name, but if no user name is entered, a hardcoded value "Default User" is shown. Now you want to pass arround that name. If the user entered a name you might have allocted some space for it, and pass aorund that pointer. If no user name is given you are pointing to that hard coded value, which you already predefined, and you don't need to allocate memory for it.

 char const *Default = "Default User"
 char *username = NULL;
 username = getUser();   // here the name is allocated dynamically if it is present.
 if(username == NULL)
    username = Default;

In the above case, the sapce for the "Default User" String is already reserved in the executable by the compiler, so you need not to allocate additional memory for it, unless you want to manipulate it.

Another case could be when you program a low level device, and you have a pointer to a hardware buffer provided by the device. In such a case you also wouldn't allocate memory, but you would still use the pointer for that buffer which may be on a fixed address. So you would use the pointer like this (Example which would change the screen border color on a C64):

 char *BorderColor = 0xd020;
 *BorderColor = 0;

Upvotes: 3

Tao T
Tao T

Reputation: 66

I think that in C, a variable is a pointer added * with a memory. And a pure pointer has no memory. So if we want to store a data, we need to give the pointer a memory. Sure, there are many ways to assign the memory.

First:

uint32_t x;
uint32_t *p = &x;
uint32_t array[10];
uint32_t *q = array;// equals to int *q = &array[0];

Second:

#define NUM_MACRO   ((uint32_t) 10)
//we must free after we using. Or, perhaps we we will get a segment fault.
uint32_t *p = malloc(size(uint32_t) * NUM_MACRO); 

Upvotes: 1

Kerrek SB
Kerrek SB

Reputation: 477070

Pointers point at things. It's up to you what you make them point at.

  • You can leave them uninitialized and don't use them: int * q; That's a little silly.

  • You can make them point to something that exists: int x; int * q = &x;

  • You can store the address of dynamically allocated memory in them: int * q = malloc(29);

Upvotes: 13

Chinna
Chinna

Reputation: 3992

First thing you need to understand is, pointers are variables which are used to store addresses of memory or addresses of other variables. When you declare a pointer, you are allocating memory for that pointer and not to data pointing by that pointer. For example,

char *ptr; //Here you allocated memory for pointer variable.
ptr = malloc(sizeof(char)); // allocated memory for the data pointed by ptr

Then call free() after using memory

free(ptr); // DE-allocates memory pointed by ptr and not variable ptr. 

Upvotes: 8

Related Questions