LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 09-08-2002, 10:13 AM   #1
raven
Member
 
Registered: Dec 2001
Location: Basel, Switzerland
Distribution: ubuntu
Posts: 297

Rep: Reputation: 30
memory allocation.


hello

i have a function, that gets a few variables. one of the given variables is a pointer to a string.

prototype:

int add_user(u_elem *list, int conn_num, char *secret)

after executing tthe following line of code:

new=(u_elem *)calloc(sizeof(u_elem),1);

the value in the "secret" variable is lost. it is not just garbage, it is empty. how can I take care that the variable is not going to be cleared?

thanks

raven
 
Old 09-08-2002, 10:17 AM   #2
lackluster
Member
 
Registered: Apr 2002
Location: D.C - USA
Distribution: slackware-current
Posts: 488

Rep: Reputation: 30
IS that second line in the function? IN any case, I think you have the params of calloc backwards. And is new an array?
 
Old 09-08-2002, 10:45 AM   #3
raven
Member
 
Registered: Dec 2001
Location: Basel, Switzerland
Distribution: ubuntu
Posts: 297

Original Poster
Rep: Reputation: 30
new is not an array, new is a structure (u_elem).

it looks like this:

typedef struct base
{
char *secret;
int conn_num;
struct base *next;
}u_elem;

I also tryed to allocate memory with malloc instead of calloc, ant then it cut off the contents of the variable secret from the fourth caracter off.

and no, it is not exactly the second line, it is the first line after the variable declarations...

it looks like this:

int i;
u_elem *new;
new=(u_elem *)calloc(sizeof(u_elem),1); <--- this line is now with malloc

thanks

raven
 
Old 09-08-2002, 11:31 AM   #4
lackluster
Member
 
Registered: Apr 2002
Location: D.C - USA
Distribution: slackware-current
Posts: 488

Rep: Reputation: 30
what makes you think that secret should have a value? did you put a value in there to begin with? calloc & malloc init the values to 0 of all memory allocated. so unless you strcpy a value into secret, you'll be missing a value.

AFAIK calloc just returns aligned allocated memory blocks. since you have types of different sizes, you should use malloc

Last edited by lackluster; 09-08-2002 at 11:34 AM.
 
Old 09-08-2002, 11:48 AM   #5
sarin
Member
 
Registered: May 2001
Location: India, Kerala, Thrissur
Distribution: FC 7-10
Posts: 354
Blog Entries: 2

Rep: Reputation: 34
secret is a char*. Before using that I think you should allocate memory for the string that you are going to put there. Else it will have only 4 bytes which the memory allocated for char*.
--Sarin
 
Old 09-08-2002, 01:50 PM   #6
raven
Member
 
Registered: Dec 2001
Location: Basel, Switzerland
Distribution: ubuntu
Posts: 297

Original Poster
Rep: Reputation: 30
well, secret has a value, i explicitly give one when calling the function. secret should be set. definitely.

and: ther is memory being allocated for secret. at least in the function where secret is first set. afterwards, I only set pointers. so it should work.

anyway, thanks for the help, I solved one problem, now I am having another. so, see you later in another thread :-)

raven
 
  


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
memory allocation esael Linux - General 7 01-12-2008 12:12 PM
memory allocation gecoool Programming 2 10-24-2005 09:47 AM
Help - memory allocation in C zaichik Programming 3 09-04-2005 10:16 AM
memory allocation docGonzo2000 Linux - General 1 05-16-2003 09:24 PM
memory allocation docGonzo2000 Linux - General 1 05-16-2003 09:22 PM


All times are GMT -5. The time now is 09:39 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration