Archived

This topic is now archived and is closed to further replies.

BradDaBug

What's wrong with this?

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Read up on passing parameters by value. Or, just allocate the memory for somenum prior to passing it to FillItIn.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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;

}

Share this post


Link to post
Share on other sites

  
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
Release everything you allocate. It doesn''t matter whether the OS cleans up at the end or not - you''re essentially losing a valuable resource and eventually you may run out, causing your program, other programs, or even your OS to crash.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files | My stuff ]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Actually, I believe in Windows 9X, if you leak memory, it can''t be accessed by the OS until you restart.

I think....

Share this post


Link to post
Share on other sites