
and I am not liking it one bit.
daily dose of the cube life from the world's best multi-slackers
memory leaks. wtf are they anyway...if you're those computer g33ks, you'd know right away its those memory that is still all over the place after you run a program in C/C++. most of the problems are with regards to the following:
#!/bin/bash
BIN=fake executable.bin
valgrind --leak-check=full --leak-resolution=high --log-file="actual executable" ./$BIN $*
Nodes::~Nodes()
{
delete []next;
}
Nodes::~Nodes()
{
//ADDED by XXX XXXXX 05/02/2007 XXXXXXX BEGIN
for (int mIdx_i = 0; mIdx_i < nLength; mIdx_i++)
{
if (next[mIdx_i] != 0)
{
delete ( next[mIdx_i] );
}
}
if (Data != NULL)
{
free (Data);
}
//ADDED by XXX XXXXX 05/02/2007 XXXXXXX END
delete []next;
}
1. With these variable declarations: int i, j[10], *k;
Which of the following are legal?
a.
i = *(&(j[2]) + 1);
b.k = &(j[1]);
c.i = &(j[2]) + 1;
2. Assuming the function lookupName is defined, what’s wrong with this code (hint: 2 bugs)?
const char *getName(const char *c) {
std::string name = lookupName(c);
if (name == NULL)
return "Anonymous";
return name.c_str();
}
int main(int argc, char *argv[]) {
const char *name = NULL, *c = NULL;
if (argc >= 2)
c = argv[1];
name = getName(c);
printf("My name is %s\n", name);
return 0;
}
3. What’s wrong with this program? If you were to fix it, what would the intended output be?
yes, I'm so geeky. but it looks fun. :)
void swap(char *str, int index1, int index2) {
char tmp = str[index1];
str[index1] = str[index2];
str[index2] = tmp;
}
int main(int argc, char *argv[]) {
char *planet1;
char *planet2;
planet1 = (char *) malloc(7 * sizeof(char));
if (!planet1)
return 0;
snprintf(planet1, 7, "Jupiter");
planet2 = "Saturn";
swap(planet1, 0, 3);
swap(planet2, 3, 4);
printf("results: %s and %s\n", planet1, planet2);
return 0;
}
Oh, give me a break!that made me laugh this morning.