I am beginner to C programming. I have a defined astructure like the
following, and i am using aliases for the different data types in the
structure,
typedef struct _NAME_INFO {
struct _NAME_INFO *Next;
ULONG LastId;
ULONG Id;
PVOID Value;
BOOLEAN Used;
USHORT CodePage;
WCHAR *Name;
} NAME_INFO, *PNAME_INFO;
Why are you using all these aliases? If PVOID is supposed to be a
pointer to void, just use void*; if it isn't, the name PVOID is
misleading.
There's some debate as to whether typedefs for structs are a good
idea. Some argue that just using "struct foo" directly is clearer
than using a typedef name. A typedef hides the fact that the type is
a struct. There are cases where you want to do this (e.g., the type
FILE in <stdio.h>), but this isn't such a case.
The name _NAME_INFO is potentially a problem. Many identifiers
starting with an underscore are reserved to the implementation. (The
rule is a bit more complicated, and has to do with what character
follows the initial underscore, but it's best to avoid declaring
things with leading underscores altogether.)
If you're using a C99 compiler, you can use type "bool" if you have
"#include <stdbool.h>". Otherwise, you can define type "bool"
yourself. One approach is:
#if __STDC_VERSION__ >= 199901L
#include <stdbool.h>
#else
typedef int bool; /* or typedef char bool; */
#define false 0
#define true 1
#endif
So, given the above definition of "bool", and assuming I'm guessing
correctly what your aliases mean, and assuming "#include <stddef.h>"
for the definition of wchar_t, I'd declare your structure as follows:
struct Name_Info {
struct name_info *Next;
unsigned long LastId;
unsigned long Id;
void *Value;
bool Used;
unsigned short CodePage;
wchar_t *Name;
};
If you want to be able to refer to the type as "Name_Info" rather than
"struct Name_Info", you can do this:
typedef struct Name_Info {
/* member declarations as above */
} Name_Info;
Structure tags and typedef names are in separate namespaces, so
there's no need to use distinct identifiers. However, I probably
wouldn't bother with the typedef; instead, I'd use "struct Name_Info"
directly.
I definitely wouldn't create a typedef for a pointer to a struct
Name_Info. Hiding the fact that something is a pointer is likely to
cause confusion.
When I allocate memory to the defined structure, in the following way,
p = (PNAME_INFO)malloc(sizeof(PNAME_INFO)+ (wcslen(Name) + 1 ) *
sizeof( WCHAR ));
p->Name = (WCHAR *) malloc(( wcslen(Name) + 1 ) * sizeof( WCHAR ));
It core dumps when allocating memory to the pointer to wide character
*Name. Can somebody explain how to overcome this problem.
If you're allocating an object of type "struct Name_Info", you just
want to allocate the size of the struct. You could possibly allocate
a "struct Name_Info" and the wide string to be pointed to by the Name
member in a single chunk, but getting the alignment right would be
difficult -- and since you're allocating space for p->Name separately,
that's obviously not what you're trying to do anyway.
Casting the result of malloc() is unnecessary and can mask the error
of forgetting the "#include <stdlib.h>".
So here's how I'd do the allocations:
Name_Info *p;
p = malloc(sizeof *p);
if (p == NULL) {
/* error handling */
}
p->Name = malloc((wcslen(Name) + 1) * sizeof wchar_t);
if (p->Name == NULL) {
/* error handling */
}
In the first malloc(), note that the argument is "sizeof *p". By
referring to the pointer object and not to its type, we avoid problems
if the type is changed later on. Since the expression "*p" is the
operand of a sizeof, it's not evaluated; it's used only to determine
the size of the type p points to -- which is exactly what we want.
I'm assuming that the error handling code will bail out of whatever
context you're in, so you won't attempt to assign to p->Name unless
you know that the allocation for p has already succeeded.