Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

hi all, heres a nuebie question

Status
Not open for further replies.

JaybOt

Programmer
Apr 18, 2001
101
GB
hi all,

heres a nuebie question for ya.. I'm trying to create infinite loop that continually assigns the results of some called functions to various variables, but i'm having a problem with re-assignment of the variables on the second loop? Basically I get a core dump!

Could anyone point me in the correct direction. Here is some pesudo code to show what i mean am doing...

main() {
int sentinal;
char *GetValue;
char * value;

sentinal = 1;
while (sentinal !=0) {
value = GetValue(); /* returns a char* */
printf("Value is %s\n", value);
sleep(10);
}
}

The loop does work, but core dumps on the second itteration. The problem is with the re-assignment of 'value'. How can i do this safely?
I have tried combos of malloc, calloc, setting value to "" but still core dumps. I am compiling with gcc under FreeBSD unix..


Any help appreciated.

Regards,
JayB0t [afro]


"Always know what you say, but don't always say what you know!"
 
hi,

I don't know what GetValue() is, but this errot is typical:

nuebie think that char* is a sort of STRING$ of basic
or CString of C++ : no . char* is h char pointer, a 16 or
32 bit variable, whose can contain adress of a storage
variable;

if you write
char x[64], x is still a pointer, but the storage area
follows variable.

you can define

char buffer[128] ;
char *p ; // buffer and p has different address:
// the value contained in p is not defined
// the 128 bytes of buffer, too.

than you can

p = buffer ; // now, p contains buffer address, but
// 128 byte can still be garbage.

gets( p ) // takes data from keyboard, and put
// them in the area pointed by p : the buffer

now you can printf(p) or printf(buffer) with the same result

bye
 
Thanks victorv!

I was not sure how to implement this at first and could not get things to work as i wanted, but then I tried setting up buffer like this..

buffer = (char*) calloc(128,sizeof(char*));

.. and got a result! Now i can do myVar = buffer; just before my assignment to effectively 'clear' the address space previously used by myVar.

I have been told this is good programming practice in 'C', that is, to use malloc and calloc to reserve space for vars. Bonus!

Regards,
J@yB0t[afro]

"Always know what you say, but don't always say what you know!"
 
hi,

you can
char *buffer
char *myVar = buffer ;

or directly use buffer as

buffer = (char*) calloc(128,sizeof(char*));
scanf( "%d", buffer ) ;
free(buffer);

or simply

char buffer[16];
scanf( "%d", buffer);

bye


 
If you're using C++, new and delete are generally preferable to malloc and friends.

If you do want to use calloc, you're allocating more space than you need. The call should be:

buffer = (char *) calloc(128, 1); /* or sizeof(char), which is always 1 */

Since you're allocating memory for 128 pointers to char not 128 pointers to pointers to char.

There should also be a corresponding free() call for every calloc, so your loop should look something like this:

while (sentinal !=0) {
value = GetValue(); /* returns a char* */
if (value != NULL) {
printf("Value is %s\n", value);
free(value);
sleep(10);
} else {
/* error recovery */
}
}

Incidentally, calloc shouldn't be necessary. Plain malloc should do just fine.

I noticed the usage of gets above. Never use gets because you can't place a limit on how many characters are read into your buffer causing a potential overflow; use fgets instead. Or, in C++, cin::getline.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top