1

I m Writing program for dynamic memory allocation. In this program i getting error of undefined symbol _msize. I have also include . Please help me with this.

/* Example of _msize */

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

void main()
{
 long *buffer;
 size_t size;

 buffer = (long *)malloc(100 * sizeof(long));
 size = _msize(buffer);
 printf("The size of the buffer is %d\n", size);
}
Jabberwocky
  • 40,411
  • 16
  • 50
  • 92
  • 1
    This is a non-portable thing from Microsoft. Are you using the MSVC compiler? – Mat Nov 24 '17 at 08:29
  • 1
    _msize() is not the standard function of C. It looks like home cooked stuff. You need to include the right header file in order to use that. Find _msize() in which header file it’s located. Side note: don’t use void main() that is not part of the C standard. – danglingpointer Nov 24 '17 at 08:29
  • @LethalProgrammer..In header file, this function is already there. I have included that one also. http://www.digitalmars.com/rtl/stdlib.html#_msize –  Nov 24 '17 at 08:32
  • Is the header file you use meant to be used for the environment you use for building? I.e. matching compiler, all needed libraries linked implicitly or explicitly. – Yunnosch Nov 24 '17 at 08:33
  • MSVC has `_msize` in `malloc.h` which you have included. – Weather Vane Nov 24 '17 at 08:34
  • @Mat..I m using turbo c++ compiler.. –  Nov 24 '17 at 08:42
  • 1. TurboC++ is an outdated antiquated product. 2. TurboC++ obviously doesn't have `_msize`. 3. `_msize` is not standard. 4. `_msize` isn't needed at all, you can do everything you want without it. 5. why is `_msize`not needed? Because _you_ allocated the buffer so you obviously know the size. – Jabberwocky Nov 24 '17 at 08:50
  • This is clearly a linker error and does not come from the compiler – Jens Gustedt Nov 24 '17 at 08:59

1 Answers1

1

The _msize is not Standard C.

It is from Microsoft Visual C++(MSVC), and you need to include <malloc.h>, as you already did, and use a Microsoft Compiler (e.g. within the Visual Studio).


PS: Unrelated to your problem: What should main() return in C and C++?

gsamaras
  • 66,800
  • 33
  • 152
  • 256