1

Syntax for std::fread :

size_t fread ( void * ptr, size_t size, size_t count, FILE * stream );

we can call std::fread with a character pointer and get the read data with the same character pointer. eg:

char* data;
fread(data,5,10,file);

What i tried is to create a similar function which accepts a character pointer as a void* pointer.

#include <iostream>
#include<string.h>

using namespace std;

void modify(void* ptr)
{
    char* temp = new char[50];
    strcpy(temp,"testing");
    __ptr=(void*)temp;
    std::cout<<"__ptr="<<(char*)__ptr<<endl;
}

int main()
{
    char* str;
    modify(str);
    cout<<"str="<<str;
    return 0;
}

I tried the above code with http://www.compileonline.com/compile_cpp11_online.php and I got the output as

__ptr=testing

str=

(a)Why str is not printing the string "testing"?

I am assigning the address of a dynamically allocated variable to ptr. So the value "testing" must be available in the heap even after the control returns from the method.

(b)How can i modify the function so that i will get the output as

__ptr=testing

str=testing

But function prototype cannot be modified.

Please help.

Hope the question is clear.

5
  • 2
    title doesn't match body, random pointer mistreatment gives random results. Not a useful question. Commented Feb 15, 2014 at 11:56
  • This code doesn't compile without warnings/errors (ideone.com/yrOcHS). Please fix that first, and then we can revisit. Commented Feb 15, 2014 at 11:58
  • this is basically C, if you want to do things the C++ way, you should use iterators and streams. Commented Feb 15, 2014 at 11:59
  • @MSalters..actually i am writing a wrapper function for fread function. I just shortened the problem with the help of a simple example. "random pointer mistreatment" can you please help me why you said like that? if you are right, then I think your answer may help me to solve the issue. :) Commented Feb 15, 2014 at 12:03
  • This won't even compile. there is no function that takes a reference to a char* to be called. If this compiles for you, kindly tell us what your toolchain is (and if it is TurboC++ I won't be a bit surprised at the crap it accepts). Commented Feb 15, 2014 at 12:09

2 Answers 2

4

The problem is that the function modify() takes a reference to a void* but you are actually passing a char*. This is not valid C++ and should not compile.

That said, there are some old C++ compilers out there (Visual C++ 6?) that have a kind of extension, that allows you to do that. That is when you are calling modify(str), the compiler is creating a temporary value of type void* and initializing it to the value of str. Then the temporary is passed to the function and taking the address of the allocated memory. But, obviously, the str original value is never modified, so it remains uninitialized.

That's the main reason why the original C++ rule forbidding this is there: to protect you from this kind of bugs.

You have two solutions:

  • Declare the function with the proper pointer type: void modify(char*& ptr).
  • Change the compiler!
  • If you really wanted the function as is you could call it with modify(reinterpret_cast<void*&>(str)) but I do not recommend that!

By the way, never name your identifier with two underscores (__ptr) these names are reserved for the compiler writers only·

Ah! I understand now the OP problem: his idea is to write a wrapper to fread that allocates memory, reads into it from a file and returns the pointer. It is easier if you just return the pointer the usual way. Using your modify() simplified example:

void* modify()
{
    char* temp = new char[50];
    strcpy(temp,"testing");
    return temp;    
}

And then, to call it:

char *ptr = static_cast<char*>(modify());
Sign up to request clarification or add additional context in comments.

10 Comments

So you think the compiler is creating a temp to pass as a non-const reference? Egads.
@WhozCraig: Yes, I think that VC++6 (and maybe later versions, I didn't use them much) used to allow that kind of code.
I know VC++ since at least 2008 supported non-const-references to return values of temps, but this (what you described) is Ugly with a capital U.
@WhozCraig: Look at the list of VC++ extensions. The other extensions are more or less sensible, but that one is madness!
@NidhinJoseph: fread() does not modify the pointer, it modifies the memory pointed by the pointer. These are two totally different things. Think of ptr = X vs. *ptr = X.
|
1

From the valuable answer and comments from @rodrigo, i was able to solve my problem. I modified the function.

void modify(void* ptr)
{
    char* temp;
    temp = (char *)ptr;
    strcpy(temp,"testing");
}

This solved all the issues!

1 Comment

If applied to the code which is currently in the question, this writes to a wild pointer, since str was never initialized to point anywhere.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.