-3
std::streampos size;
char * memblock;

std::ifstream input ("A.JPG", std::ios::in|std::ios::binary|std::ios::ate);

if (input.is_open())
{
    size = input.tellg();
    memblock = new char [size];
    input.seekg (0, std::ios::beg);
    input.read (memblock, size);
    input.close();

    std::cout << "[INPUT]the entire file content is in memory " << sizeof(memblock) << " \n";

}
delete[] memblock;

I would like to use the ifstream to read A.JPG (28KB) and save it into the array memblock. But why do the size of the memblock is 4 instead of 28403 while the variable size is equal to 28403?

Thank you.

8
  • Change sizeof(memblock) to sizeof(*memblock) Commented Nov 26, 2015 at 10:02
  • 1
    @RomanZaytsev sizeof(*memblock) won't help here. Commented Nov 26, 2015 at 10:04
  • @πάντα ῥεῖ Can you elaborate please? I was thinking, that this case is equal to stackoverflow.com/a/37539/5247040 Commented Nov 26, 2015 at 10:09
  • 1
    @RomanZaytsev, calling sizeof(*memblock) will be similar to calling sizeof(memblock[0]) which is to say sizeof(char). Commented Nov 26, 2015 at 10:13
  • 1
    @RomanZaytsev sizeof() cannot be used to determine the size of an array allocated at runtime, calculation is done at compile time . Your sample applies for statically allocated arrays. Commented Nov 26, 2015 at 10:14

2 Answers 2

0

Because memblock is a pointer, so the sizeof operator evaluates to the size of the pointer variable, which is 4 bytes.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer but I do not have enough reputation to +1 your answer :(. But anyway, your answer makes me to understand the reason.
0

Thanks all and finally I used the vector instead. Because seems it is hard to display the result I want (length of the actual char array)

std::vector <char> memblock(0);
  if (input.is_open())
  {
    size = input.tellg();
    //memblock = new char [size];
    memblock.resize(size);
    input.seekg (0, std::ios::beg);
    input.read (&memblock[0], size);
    input.close();


    //std::cout << "[INPUT]the entire file content is in memory " << ((char *)(&memblock+1) - (char *)memblock) / (sizeof(memblock[0])) << " \n";
    std::cout << "[INPUT]the entire file content is in memory " << memblock.size() << " \n";

2 Comments

There is nothing wrong with this version that uses a vector but this is not the answer to your original question. You said you want to read the jpeg into an array, you did not ask for suggestions regarding a different approach. Your question was: "why do the size of the memblock is 4 instead of 28403", and you were given a correct answer. Next time when someone gives you an answer, please mark it as accepted. After all, we are using a part of our spare time to help you.
@Marko Popovic Sorry for that, actually I still want to know how to get the length of it. And this is not the answer maybe, and I just wanna share the alternative way to achieve the same goal. I think my question is not good, I should ask How to get the length rather than "why". But anyway, really thanks for your comment and answer. I will mark it as answer, thank you.

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.