Jump to content
  • Advertisement

Archived

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

BradDaBug

What's wrong with this?

This topic is 5752 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Guest Anonymous Poster
The original poster is obviously using C and not C++. Use free() instead of delete.

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
That the OS will tidy unfreed resources after the application terminates does not mean that the application does not leak.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!