#include "stdio.h"
int FillItIn(int *something, int value);
int main()
{
int *somenum;
FillItIn(somenum, 666);
printf("Value is %d!\n", *somenum);
return(0);
}
int FillItIn(int *something, int value)
{
something = (int*)malloc(sizeof(int));
memcpy(something, &value, sizeof(int));
printf("Value = %d\n", *something);
return(0);
}
What's wrong with this?
I don''t get it! It''s not working! I''m sure its something simple, but its driving me crazy, and I''m starting to pull out grey hairs.
Within FillItIn(), the value is right, but back in main(), its not. What''s going on?
Read up on passing parameters by value. Or, just allocate the memory for somenum prior to passing it to FillItIn.
Wait a sec! You mean when I pass an uninitialized pointer to a function that''s supposed to initialize it to something, I''m actually getting a pointer back that has been...wait, what?
Oh! You mean I have to have pass a pointer to a pointer to get initialized because the pointer itself is actually just some number (an address) that''s not actually changing when I expect it to, so I need a pointer to a pointer, so that I can send back the pointer to a pointer....ah. I think I get it!
Oh! You mean I have to have pass a pointer to a pointer to get initialized because the pointer itself is actually just some number (an address) that''s not actually changing when I expect it to, so I need a pointer to a pointer, so that I can send back the pointer to a pointer....ah. I think I get it!
In any case I wouldn''t run your program since it''s leaking memory. You have to delete the memory you allocated.
Here''s the code you provided with the correct implementation:
Here''s the code you provided with the correct implementation:
#include "stdio.h"int FillItIn( int* something, int value );void UnfillIt( int* something );int main(){ int* somenum; FillItIn( somenum, 666 ); printf( "Value is %d!\n", *somenum ); UnfillIt( somenum ); return( 0 );}int FillItIn( int* something, int value ){ something = new int; *something = value; printf( "Value = %d\n", *something ); return( 0 );}void UnfillIt( int* something ){ delete something;}
int FillItIn(int **something, int value){ *something = (int*)malloc(sizeof(int)); memcpy(*something, &value, sizeof(int)); printf("Value = %d\n", **something); return(0);}// in main()FillItIn(&somenum, 666);
... or use reference of a pointer to int.
Gladiator says: "In any case I wouldn''t run your program since it''s leaking memory. You have to delete the memory you allocated."
Nope. It doesn''t leak under Windows, MacOS X, or any UNIX-variant. When a program exits, it''s entire heap just disappears into the ether. Some people will argue that you should release all memory before just to be neat and pretty, but it doesn''t really matter.
Nope. It doesn''t leak under Windows, MacOS X, or any UNIX-variant. When a program exits, it''s entire heap just disappears into the ether. Some people will argue that you should release all memory before just to be neat and pretty, but it doesn''t really matter.
That the OS will tidy unfreed resources after the application terminates does not mean that the application does not leak.
then could you clarify because now i''m a little unclear on leaking and OS garbage collection....
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement