Error message: undefined symbol _msize in C - c

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);
}

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++?

Related

How to dynamically allocate a multidimensional array in Visual Studio 2017 in C?

I started to use the method taught by Lundin here to dynamically allocate multidimensional arrays in C in Codeblocks. Now I switched to Visual Studio 2017 and I can't do it anymore, and I don't know how to do it. I have read many questions and answers and I google searched for the answer, but I still can't do it. Please help me to dynamically allocate a 2D array in Visual Studio. Here is the my code:
#define _CRT_SECURE_NO_DEPRECATE
#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
void arr_alloc(size_t x, size_t y, int(**aptr)[x][y])
{
*aptr = malloc(sizeof(int[x][y])); // allocate a true 2D array
assert(*aptr != NULL);
}
int main()
{
int n = 5;
int(*aptr)[n][n];
arr_alloc(n, n, &aptr);
return 0;
}
I will add a screenshot with the error messages and the code.

C - Global vs Local multidimensional array

When I execute this code (gcc compiled):
#include <stdio.h>
int main() {
int table[1005][1005];
return 0;
}
it stops working, but when I change it to:
#include <stdio.h>
int table[1005][1005];
int main() {
return 0;
}
it works just fine.. Why is this concretely happening? Does global variables get more space to allocate? Why?
First way is probably creating the array on the stack, the second is probably putting it into the "data segment".
The amount allocated may be too big for the stack depending on your platform.

strlen EXC_BAD_ACCESS in Xcode - c

This is my code:
#include <stdio.h>
#include <string.h>
int main() {
char x[256];
strcpy(x, "Gonzo!");
printf(strlen(x));
}
I'm not sure that I am coding this correctly. I want to know the string length of x.
I am using XCode 5+ and I get the error:
EXC_BAD_ACCESS(code=1, address=0x6)
The first argument of printf() is a const char*, which specifies the format. The posted code passes in 6 as the starting memory location for the char* which is incorrect and is causing the error. Use:
printf("%d\n", strlen(x));
See the linked printf() reference documentation.

Why does this code not compile?

Could anyone please explain why this code compiles :
#include <stdio.h>
#include <string.h>
int main (int argc, char *argv [])
{
FILE *ptr;
char string[10] = "Testing";
ptr = fopen("C:\\Users\\Jordan\\Desktop\\Hello.txt", "wb");
fwrite(string,sizeof(string[0]), sizeof(string)/sizeof(string[0]), ptr);
}
Yet this does not : Gives an Error C2065:'string' : undeclared identifer
#include <stdio.h>
#include <string.h>
int main (int argc, char *argv [])
{
FILE *ptr;
ptr = fopen("C:\\Users\\Jordan\\Desktop\\Hello.txt", "wb");
char string[10] = "Testing";
fwrite(string,sizeof(string[0]), sizeof(string)/sizeof(string[0]), ptr);
}
I am using Visual Studio 2010 on a Windows 7 Machine.
Thanks
Visual Studio uses the old C89/90 C. In that older C version, you can't mix declarations and code.
All your declarations must go on top. That's why the second example fails to compile.
// This a declaration
FILE *ptr;
// This is code
ptr = fopen("C:\\Users\\Jordan\\Desktop\\Hello.txt", "wb");
// This is another declaration. Not Allowed in C89/C90!!!
char string[10] = "Testing";
In (the C89 version of) C, all variables must be declared at the top of the block (the function, in this case). In your first example, you're doing that, in your second one you're not.
If you saved this file with a .c extension the compiler is interpreting it as a C source file, and since VC++ support for C is for C89, the C89 rules for variable declaration apply; in particular, in C89 you must declare all the local variables at the beginning of their block.

Fatal error 'stdio.h' not found

Why am I getting this message? The compiler is clang. Here is a simple program where it occurs for examples sake:
#include<stdio.h>
int fib(int);
int main()
{
int i;
scanf("%d",&i);
printf("The fibonacci number that is %i'th in the sequence is %i \n", i, fib(i));
return 0;
}
int fib(int n)
{
if (n==1 || n==0) return 1;
else return fib(n-1)+fib(n-2);
}
Assuming C
<stdio.h> is one of the standard C headers. Your compiler complains that it can not find this header. This means that your standard library is broken.
Consider reinstalling your compiler.
Assuming C++
<stdio.h> is the C standard header, with C++ we use <cstdio> instead. Though <stdio.h> is still required to exist in C++, so this probably isn't the problem.
Apart from these assumptions, it seems most likely (by your coding style and tags) that you are using C. Try this as some example code. This is guaranteed (by me) to compile on a working C compiler, if it doesn't then your compiler is horribly broken and you must install another one/reinstall:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv) {
printf("Hello World!\n");
return EXIT_SUCCESS;
}
I got a runtime error similar to this while using C++ in Visual Studio 2017. The solution in my case was to simply clean and rebuild the solution

Resources