my problem is highly conceptual problem.

when i write: node *new1=malloc(sizeof(node));

it works properly.

but when i write : node *new1=malloc(sizeof(node*));

it starts giving errors.

// node is the structure having a integer and pointer to itself.

## All 15 Replies

What error are you getting?

When you are allocating memory for a pointer, you want it to be the size of the data the pointer is pointing to, so your first example is correct. A node pointer should pointer to allocated memory the size of a node.

my problem is highly conceptual problem.

when i write: node *new1=malloc(sizeof(node));

it works properly.

but when i write : node *new1=malloc(sizeof(node*));

it starts giving errors.

// node is the structure having a integer and pointer to itself.

If `sizeof(node) > sizeof(node*)` then you'll have problems because you're allocating less memory to the pointer than you're sure to end up trying to use.

You want to use the malloc function to allocate memory on the heap for the size that a variable, array, etc. will occupy.

One way the * operator can be explained is by saying "the contents of" in its place.

So, your first example makes sense: the contents of new1 = malloc(sizeof(node))

In other words, malloc is allocating the size in bytes that node will hold in memory for the contents of new1.

A variable or an object takes up a certain amount of space or bytes in memory: a byte variable would take up more space than a long variable.

A pointer variable contains an address to a location. It holds the value of an address.

Think of a pointer variable as the address on the house: say 123.

malloc's intent is to allocate memory for what's inside the house at 123, not for allocating memory for the address on the house.

That's what you're doing in the second example: allocating memory for the address instead of what's inside the house at 123. That's not the intended use of malloc.

What error are you getting?

When you are allocating memory for a pointer, you want it to be the size of the data the pointer is pointing to, so your first example is correct. A node pointer should pointer to allocated memory the size of a node.

@lamouche sir, plz will u make it more clearified.?

You want to use the malloc function to allocate memory on the heap for the size that a variable, array, etc. will occupy.

One way the * operator can be explained is by saying "the contents of" in its place.

So, your first example makes sense: the contents of new1 = malloc(sizeof(node))

In other words, malloc is allocating the size in bytes that node will hold in memory for the contents of new1.

A variable or an object takes up a certain amount of space or bytes in memory: a byte variable would take up more space than a long variable.

A pointer variable contains an address to a location. It holds the value of an address.

Think of a pointer variable as the address on the house: say 123.

malloc's intent is to allocate memory for what's inside the house at 123, not for allocating memory for the address on the house.

That's what you're doing in the second example: allocating memory for the address instead of what's inside the house at 123. That's not the intended use of malloc.

@hkdani can u tell me wat this statement means ? "node *new1= malloc(sizeof(node*));"
means meaning of each and every word in this statament.?

node *new1= malloc(sizeof(node*)) means make a pointer to a node. Call this pointer new1. Make it point to a lump of memory. Make that lump of memory the size of a pointer.

Do you understand the difference between a node and a pointer (or indeed, an int and a pointer, or anything and a pointer)?

node *new1= malloc(sizeof(node*)) means make a pointer to a node. Call this pointer new1. Make it point to a lump of memory. Make that lump of memory the size of a pointer.

Do you understand the difference between a node and a pointer (or indeed, an int and a pointer, or anything and a pointer)?

ya! i knoe difference between pointer and pointer to integer.thats not mah question. my question is that wat is the every possible difference between sizeof(node*); and sizeof(node);

node
1. In local area networks, a device that is connected to the network and is capable of communicating with other network devices. 2. In tree structures, a location on the tree that can have links to one or more nodes below it. Some authors make a distinction between node and element, with an element being a given data type and a node comprising one or more elements as well as any supporting data structures. (MSDN)

So, you are declaring the contents of new1 to be equal to what memory is allocated for the size of the address of a node pointer variable.

the contents of new1 is allocated for the size of the address of a pointer to node
*new1 -----------------= ---- malloc ------- sizeof -------------(node*)

Many times the purpose of allocating memory on the heap is so that all memory transactions take place on the physical random access memory (RAM) of the system instead of being possibly swapped out with virtual memory; i.e the slower memory on the hard drive. This could greatly slow down a thread or a process. So, if your process or thread is constantly accessing node new1, you don't want it using virtual memory.

wat is the every possible difference between sizeof(node*); and sizeof(node);

Any variable using the * operator means that it contains the memory address of a variable.

What is important to know is that your memory has been set aside by malloc insuring that a process or thread is accessing dynamic memory or RAM. malloc needs to know how much memory to set aside. And since variables come in different byte sizes, you must tell it which type of variable to use with the sizeof function. Using the sizeof function on an address (node*) will not tell you what size needs to be allocated for the variable node: sizeof(node).

Your program will be using a thread or process that needs to know what's inside the variable node. It will know the address, but if it has to kludge through virtual memory to find the information it needs, then your program will not operate at peak efficiency.

So, the real question is not what is the difference. But what is the reason. What is the reason for using node instead of node*.

I don't know why you're looking for differences. An address is an address. A variable is a variable. It's kind of basic. Don't make it more difficult than what it is. Wrapping your head around the meaning of pointers in C is sometimes a process that takes a few months or even a year or so for some.

ya! i knoe difference between pointer and pointer to integer.

No, you don't. You think you do, but if you did know you would not be asking this;

my question is that wat is the every possible difference between sizeof(node*); and sizeof(node);

A node is some kind of object. A pointer to a node is not a node. It is completely different. It's a different kind of object. They're completely different objects.

As a general rule of thumb, a pointer (any pointer, pointing to any kind of object) will be the same size as an int. Whatever size a pointer is, all your pointers will be the same size. A pointer to an int is the same size as a pointer to a double is the same size as a pointer to a node is the same size as etc. etc. etc. A pointer is a number. One number. That's it. That's why all pointers are the same size. Because they're just a number.

Whatever it points to could be some huge enormous object taking up gigabytes of memory, or it could be a single byte. So, the difference in size between a node and a node* could be anything from zero (same size) to as much memory as your system can handle - gigabytes and gigabytes.

commented: Demonstrates a fundamental understanding of a complex subject and explains it in terms easily grasped. +5

What is important to know is that your memory has been set aside by malloc insuring that a process or thread is accessing dynamic memory or RAM. malloc needs to know how much memory to set aside. And since variables come in different byte sizes, you must tell it which type of variable to use with the sizeof function. Using the sizeof function on an address (node*) will not tell you what size needs to be allocated for the variable node: sizeof(node).

these lines has made me clear so finely. really thnxx for this help. i will tell it my group as they were also facing this conceptual problem.thanks.

Many times the purpose of allocating memory on the heap is so that all memory transactions take place on the physical random access memory (RAM) of the system instead of being possibly swapped out with virtual memory; i.e the slower memory on the hard drive. This could greatly slow down a thread or a process. So, if your process or thread is constantly accessing node new1, you don't want it using virtual memory.

No, you don't. You think you do, but if you did know you would not be asking this;

A node is some kind of object. A pointer to a node is not a node. It is completely different. It's a different kind of object. They're completely different objects.

As a general rule of thumb, a pointer (any pointer, pointing to any kind of object) will be the same size as an int. Whatever size a pointer is, all your pointers will be the same size. A pointer to an int is the same size as a pointer to a double is the same size as a pointer to a node is the same size as etc. etc. etc. A pointer is a number. One number. That's it. That's why all pointers are the same size. Because they're just a number.

Whatever it points to could be some huge enormous object taking up gigabytes of memory, or it could be a single byte. So, the difference in size between a node and a node* could be anything from zero (same size) to as much memory as your system can handle - gigabytes and gigabytes.

Virtual memory. Memory from the hard drive simulated to run as Random Access Memory (RAM) when your computer runs out of RAM resources. Your system will thrash when using the swap memory or virtual memory. Performance will slow down.

The heap is just a generic term referring to the available Random Access Memory. You might want to a Google search using the two words HEAP and STACK.

Virtual memory. Memory from the hard drive simulated to run as Random Access Memory (RAM) when your computer runs out of RAM resources. Your system will thrash when using the swap memory or virtual memory. Performance will slow down.

The heap is just a generic term referring to the available Random Access Memory. You might want to a Google search using the two words HEAP and STACK.

hats off to hkdani. wat a cloud of concept knowledge u have. my thread is closed. awsum answers. hats off.!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.