bug

Y

Your Uncle

/* begin port to c++ */

# ifdef __cplusplus
extern "C" {
# endif

int * get_an_int(void);
int * pass_pointer_triv(int *);


# ifdef __cplusplus
}
# endif

/* end port to c++ */

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int *rt, *qt;
qt = get_an_int();
rt = pass_pointer_triv(qt);
printf ("t is %d\n", *rt);
printf ("tja\n");
return 0;
}

int * get_an_int(void)
{
int t;
int * pt;
pt = malloc (sizeof*pt);
t = 41;
pt[0] = t;
return pt;
}

pass_pointer_triv(int *a)
{
int t;
t = *a;
printf("t is %d\n", t);
return a;
}
/* end source */
I believe that this source is one silly error away from being kosher c++.
My compiler tells me that the trivial pointer pass varies in level of
indirection, but to my eye, it all looks the same. I believe that my
compiler is correct. Grateful for any hints. furunculus
 
J

Jim Langston

Your Uncle said:
/* begin port to c++ */

# ifdef __cplusplus
extern "C" {
# endif

int * get_an_int(void);
int * pass_pointer_triv(int *);


# ifdef __cplusplus
}
# endif

/* end port to c++ */

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int *rt, *qt;
qt = get_an_int();
rt = pass_pointer_triv(qt);
printf ("t is %d\n", *rt);
printf ("tja\n");
return 0;
}

int * get_an_int(void)
{
int t;
int * pt;
pt = malloc (sizeof*pt);

My compiler complains about this.
error C2440: '=' : cannot convert from 'void *' to 'int *'

Either typecast to int * or use new (preferred) instead.
t = 41;
pt[0] = t;
return pt;
}

pass_pointer_triv(int *a)

You forgot the return type. Change this to
int* pass_pointer_triv(int *a)
and it compiles for me, except for the malloc error.
 
Y

Your Uncle

Jim Langston said:
My compiler complains about this.
error C2440: '=' : cannot convert from 'void *' to 'int *'

Either typecast to int * or use new (preferred) instead.
Please explain.
t = 41;
pt[0] = t;
return pt;
}

pass_pointer_triv(int *a)

You forgot the return type. Change this to
int* pass_pointer_triv(int *a)
and it compiles for me, except for the malloc error.
I think that's gonna get her going. Thanks for pretending like my problems
were your problems tonight. ciao, f
 
J

Jim Langston

Your Uncle said:
Please explain.

malloc returns a void* (void pointer). If you use it in c++ you need to
typecast it to the proper type either with reinterpret_cast or a c-style
cast. Either of these work, the reinterpert_cast prefered.

pt = reinterpret_cast<int*>( malloc( sizeof *pt ) );
pt = (int*) malloc( sizeof *pt );

but prefered is using new.

int *pt = new int;

Then just use delete to get rid of the memory instead of free (which you use
with malloc, but you are missing in your code so you have a memory leak)

t = 41;
pt[0] = t;
return pt;
}

pass_pointer_triv(int *a)

You forgot the return type. Change this to
int* pass_pointer_triv(int *a)
and it compiles for me, except for the malloc error.
I think that's gonna get her going. Thanks for pretending like my
problems were your problems tonight. ciao, f
 
Y

Your Uncle

Jim Langston said:
malloc returns a void* (void pointer). If you use it in c++ you need to
typecast it to the proper type either with reinterpret_cast or a c-style
cast. Either of these work, the reinterpert_cast prefered.

pt = reinterpret_cast<int*>( malloc( sizeof *pt ) );
pt = (int*) malloc( sizeof *pt );

but prefered is using new.

int *pt = new int;

Then just use delete to get rid of the memory instead of free (which you
use with malloc, but you are missing in your code so you have a memory
leak)
4 bytes unaccounted for? With resources such as they are, I think Keith
Thompson needs to write 42 volumes on it.

/* hello1.cpp contibutors: usual suspects */
/* begin port to c++ */

# ifdef __cplusplus
extern "C" {
# endif

int * get_an_int(void);
int * pass_pointer_triv(int *);


# ifdef __cplusplus
}
# endif

/* end port to c++ */

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int *rt, *qt;
qt = get_an_int();
rt = pass_pointer_triv(qt);
printf ("t is %d\n", *rt);
printf ("tja\n");
return 0;
}

int * get_an_int(void)
{
int t;
int * pt;
pt = malloc (sizeof*pt);
t = 41;
pt[0] = t;
return pt;
}

int * pass_pointer_triv(int *a)
{
int t;
t = (*a) ++;
printf("t is %d\n", t);
return a;
/* shut it down */
 
H

Howard

Your Uncle said:
4 bytes unaccounted for? With resources such as they are, I think Keith
Thompson needs to write 42 volumes on it.

Sarcasm?

You're free to ignore advice if you like, but then why come here asking for
help? You asked him to explain, and he did.
/* hello1.cpp contibutors: usual suspects */
/* begin port to c++ */

# ifdef __cplusplus
extern "C" {
# endif

int * get_an_int(void);
int * pass_pointer_triv(int *);


# ifdef __cplusplus
}
# endif

/* end port to c++ */

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int *rt, *qt;
qt = get_an_int();
rt = pass_pointer_triv(qt);
printf ("t is %d\n", *rt);
printf ("tja\n");
return 0;
}

int * get_an_int(void)
{
int t;
int * pt;
pt = malloc (sizeof*pt);

Speaking of ignoring advice... that still won't compile. Did you understand
the explanation you were given?

If you want "kosher" c++, you need to get rid of memory leaks.
t = 41;
pt[0] = t;
return pt;
}

int * pass_pointer_triv(int *a)
{
int t;
t = (*a) ++;
printf("t is %d\n", t);
return a;
/* shut it down */

?

Do you still have a question? It's not apparent exactly what you're
saying/asking in this latest response.

-Howard
 
Y

Your Uncle

Howard said:
Sarcasm?

You're free to ignore advice if you like, but then why come here asking
for help? You asked him to explain, and he did.
Yes, sarcasm. I ignore advice all the time. Pronouns usually refer to the
immediate antecedent.
Speaking of ignoring advice... that still won't compile. Did you
understand the explanation you were given?
I get no errors, no warnings. Are you certain that it just doesn't link?

If you want "kosher" c++, you need to get rid of memory leaks.
But, what if I WANT something to leak?
Do you still have a question? It's not apparent exactly what you're
saying/asking in this latest response.
I've exxagerated twice now since beginning to post here in clc++. The first
was when I intimated that I got a $260 ticket in Wisconsin. It was $230.
My other error was when I tried to quantify the size of this memory leak.
I'm still thinking about that one. I can't decide when I free it whether
I'm freeing just a pointer, or a pointer and an int. ciao, f
 
J

Jim Langston

Your Uncle said:
Yes, sarcasm. I ignore advice all the time. Pronouns usually refer to
the immediate antecedent.

I get no errors, no warnings. Are you certain that it just doesn't link?


But, what if I WANT something to leak?

I've exxagerated twice now since beginning to post here in clc++. The
first was when I intimated that I got a $260 ticket in Wisconsin. It was
$230. My other error was when I tried to quantify the size of this memory
leak. I'm still thinking about that one. I can't decide when I free it
whether I'm freeing just a pointer, or a pointer and an int. ciao, f

I would tell you, but then you would just come back with sarcasm. You're
one of the few I'm seriously considering ploinking.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top