L
lovecreatesbeauty
Hello experts,
1. Does C guarantee the data layout of the memory allocated by malloc
function on the heap. I mean, for example, if I allocate a array of 100
elements of structure, can I always reference a correct/valid structure
member upon that allocated memory?
If I allocate memory, for example like this way:
lp_Person = (person_t *)malloc(CNT * sizeof(person_t));
I guess the malloc/compiler doesn't know the detail of the data type
person_t. It only know the size of the object of the data type. So, how
can I use the members of the object of person_t (suppose it had
members) without compiler knowing it.
2. Is the base type of a pointer very important? I ever read one book,
it says "A pointer is not just a pointer, but a pointer with some
particular data type" (perhaps like this). Is it right?
3. If I allocate memory inside a function named f1(), is it better for
me to delay the de-allocation of that memory in some other functions
later?
Could you please also give me some more suggestion on my following
code, thank you very much.
lovecreatesbeauty
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#define NMLEN 30
typedef enum {
male = 1,
female = 2
} gender_t;
typedef struct {
char name[NMLEN];
gender_t gender;
int age;
} person_t;
typedef enum {
false = 0,
true = 1
} bool_t;
int NameListInitialization(void){
const int CNT = 100;
int ln_LoopCnt = 0;
person_t * lp_Person = NULL;
char la_PersonName[NMLEN] = {'\0'};
bool_t lb_IsAllocated = false;
lp_Person = (person_t *)malloc(CNT * sizeof(person_t));
if (lp_Person != NULL){
lb_IsAllocated = true;
for (ln_LoopCnt = 0; ln_LoopCnt < CNT ; ++ln_LoopCnt){
memset(la_PersonName, 0x00, sizeof(la_PersonName));
memset(lp_Person[ln_LoopCnt].name,
0x00,
sizeof(lp_Person[ln_LoopCnt].name));
itoa(ln_LoopCnt, la_PersonName, 10);
strcat(la_PersonName, "-Anonymous");
strcpy(lp_Person[ln_LoopCnt].name, la_PersonName);
lp_Person[ln_LoopCnt].gender = (gender_t)(
ln_LoopCnt % 2 + 1);
lp_Person[ln_LoopCnt].age = ln_LoopCnt;
}
}
/* do something here ... */
if (lb_IsAllocated == true){
free(lp_Person); /* Can I call this free in other place? */
}
return 0;
}
1. Does C guarantee the data layout of the memory allocated by malloc
function on the heap. I mean, for example, if I allocate a array of 100
elements of structure, can I always reference a correct/valid structure
member upon that allocated memory?
If I allocate memory, for example like this way:
lp_Person = (person_t *)malloc(CNT * sizeof(person_t));
I guess the malloc/compiler doesn't know the detail of the data type
person_t. It only know the size of the object of the data type. So, how
can I use the members of the object of person_t (suppose it had
members) without compiler knowing it.
2. Is the base type of a pointer very important? I ever read one book,
it says "A pointer is not just a pointer, but a pointer with some
particular data type" (perhaps like this). Is it right?
3. If I allocate memory inside a function named f1(), is it better for
me to delay the de-allocation of that memory in some other functions
later?
Could you please also give me some more suggestion on my following
code, thank you very much.
lovecreatesbeauty
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#define NMLEN 30
typedef enum {
male = 1,
female = 2
} gender_t;
typedef struct {
char name[NMLEN];
gender_t gender;
int age;
} person_t;
typedef enum {
false = 0,
true = 1
} bool_t;
int NameListInitialization(void){
const int CNT = 100;
int ln_LoopCnt = 0;
person_t * lp_Person = NULL;
char la_PersonName[NMLEN] = {'\0'};
bool_t lb_IsAllocated = false;
lp_Person = (person_t *)malloc(CNT * sizeof(person_t));
if (lp_Person != NULL){
lb_IsAllocated = true;
for (ln_LoopCnt = 0; ln_LoopCnt < CNT ; ++ln_LoopCnt){
memset(la_PersonName, 0x00, sizeof(la_PersonName));
memset(lp_Person[ln_LoopCnt].name,
0x00,
sizeof(lp_Person[ln_LoopCnt].name));
itoa(ln_LoopCnt, la_PersonName, 10);
strcat(la_PersonName, "-Anonymous");
strcpy(lp_Person[ln_LoopCnt].name, la_PersonName);
lp_Person[ln_LoopCnt].gender = (gender_t)(
ln_LoopCnt % 2 + 1);
lp_Person[ln_LoopCnt].age = ln_LoopCnt;
}
}
/* do something here ... */
if (lb_IsAllocated == true){
free(lp_Person); /* Can I call this free in other place? */
}
return 0;
}