Jump to content
  • Advertisement

Archived

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

Darkeye

Pointer dereferencing fun!

This topic is 6087 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

Ok, I lied in the subject, this isn''t fun at all. Basically, I need to pass a string to a function and allow that function to modify the string. At the moment, this is what I have: char *category; memset( &category, 0, sizeof( category ) ); That is the string I need to pass and modify in the other function. get_submitted_question( index, submit_filename, &category, etc etc. That is the call to the function that needs to modify the string. (The function takes a char** as the param) Then, in that function, whenever I try to modify the string by dereferencing it, for example: strcpy((*category), "Category"); The program crashes with: Unhandled exception at 0x004033eb in Quizbot.exe: 0xC0000005: Access violation writing location 0x00000000 Any ideas would be welcome.

Share this post


Link to post
Share on other sites
Advertisement
char *category isn''t really a string. its a pointer to a char (which could be a string, but wouldn''t work here).
try changing it to
char category[80];

Share this post


Link to post
Share on other sites
You have to make sure that the memory allocated to category is large enough to actually contain "Category" plus the terminating ''\0''.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
char *category;
memset( &category, 0, sizeof( category ) );

well all you have is a pointer
and when you call sizeof (category) this returns the size of the pointer which is like 4 bytes ( i believe), but your using &category, which is partially correct, but your zeroing out only first 4 bytes.


you need to do this

char* category
category = new char[MAXIUM_SIZE_EVER];
memset(category, 0, sizeof (category));

//then
strcpy(category, "Category");

this should work now
Pactuul

Share this post


Link to post
Share on other sites
char *category;
memset( &category, 0, sizeof( category ) );

well all you have is a pointer
and when you call sizeof (category) this returns the size of the pointer which is like 4 bytes ( i believe), but your using &category, which is partially correct, but your zeroing out only first 4 bytes.


you need to do this

char* category
category = new char[MAXIUM_SIZE_EVER];
memset(category, 0, sizeof (category));

//then
strcpy(category, "Category");

this should work now
Pactuul

Share this post


Link to post
Share on other sites
Access violation writing location 0x00000000

That''s a NULL pointer you''re trying to write on.

Share this post


Link to post
Share on other sites
Assuming he''s using C++, Null and Void.

Much the same is:

  
char *category;
category = malloc(sizeof(char) * size_wanted);
or, in c++
category = new char[size_wanted];
memset(category, 0, size_wanted);


You don''t need to change the strcpy line - it should be correct as it is (because category is a char** in get_submitted_question).

  
strcpy((*category), "Category");


All your bases belong to us (I know. It''s irony.)

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!