What's wrong with this?

Started by
10 comments, last by BradDaBug 21 years, 4 months ago
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.
  
#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);
}
  
Within FillItIn(), the value is right, but back in main(), its not. What''s going on?
I like the DARK layout!
Advertisement
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!
I like the DARK layout!
Or you could just use iostreams and pass by reference.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
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:


  #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;}  
The original poster is obviously using C and not C++. Use free() instead of delete.

  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.
"after many years of singularity, i'm still searching on the event horizon"
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.
That the OS will tidy unfreed resources after the application terminates does not mean that the application does not leak.
char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;}  /*** drpizza@battleaxe.net ***/
then could you clarify because now i''m a little unclear on leaking and OS garbage collection....

Beginner in Game Development?  Read here. And read here.

 

This topic is closed to new replies.

Advertisement