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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
09-08-2002, 10:13 AM
|
#1
|
|
Member
Registered: Dec 2001
Location: Basel, Switzerland
Distribution: ubuntu
Posts: 297
Rep:
|
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
|
|
|
|
09-08-2002, 10:17 AM
|
#2
|
|
Member
Registered: Apr 2002
Location: D.C - USA
Distribution: slackware-current
Posts: 488
Rep:
|
IS that second line in the function? IN any case, I think you have the params of calloc backwards. And is new an array?
|
|
|
|
09-08-2002, 10:45 AM
|
#3
|
|
Member
Registered: Dec 2001
Location: Basel, Switzerland
Distribution: ubuntu
Posts: 297
Original Poster
Rep:
|
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
|
|
|
|
09-08-2002, 11:31 AM
|
#4
|
|
Member
Registered: Apr 2002
Location: D.C - USA
Distribution: slackware-current
Posts: 488
Rep:
|
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.
|
|
|
|
09-08-2002, 11:48 AM
|
#5
|
|
Member
Registered: May 2001
Location: India, Kerala, Thrissur
Distribution: FC 7-10
Posts: 351
Rep:
|
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
|
|
|
|
09-08-2002, 01:50 PM
|
#6
|
|
Member
Registered: Dec 2001
Location: Basel, Switzerland
Distribution: ubuntu
Posts: 297
Original Poster
Rep:
|
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
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 10:45 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|