R
Rano
/*
Hello,
I've got some troubles with a stupid program...
In fact, I just start with the C language and sometime
I don't understand how I really have to use malloc.
I've readden the FAQ http://www.eskimo.com/~scs/C-faq/faq.html
but it doesn't seem to answer my questions...
So, I've made an example behind, with some included questions...
If you can tell me what's wrong
(In fact, i'me trying to write a chat server to see how C works.
And somewherre in my code something like my example behind. (It's
to know whose (BB are users) are connected in a room (AA are rooms)
and in which rooms (AA) is connected a user (BB). But when I test,
with about 6 users, I've got a "pointer" bug. My room "pointer name"
point to the user name... :| And I don't know why, because "for me"
everything is right ^^)
Sorry for my bad English
*/
#include <stdlib.h>
#include <string.h>
// ---------------- STRUCT ----------------
struct AAtmp
{
int name_length;
char * name;
int n;
struct BBtmp **bb;
};
struct BBtmp
{
int name_length;
char * name;
int n;
struct AAtmp **aa;
};
typedef struct AAtmp AA;
typedef struct BBtmp BB;
// ------------- FUNCTIONS --------------
AA *createAA(char *name, unsigned int name_length)
{
AA *a_tmp = malloc(sizeof(AA));
/*************************************************
Q1) Do you I have to malloc sizeof(AA) or sizeof(AA*) ? And Why ?
Because what I want is a AA*, but when I try to malloc sizeof(AA*)
the execution crash. (Freeze)
**************************************************/
memset(a_tmp, 0, sizeof(AA));
// The name
a_tmp->name_length = (name_length+1);
a_tmp->name = malloc(name_length+1);
memcpy(a_tmp->name, name, name_length+1);
// The BBs'array
a_tmp->n = 0;
a_tmp->bb = malloc(sizeof(BB **));
/************************************************
Q2) Do i have to malloc (sizeof(BB **) or sizeof(BB *)) ?
Both have a size of 4, but what is the most clean ?
If it's like the Q1), I suppose it's sizeof(BB *) no ?
************************************************/
return a_tmp;
}
BB *createBB(char *name, unsigned int name_length)
{
/*
same as AA but whith BB that comes AA
*/
BB *b_tmp = malloc(sizeof(BB));
memset(b_tmp, 0, sizeof(BB));
b_tmp->name_length = (name_length+1);
b_tmp->name = malloc(name_length+1);
memcpy(b_tmp->name, name, name_length+1);
b_tmp->n = 0;
b_tmp->aa = malloc(sizeof(AA **));
return b_tmp;
}
// ---------------- MAIN ----------------
int main()
{
int nbb, naa;
BB **BB_array;
AA **AA_array;
char *name;
nbb = 0;
naa = 0;
name = malloc(50);
BB_array = malloc(sizeof(BB **));
AA_array = malloc(sizeof(AA **));
/************************************************
Q3) Do i have to malloc these arrays ?
************************************************/
name = "Hello";
AA_array[naa++] = createAA(name, strlen(name));
name = "World";
BB_array[nbb++] = createBB(name, strlen(name));
AA_array[0]->bb[AA_array[0]->n] = BB_array[0];
/************************************************
Q4) Can I do that ? Or do I have to malloc (before) again and again
with something like :
AA_array[0]->bb[AA_array[0]->n] = malloc(sizeof(B *));
************************************************/
BB_array[0]->aa[BB_array[0]->n] = AA_array[0];
/************************************************
Q5) Can it be a problem to have what I do ? I mean, a pointer to a BB
in a AA that have got
itself a pointer to the BB (... that have got a pointer to the AA...)
************************************************/
return 0;
}
/*
Thanks
*/
Hello,
I've got some troubles with a stupid program...
In fact, I just start with the C language and sometime
I don't understand how I really have to use malloc.
I've readden the FAQ http://www.eskimo.com/~scs/C-faq/faq.html
but it doesn't seem to answer my questions...
So, I've made an example behind, with some included questions...
If you can tell me what's wrong
(In fact, i'me trying to write a chat server to see how C works.
And somewherre in my code something like my example behind. (It's
to know whose (BB are users) are connected in a room (AA are rooms)
and in which rooms (AA) is connected a user (BB). But when I test,
with about 6 users, I've got a "pointer" bug. My room "pointer name"
point to the user name... :| And I don't know why, because "for me"
everything is right ^^)
Sorry for my bad English
*/
#include <stdlib.h>
#include <string.h>
// ---------------- STRUCT ----------------
struct AAtmp
{
int name_length;
char * name;
int n;
struct BBtmp **bb;
};
struct BBtmp
{
int name_length;
char * name;
int n;
struct AAtmp **aa;
};
typedef struct AAtmp AA;
typedef struct BBtmp BB;
// ------------- FUNCTIONS --------------
AA *createAA(char *name, unsigned int name_length)
{
AA *a_tmp = malloc(sizeof(AA));
/*************************************************
Q1) Do you I have to malloc sizeof(AA) or sizeof(AA*) ? And Why ?
Because what I want is a AA*, but when I try to malloc sizeof(AA*)
the execution crash. (Freeze)
**************************************************/
memset(a_tmp, 0, sizeof(AA));
// The name
a_tmp->name_length = (name_length+1);
a_tmp->name = malloc(name_length+1);
memcpy(a_tmp->name, name, name_length+1);
// The BBs'array
a_tmp->n = 0;
a_tmp->bb = malloc(sizeof(BB **));
/************************************************
Q2) Do i have to malloc (sizeof(BB **) or sizeof(BB *)) ?
Both have a size of 4, but what is the most clean ?
If it's like the Q1), I suppose it's sizeof(BB *) no ?
************************************************/
return a_tmp;
}
BB *createBB(char *name, unsigned int name_length)
{
/*
same as AA but whith BB that comes AA
*/
BB *b_tmp = malloc(sizeof(BB));
memset(b_tmp, 0, sizeof(BB));
b_tmp->name_length = (name_length+1);
b_tmp->name = malloc(name_length+1);
memcpy(b_tmp->name, name, name_length+1);
b_tmp->n = 0;
b_tmp->aa = malloc(sizeof(AA **));
return b_tmp;
}
// ---------------- MAIN ----------------
int main()
{
int nbb, naa;
BB **BB_array;
AA **AA_array;
char *name;
nbb = 0;
naa = 0;
name = malloc(50);
BB_array = malloc(sizeof(BB **));
AA_array = malloc(sizeof(AA **));
/************************************************
Q3) Do i have to malloc these arrays ?
************************************************/
name = "Hello";
AA_array[naa++] = createAA(name, strlen(name));
name = "World";
BB_array[nbb++] = createBB(name, strlen(name));
AA_array[0]->bb[AA_array[0]->n] = BB_array[0];
/************************************************
Q4) Can I do that ? Or do I have to malloc (before) again and again
with something like :
AA_array[0]->bb[AA_array[0]->n] = malloc(sizeof(B *));
************************************************/
BB_array[0]->aa[BB_array[0]->n] = AA_array[0];
/************************************************
Q5) Can it be a problem to have what I do ? I mean, a pointer to a BB
in a AA that have got
itself a pointer to the BB (... that have got a pointer to the AA...)
************************************************/
return 0;
}
/*
Thanks
*/