Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Can anybody tell me why this doesn't work
Message
From
03/08/2000 18:26:52
 
 
To
03/08/2000 15:39:04
General information
Forum:
Visual C++
Category:
Troubleshooting
Miscellaneous
Thread ID:
00400373
Message ID:
00400772
Views:
17
>Well, with the password that isn't a big deal, but that name in the program is really a filepath, so any ideas on that?

Matt,

Leave them as pointers, but allocate the memory for them.

Code sample

// Initialize required memory variables
int q ;
char* name = (char *)malloc(200) ;
char ch ;
char* password = (char *)malloc(20) ;
bool breturn = false ;

// Get values from the screen input
if (password != NULL )
{
puts("Password!");
}
printf("Password:");

if (name != NULL )
{
printf("Name:");
scanf("%s",name);
for (q=0;q<200 && !breturn;q++)
{
ch = getch();
if (ch =='\r')
{
breturn=true;
}
else
{
password[q] = ch;
}
}
}
cout << password << endl ;
cout << breturn << endl ;

free(password) ;
free(name) ;
}

malloc() allocates memory for a pointer to a memory space. By preallocating the memory, you control where things are stored in memory. This is both a strength and a weakness of C++ in that you have control over where you put things, but the downside is you pretty much have to control them. If you allocate memory, you must also free() it. If you neglect to free() the memory you've allocated, the system will allocate new memory spaces each time you hit the malloc() call. The result is a memory leak that if you were to go back and run the program over and over would eventually cause the heapstack to overload, and the system to crash.

I tested this and it ran without error. The prompts wheren't quite what you were after, but I'm sure you can work that little glitch out.

Good luck, hope this helped.

Regards,

Jason Tryon
Jason Tryon
Senior Systems Analyst / Technical Lead
eBusiness / iPage
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform