Why VS Code, C build works but run code not? [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
This is my code. When i run the code it sends me an error:
helloworld.c:8:16: error: storage size of 'bin' isn't constant
static int bin[size];
but when i do a build the exe works. Help pls.

The initializer of a static variable must be known at compilation time. That's why the declaration of bin is rejected. Instead you need to declare bin as a pointer to an integer and allocate memory dynamically, that is at run-time:
int *bin = malloc(size * sizeof *bin);
As pointed out by rioV8, you should also free the allocated memory with free(bin) when you are done with bin.

Related

What is the special macro of buffer size in standard C library? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I saw this special macro when I read a source code. If I remember correctly, it is defined in the standard library.
The name of this macro is related to the buffer size, and in my machine its implementation is 1024.
Now I want to use it to initialize the buffer but I forgot what it is called.
So is there any one who can help me make my code look more professional?
If I don't know what I am looking for specifically, how can I clearly say what I need?
Are you talking about BUFSIZ? It's a macro provided by <stdio.h> and it expands to the size of the buffer used by setbuf().
I'm not sure what use it has in your own code.

What does "memory allocation" actually mean in C [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I was in an interview recently where I was given a piece of paper with a few function signatures and asked to fill in the code, I was also instructed not to "allocate memory".
The question was relatively simple (Smallest value in a list) so I solved it recursively which the interviewers seemed unimpressed by, they seemed to suggest that I could have declared variables on the stack but i was nervous and regrettably didn't press the interviewer on it.
What does it mean to "allocate" memory in C?
Allocate memory means you keep for your program an amount of memory situated in the HEAP section. On the contrary, when you don't allocate memory, new variables are stored in the STACK section.
See What and where are the stack and heap?

How could I access the memory of a running program? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Presuming I have a program in C:
#include<stdio.h>
#include<time.h>
int main()
{
int a = rand() % 1000;
return 0;
}
How can I find the value of "a" without printing it on the console, but by accessing, with a linux terminal command, its address to read the value from it?
Or generally, how can I map all the written values of a binary without seeing its implementation (like a "blackbox")?
Memory is accessible via /proc/pid/mem, index is /proc/pid/maps, Python prototype is how-do-i-read-from-proc-pid-mem-under-linux.

Displaying pointer's address in C, without using printf? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I need to recode the printf function using only a handful of functions from the libc library, namely write, malloc and free and a few other basic ones. The only formatting tab that I am having trouble with is %p as I have no idea how to display it using the write function, as passing the address of my pointer to a function that prints strings resulted in empty output. I have also tried typecasting it to a char * and unsigned char * but that did not work either. I have looked online but only found solutions invloving forbidden functions so I am not even sure where to begin.

Free a Unicode struct [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a pointer to an wide char array, which I want to free.
What is the easiest way to do this?
That depends on how the memory was allocated.
If malloc() was used, pass the address of the allocated memory to free(). If it came from a third-party library, see that library's documentation. If the struct is actually a local or global variable, don't worry about cleaning it up at all.

Resources