By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,128 Members | 1,059 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,128 IT Pros & Developers. It's quick & easy.

bug

P: n/a


/* 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
Jun 21 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a

"Your Uncle" <in*****@crippled.net> wrote in message
news:44***********************@news.usenetmonster. com...


/* 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.
{
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

Jun 21 '06 #2

P: n/a

"Jim Langston" <ta*******@rocketmail.com> wrote in message
news:3p***************@fe05.lga...

"Your Uncle" <in*****@crippled.net> wrote in message
news:44***********************@news.usenetmonster. com...


/* 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.

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
Jun 21 '06 #3

P: n/a

"Your Uncle" <in*****@crippled.net> wrote in message
news:44***********************@news.usenetmonster. com...

"Jim Langston" <ta*******@rocketmail.com> wrote in message
news:3p***************@fe05.lga...

"Your Uncle" <in*****@crippled.net> wrote in message
news:44***********************@news.usenetmonster. com...


/* 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.

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

Jun 21 '06 #4

P: n/a

"Jim Langston" <ta*******@rocketmail.com> wrote in message
news:OJ***************@fe04.lga...

"Your Uncle" <in*****@crippled.net> wrote in message
news:44***********************@news.usenetmonster. com...

"Jim Langston" <ta*******@rocketmail.com> wrote in message
news:3p***************@fe05.lga...

"Your Uncle" <in*****@crippled.net> wrote in message
news:44***********************@news.usenetmonster. com...
Either typecast to int * or use new (preferred) instead.

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)

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 */
Jun 21 '06 #5

P: n/a

"Your Uncle" <in*****@crippled.net> wrote in message
news:44***********************@news.usenetmonster. com...
Either typecast to int * or use new (preferred) instead.
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)

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

Jun 21 '06 #6

P: n/a

"Howard" <al*****@hotmail.com> wrote in message
news:7K********************@bgtnsc05-news.ops.worldnet.att.net...

"Your Uncle" <in*****@crippled.net> wrote in message
news:44***********************@news.usenetmonster. com...
> Either typecast to int * or use new (preferred) instead.
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)

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.

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
Jun 21 '06 #7

P: n/a

"Your Uncle" <in*****@crippled.net> wrote in message
news:44**********************@news.usenetmonster.c om...

"Howard" <al*****@hotmail.com> wrote in message
news:7K********************@bgtnsc05-news.ops.worldnet.att.net...

"Your Uncle" <in*****@crippled.net> wrote in message
news:44***********************@news.usenetmonster. com...

>> Either typecast to int * or use new (preferred) instead.
> 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)
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.

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


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

This discussion thread is closed

Replies have been disabled for this discussion.