|
Blue Forest http://www.lslnet.com at 10:18 on June 6, 2006
Malloc (struct A *) problem Struct Node
{
Int ia;
Int ib;
Char ca;
};
Typedef struct Node NODE;
Typedef YA * NODEPTR;
Main ()
{
NODEPTR malloc_node_ptr, malloc_nodeptr_ptr;
Malloc_node_ptr = malloc (sizeof (Dalian));
Malloc_nodeptr_ptr = malloc (sizeof (NODEPTR));
Printf ( "sizeof (Dalian) : %d\n", sizeof (Dalian));
Printf ( "sizeof (NODEPTR) : %d\n", sizeof (NODEPTR));
Printf ( "sizeof (malloc_node_ptr) : %d\n", sizeof (malloc_node_ptr));
Printf ( "sizeof (malloc_nodeptr_ptr) : %d\n", sizeof (malloc_nodeptr_ptr));
Printf ( "sizeof (*malloc_node_ptr) : %d\n", sizeof (*malloc_node_ptr));
Printf ( "sizeof (*malloc_nodeptr_ptr) : %d\n", sizeof (*malloc_nodeptr_ptr));
Free (malloc_node_ptr);
Free (malloc_nodeptr_ptr);
}
Such an outcome will be
Sizeof (Dalian) : 12
Sizeof (NODEPTR) :
Sizeof (malloc_node_ptr) :
Sizeof (malloc_nodeptr_ptr) :
Sizeof (*malloc_node_ptr) : 12
Sizeof (*malloc_nodeptr_ptr) : 12
Why sizeof (Dalian) sizeof (NODEPTR), but there really malloc allocation of the same size. |
Malloc (struct A *) problem Dalian is the type of structure, the share byte count for 4+4+4=12
NODEPTR itself is the target type at Dalian, sizeof (NODEPTR) is seeking an indication of the size of the share of the storage space in the guidelines exist in a 32-bit word length machine (32-bit, 4 bytes) of memory space. So the result is 4. |
Malloc (struct A *) problem Understand this.
I would like to say is :
Malloc two different sentences, but they like sizeof. |
Malloc (struct A *) problem Malloc_node_ptr = malloc (sizeof (Dalian));
Malloc_nodeptr_ptr = malloc (sizeof (NODEPTR));
These are both indicators
Sizeof (malloc_node_ptr) :
Sizeof (malloc_nodeptr_ptr) :
The indicator is calculated on the length indicators are not referring to the size of memory, two different and quite rightly |
Malloc (struct A *) problem Very simple!
Procedures
";>;NODEPTR Malloc_node_ptr, malloc_nodeptr_ptr;
Malloc_nodeptr_ptr front of a small *. |
Malloc (struct A *) problem Sizeof (Dalian) : 12
Sizeof (NODEPTR) :
Sizeof (Dalian) is a length of the structure, sizeof (NODEPTR) is a guide length
Not not know? |
Malloc (struct A *) problem -->
Sizeof () not malloc () request for the memory size, in order to know the size, it is time for you to remember how much there. |
Malloc (struct A *) problem This is the equivalent of three
Sizeof (Dalian)
Sizeof (*malloc_node_ptr)
Sizeof (*malloc_nodeptr_ptr)
Int as a
Sizeof (int) =sizeof (a)
Is it true? |
Malloc (struct A *) problem -->
If only to say malloc malloc_nodeptr_ptr= malloc (sizeof (NODEPTR)); Distribution of the four bytes of memory, this is obviously wrong.
#include "Stdio.h>;
#include "Stdlib.h>;
Struct (listNode
Char data;
Struct listNode * nextPtr;
};
Typedef struct listNode LISTNODE;
Typedef LISTNODE * LISTNODEPTR;
Void insert (LISTNODEPTR *, char);
Char delete (LISTNODEPTR *, char);
Int isEmpty (LISTNODEPTR);
Void printList (LISTNODEPTR);
Void instructions (void);
Main ()
{
LISTNODEPTR startPtr = NULL;
Int choice;
Char item;
Instructions ();
Printf ( "? ");
Scanf ( "%d" &choice);
While (choice> 3) (
Switch (choice) (
Case 1 :
Printf ( "Enter acharacter :");
Scanf ( "\n%c" &item);
Insert (&startPtr, item);
PrintList (startPtr);
Break;
Case 2 :
If (!isEmpty (startPtr)) (
Printf ( "Enter character to be deleted :");
Scanf ( "\n%c" &item);
If (delete (&startPtr, item)) (
Printf ( "%c deleted.\n" item);
PrintList (startPtr);
) Else
Printf ( "%c not found.\n\n" item);
) Else
Printf ( "List is empty.\n\n");
Break;
Default :
Printf ( "Invalid choice.\n\n");
Instructions ();
Break;
}
Printf ( "? ");
Scanf ( "%d" &choice);
}
Printf ( "End of run. \n ");
Return 0
}
Void instructions (void)
{
Printf ( "Enter your choice : \n"
"1 to insert a element into the list. \n "
"2 to delete an element from the list. \n "
"3 to end.\n");
}
Void insert (LISTNODEPTR *sPtr, char value)
{
LISTNODEPTR newPtr, previousPtr, currentPtr;
NewPtr = malloc (sizeof (LISTNODEPTR));
If (newPtr> NULL) (
NewPtr ->; data = value;
NewPtr ->; nextPtr = NULL;
PreviousPtr = NULL;
CurrentPtr = *sPtr;
While (currentPtr, named value>;currentPtr->;data> NULL) (
PreviousPtr = currentPtr;
CurrentPtr = currentPtr ->; nextPtr;
}
If (previousPtr ====== NULL) (
NewPtr->; nextPtr = *sPtr;
*sPtr = NewPtr;
}else --
PreviousPtr->;nextPtr = newPtr;
NewPtr ->; nextPtr = currentPtr;
}
) Else
Printf ( "%c not inserted. NO memory available.\n "value);
}
Char delete (LISTNODEPTR *sPtr, char value)
{
LISTNODEPTR previousPtr, currentPtr, tempPtr;
If (value ====== (*sPtr) ->; data) (
TempPtr = *sPtr;
*sPtr = (*sPtr) ->; NextPtr;
Free (tempPtr);
Return value;
) Else (
PreviousPtr = *sPtr;
CurrentPtr = (*sPtr) ->; nextPtr;
While (NULL, named currentPtr->;data currentPtr>> value) (
PreviousPtr = currentPtr;
CurrentPtr = currentPtr ->; nextPtr;
}
If (currentPtr> NULL) (
TempPtr = currentPtr;
PreviousPtr->;nextPtr = currentPtr ->; nextPtr;
Free (tempPtr);
Return value;
}
}
Return '\0';
}
Int isEmpty (LISTNODEPTR sPtr)
{
Return sPtr ====== NULL;
}
Void printList (LISTNODEPTR currentPtr)
{
If (currentPtr ====== NULL)
Printf ( "List is empty.\n\n");
Else (
Printf ( "The list is:\n");
While (currentPtr> NULL) (
Printf ( "%c -->;" currentPtr->;data);
CurrentPtr = currentPtr->;nextPtr;
}
Printf ( "NULL.\n\n");
}
}
The malloc, this procedure proved to be absolutely correct.
Say C (GCC3.3, TC), a meaningful place (ISO C 99, I saw no definition of malloc) for malloc () a sizeof (struct), sizeof (struct *) is the same.
Malloc indicators we watch carefully to the two parameters are different. |
Malloc (struct A *) problem
| Malloc (struct A *) problem -->
Drive You have committed minor mistakes ah |
Malloc (struct A *) problem I have not committed minor mistakes : em16 em16 : : : : : : em16 : em16
* No. malloc_node_ptr not this is the type of structure and malloc_nodeptr_ptr target type.
Printf ( "sizeof (*malloc_node_ptr) : %d\n", sizeof (*malloc_node_ptr));
Printf ( "sizeof (*malloc_nodeptr_ptr) : %d\n", sizeof (*malloc_nodeptr_ptr));
The fight is the length of the structure
* The increase (this is the landlord's intent? ), It is a different story. . . |
Malloc (struct A *) problem -->
Several bytes allocated in the end?
[code]
#include "Stdio.h>;
Struct Node
{
Int ia;
Int ib;
Char ca;
};
Typedef struct Node NODE;
Typedef YA * NODEPTR;
Int main (void)
{
Int a;
NODEPTR p = (NODEPTR) & (Here is a being eaten by cu)
Printf ( "sizeof (6-12) : %d\n", sizeof (J));
Return 0
}
[/code]
Gcc v3.3.3 (cygwin) :
Sizeof (6-12) : 12 |
Malloc (struct A *) problem -->
Sorry, to the effect that, and not look at who malloc_nodeptr_ptr variables.
But the landlord to do malloc_nodeptr_ptr = malloc (sizeof (NODEPTR)); Meaningless ah |
Malloc (struct A *) problem Of course, he sizeof (*malloc_nodeptr_ptr), the owner may not necessarily be the length of malloc, as everyone knows, I do not bother to say
The procedure is only the landlord would like to express his opinion, but I have to say that mistakes could not reach |
Malloc (struct A *) problem You misinterpreted the meaning of the landlord. NODEPTR out a landlord does not want to malloc
--> |
| |