Re: [Dev-C++] Allocation and deallocation
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Per W. <pw...@ia...> - 2007-05-10 14:16:53
|
Do not try sizeof(strlen(str));
That is a fixed value - the size of the data type returned by strlen
(normally size_t, if runtime library is compliant with the C standard).
You want to allocate (strlen(str)+1) bytes, i.e. one byte for each
character in the string, and an extra byte for the termination.
An easier way to allocate and assign at the same time is to do
name = strdup(str);
Also, you have mixed up data types.
If the list entries are struct list, then that is the data type that the
queue entry should have for it's 'front' pointer.
/pwm
On Thu, 10 May 2007, Jan Mura wrote:
> Hello,
>
> I have got two more things about the allocation of pointer types.
>
> I have structure like:
>
> struct list
> {
> int id;
> char *name;
> struct list *next;
> };
>
> struct list *element;
>
> Should now I do something like:
>
> element = malloc(sizeof(struct(list element));
> element->name = malloc(sizeof(strlen("some string here")));
> element->next = malloc(sizeof(struct list((;
>
> ...main program here...
>
> free(element->next);
> free(element->name);
>
> ?
>
> And the second thing, more about the alghoritgm of connected list or how is
> it called.
> The structure above defines an element of such a list. Where there is NULL
> in element->next it means it is the last element in the list. And I have
> another structure defined like:
>
> typedef struct Queue{
> struct bet_list *front;
> }Queue;
> Queue *q;
>
> I store an adress to the first element in the list in q.
> Is this correct? Or is there some more commone and easier way?
>
> Thank you again
>
> Jan Mura
> jan...@vo...
>
>
>
> -------------------------------------------------------------------------
> This SF.net email is sponsored by DB2 Express
> Download DB2 Express C - the FREE version of DB2 express and take
> control of your XML. No limits. Just data. Click to get it now.
> http://sourceforge.net/powerbar/db2/
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|