Skip to contentSkip to conversation

Some troubles with dynamic structures ;-(

DISCUSSION Alphex K. 2 participants 3 messages Jan 22, 2004 – Jan 23, 2004

lore
Share & tools
lore

Alphex K.

author
#0 lore raw

Hi, guys
I'm develop some program using gtk+ with dynamic structures

I'm have some problem with dynamic structures in thi code:
--
#include "oaptypes.h"

OapClient *clients;

OapClient * AddClient(OapClient * clientpointer,
int id,const char *nname,char *nmainsname, char *nemail,char *naddress,char *noffaddress, char *nicq,char *ninn,char *nphone,int nstatus, char *nnotes) // this function add a new structure
{
OapClient * tt=clientpointer;
// check and to dynamic structure (begin)
if (clientpointer!=NULL)

     {
	while (clientpointer->next!=NULL)
	  clientpointer=clientpointer->next;
	clientpointer->next=(struct OapClient  *) malloc(sizeof (OapClient));
	clientpointer=clientpointer->next;
	clientpointer->next=NULL;
	clientpointer->cid=id;
	/*debugl*/ printf("name is:%s\n",nname);
	clientpointer->name=nname;
	/*debugl*/ printf("in db name is:%s\n",clientpointer->name);
	clientpointer->mainsname=nmainsname;
	clientpointer->email=nemail;
	clientpointer->address=naddress;
	clientpointer->offaddress=noffaddress;
	clientpointer->icq=nicq;
	clientpointer->inn=ninn;
	clientpointer->phone=nphone;
	clientpointer->status=nstatus;
	clientpointer->notes=nnotes;
	return tt;
     }
   else
     {
	clientpointer=(struct OapClient  *) malloc(sizeof (OapClient));
	/*debug8*/ //clientpointer=malloc(4096);
	clientpointer->next=NULL;
	clientpointer->cid=id;
	clientpointer->name=nname;
	clientpointer->mainsname=nmainsname;
	clientpointer->email=nemail;
	clientpointer->address=naddress;
	clientpointer->offaddress=noffaddress;
	clientpointer->icq=nicq;
	clientpointer->inn=ninn;
	clientpointer->phone=nphone;
	clientpointer->status=nstatus;
	clientpointer->notes=nnotes;
	return clientpointer;
     }

// end
}

// interface functions section

void _AddClient(void) //this function add a new structure, variables from gtk face =) {
int astatus;
int aid;
char *aname;
char *amainsname;
char *aemail;
char *aaddress;
char *aoffaddress;
char *aicq;
char *ainn;
char *aphone;
char *anotes;

// get values from "add client" win aname=gtk_entry_get_text(GTK_ENTRY(namecentry)); /*debugh*/ printf("name from entry is:%s\n",aname); /*debug1*/ //sprintf(aname,"Default"); amainsname=gtk_entry_get_text(GTK_ENTRY(mainercentry)); aemail=gtk_entry_get_text(GTK_ENTRY(emailcentry)); aaddress=gtk_entry_get_text(GTK_ENTRY(physaddrentry)); aoffaddress=gtk_entry_get_text(GTK_ENTRY(offaddrentry)); aicq=gtk_entry_get_text(GTK_ENTRY(icqentry)); ainn=gtk_entry_get_text(GTK_ENTRY(innentry)); aphone=gtk_entry_get_text(GTK_ENTRY(phoneentry)); anotes=gtk_entry_get_text(GTK_ENTRY(notesentry)); // for debug only
astatus=0;
//for test only
aid=12;
// so and add values to the client db clients=AddClient(clients,aid,aname,amainsname,aemail,

		     aaddress,aoffaddress,aicq,ainn,aphone,
		     astatus,anotes);

/*debug1*/ //free(aname);
}

void _FillClientsTable(OapClient *pointer) // it read from dynamic structure and fill gtk_clist {
gchar *entr[4]; /*to fill*/

   // adding to table
   if (pointer!=NULL)
     {
     while(pointer!=NULL)
       {
	  entr[0]=malloc(4);
	  sprintf(entr[0],"%d",clients->cid);
	  entr[1]=malloc(4);
	  sprintf(entr[1],"%d",clients->status);
	   //entr[0]=pointer->icq;
	  //entr[1]=pointer->icq;
	  /*debugj*/ printf("DEBUG: in db ->%s\n",pointer->name);
	  entr[2]=(char *)pointer->name;
	  /*debugj*/ printf("DEBUG: in entr ->%s\n",entr[2]);
	  entr[3]=pointer->mainsname;
	  entr[4]=pointer->email;
	  gtk_clist_append(GTK_CLIST(clclist),entr);
	  pointer=pointer->next;
	  free(entr[0]);
	  free(entr[1]);
       }
     }

}
--
so and structure
typedef struct
{
int cid;
char *name;
char *mainsname;
char *email;
char *address;
char *offaddress;
char *icq;
char *inn;
char *phone;
int status;
char *notes;
struct OapClient *next;
} OapClient;

--
Where I have a mistake,
integers in structure work normally, but
I'm have a problem with pointers to char

thanx

Signature · 4 lines
Alphex Kaanoken
Senior developer of Crew IT research labs
web: http://crew.org.ru
mailto:[email protected]
#1 lore raw
Quoted 3 lines · Alphex K.

Where I have a mistake,
integers in structure work normally, but
I'm have a problem with pointers to char

The pointers are those returned from gtk_entry_get_text(), whose documention says:

Retrieve the contents of the entry widget. The returned pointer points to internally allocated storage in the widget and must not be freed, modified or stored. For this reason, this function is deprecated. Use gtk_editable_get_chars() instead.

I take this as meaning that the data to which they point may become invalid at the next operation to be performed on the GtkEntry (e.g. the next time it receives a signal or is accessed via one of the gtk_entry_* functions).

Use e.g. strdup() to copy the data, e.g.

   aname=strdup(gtk_entry_get_text(GTK_ENTRY(namecentry)));
Signature · 1 line
Glynn Clements <[email protected]>
author
#2 lore raw
Quoted 16 lines · On Fri, 23 Jan 2004 00:02:15 +0000 Glynn Clements <[email protected]>…
Quoted 3 lines · Alphex K.

Where I have a mistake,
integers in structure work normally, but
I'm have a problem with pointers to char

The pointers are those returned from gtk_entry_get_text(), whose documention says:

Retrieve the contents of the entry widget. The returned pointer points to internally allocated storage in the widget and must not be freed, modified or stored. For this reason, this function is deprecated. Use gtk_editable_get_chars() instead.

Quoted 8 lines

I take this as meaning that the data to which they point may become invalid at the next operation to be performed on the GtkEntry (e.g. the next time it receives a signal or is accessed via one of the gtk_entry_* functions).

Use e.g. strdup() to copy the data, e.g.

   aname=strdup(gtk_entry_get_text(GTK_ENTRY(namecentry)));

yes it's work normally, thanx

Quoted 7 lines
Signature · 6 lines
-- 
Glynn Clements <[email protected]>
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Signature · 4 lines
Alphex Kaanoken
Senior developer of Crew IT research labs
web: http://crew.org.ru
mailto:[email protected]