-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.

user44
  • 13
  • 1
  • 2
  • Change `sizeof(memblock)` to `sizeof(*memblock)` – Roman Zaitsev Nov 26 '15 at 10:02
  • 1
    @RomanZaytsev `sizeof(*memblock)` won't help here. – πάντα ῥεῖ Nov 26 '15 at 10:04
  • @πάντα ῥεῖ Can you elaborate please? I was thinking, that this case is equal to http://stackoverflow.com/a/37539/5247040 – Roman Zaitsev Nov 26 '15 at 10:09
  • 1
    @RomanZaytsev, calling `sizeof(*memblock)` will be similar to calling `sizeof(memblock[0])` which is to say `sizeof(char)`. – txtechhelp Nov 26 '15 at 10:13
  • @txtechhelp Got it, thank you – Roman Zaitsev Nov 26 '15 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. – πάντα ῥεῖ Nov 26 '15 at 10:14
  • Also note to the OP: calling `delete` on an uninitialized variable is undefined behavior (which could happen if the file is not opened and the array allocated via `new`) .. – txtechhelp Nov 26 '15 at 10:21
  • @RomanZaytsev I had tried the solution of your link but seems it is not able to show the length of it. And also,((char *)(&memblock+1) - (char *)memblock) / (sizeof(memblock[0])) <= it will show different answer each of time... I am not sure but maybe I get it wrong, I am just a newbie of c++ :( – user44 Nov 26 '15 at 15:57

2 Answers2

0

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

Marko Popovic
  • 3,999
  • 3
  • 22
  • 37
  • 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. – user44 Nov 29 '15 at 05:24
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";
user44
  • 13
  • 1
  • 2
  • 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 Nov 27 '15 at 23:00
  • @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. – user44 Nov 29 '15 at 04:59