LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-21-2002, 04:15 PM   #1
raven
Member
 
Registered: Dec 2001
Location: Basel, Switzerland
Distribution: ubuntu
Posts: 297

Rep: Reputation: 31
Unhappy mess with some pointers...


hello

i am allocating a little memory block here, and there, and everywhere.

everything seems to be ok. I can allocate the memory i need (for a special struct):

typedef struct member_base
{
char * data;
int number;
struct member_base *next;
}

i can assign to every member of the structure the correct values i need to.
if in the same functuion i read from the appropriate addresses, i get all correct results. but, as soon as I lave the function, and want to access the char pointer in the struct, I get some messed up characters (8 characters).

why, oh why, do I loose the correct addresses?

has anyone a good idea?

thanks

raven
 
Old 06-21-2002, 08:25 PM   #2
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
I'd need to see a simple example. It sounds like your variables are going out of scope, but I need to see what and how you are doing it.
 
Old 06-22-2002, 03:04 PM   #3
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
If the pointer points to a local string, it may give such result.
 
Old 06-23-2002, 05:59 PM   #4
raven
Member
 
Registered: Dec 2001
Location: Basel, Switzerland
Distribution: ubuntu
Posts: 297

Original Poster
Rep: Reputation: 31
to crabboy:

if this is no problem, i will give you the link where you can get the package. the error appears in the queue_handler.c file (if you compile the program, you will get appropriate output) in the find_user function.

http://raven.eplay.ch/www/code/cbant...-1.0.1a.tar.gz

thanks

raven
 
Old 06-23-2002, 05:59 PM   #5
raven
Member
 
Registered: Dec 2001
Location: Basel, Switzerland
Distribution: ubuntu
Posts: 297

Original Poster
Rep: Reputation: 31
to mara:

OK, then this might be the proglem. how do I get around it?

thanks

raven
 
Old 06-24-2002, 10:18 AM   #6
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
You were not allocating any space for the strings in your structs. I changed the two char* in your structs to char data[50] and it works. You need to malloc space for the strings that you read in.


Code:
diff -C2 net2_bantumi/Makefile net2_bantumi2/Makefile
*** net2_bantumi/Makefile	Sun May 12 12:10:16 2002
--- net2_bantumi2/Makefile	Mon Jun 24 10:16:03 2002
***************
*** 2,12 ****
  
  all:
! 	$(CC) -c client.c
! 	$(CC) -c server_master.c
! 	$(CC) -c server_game.c
! 	$(CC) -c bantumi_common.c
! 	$(CC) -c queue_handler.c
! 	$(CC) -o client client.o bantumi_common.o
! 	$(CC) -o server server_master.o queue_handler.o bantumi_common.o server_game.o
  
  clean:
--- 2,12 ----
  
  all:
! 	$(CC) -g -c client.c
! 	$(CC) -g -c server_master.c
! 	$(CC) -g -c server_game.c
! 	$(CC) -g -c bantumi_common.c
! 	$(CC) -g -c queue_handler.c
! 	$(CC) -g -o client client.o bantumi_common.o
! 	$(CC) -g -o server server_master.o queue_handler.o bantumi_common.o server_game.o
  
  clean:
***************
*** 16,18 ****
  nice:
  	make all
! 	rm -f *.o
\ No newline at end of file
--- 16,18 ----
  nice:
  	make all
! 	rm -f *.o
diff -C2 net2_bantumi/bantumi.h net2_bantumi2/bantumi.h
*** net2_bantumi/bantumi.h	Sun Jun 16 19:39:06 2002
--- net2_bantumi2/bantumi.h	Mon Jun 24 10:58:16 2002
***************
*** 26,30 ****
  typedef struct base
  	{
! 	char *secret;
  	int conn_num;
  	struct base *next;
--- 26,30 ----
  typedef struct base
  	{
! 	char secret[50];
  	int conn_num;
  	struct base *next;
***************
*** 35,39 ****
  	int type;
  	int len;
! 	char *data;
  	}packet;
  
--- 35,39 ----
  	int type;
  	int len;
! 	char data[50];
  	}packet;
  
diff -C2 net2_bantumi/bantumi_common.c net2_bantumi2/bantumi_common.c
*** net2_bantumi/bantumi_common.c	Fri Jun 21 13:36:33 2002
--- net2_bantumi2/bantumi_common.c	Mon Jun 24 10:59:10 2002
***************
*** 109,113 ****
  		}
  	while(len>0);
! 	r_data->data=buffer_helper;
  	free(buffer);
  	free(buffer_helper);
--- 109,113 ----
  		}
  	while(len>0);
! 	strncpy( r_data->data,buffer_helper, 49 );
  	free(buffer);
  	free(buffer_helper);
Only in net2_bantumi2: client
diff -C2 net2_bantumi/queue_handler.c net2_bantumi2/queue_handler.c
*** net2_bantumi/queue_handler.c	Fri Jun 21 18:25:38 2002
--- net2_bantumi2/queue_handler.c	Mon Jun 24 10:39:26 2002
***************
*** 8,12 ****
  	list=(u_elem *)malloc(sizeof(u_elem));
  	list->conn_num=0;
! 	list->secret=NULL;
  	list->next=NULL;
  	return(list);
--- 8,12 ----
  	list=(u_elem *)malloc(sizeof(u_elem));
  	list->conn_num=0;
! 	strcpy(list->secret,"");
  	list->next=NULL;
  	return(list);
***************
*** 22,26 ****
  		}
  	new=(u_elem *)calloc(sizeof(u_elem),1);
! 	new->secret=secret;
  	new->conn_num=conn_num;
  	new->next=NULL;
--- 22,26 ----
  		}
  	new=(u_elem *)calloc(sizeof(u_elem),1);
! 	strncpy(new->secret,secret,49);
  	new->conn_num=conn_num;
  	new->next=NULL;
Only in net2_bantumi2: server

Output from server.c
Code:
waiting for connection on socket 3
listening on: 4
u_info received containing: type: 3 secret: hello

finding user
new user added: socket: 4 secret: hello

sending: . 0.29.please wait. you are in queue
writing complete
listening on: 5
u_info received containing: type: 3 secret: hello

finding user
comparing hello
 to hello
. more info: 4
removing
done.
playing: 4, 5
broadcasting boards contents...
rotating
sending: . 1.29.00404040404040004040404040400
writing complete
rotating
sending: . 1.29.00404040404040004040404040400
writing complete
broadcasting done...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
uninstalling the mess johnnydangerous Fedora 3 03-15-2005 06:52 AM
Kmess out what a mess ! dolphans1 Mandriva 0 08-21-2004 11:37 AM
Got Mess With Networking ikm19 Linux - Networking 6 12-28-2003 08:03 PM
gnometoaster mess jaan kaer Linux - Newbie 1 12-12-2003 03:08 AM
Conflict Mess nunativs Linux - Hardware 1 11-16-2003 09:10 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration