#include <stdio.h>
#include <stdlib.h>
void push(int p)
{
static int i=0;
int b;
int *ptr =(int *)malloc((10)*sizeof(int));
ptr[i]=p;
for(b=0;b<=i;b++){
printf("%d",ptr[b]);
printf("\n");
}
i++;
}
int main()
{
int a;
while(1)
{
scanf("%d",&a);
push(a);
}
}
When i put new values , function is not hold old entries.I wait for your helps.
#include <stdio.h>
#include <stdlib.h>
void push(int p)
{
static int i = 0;
static int ptr[10]; // since you are NOT freeing this memory later,
// there's no need to use malloc.
ptr[i] = p;
int b;
for (b = 0; b <= i; b++)
{
printf("ptr[%d] --> %d\n", b, ptr[b]);
}
printf("\n");
i++;
}
int main()
{
int a;
while(1)
{
scanf("%d",&a);
push(a);
}
return 0; // main() expects you to return something, remember?
}
Each time you call push(), new memory is allocated to hold 10 integers. You are not doing anything to save the pointer to that memory.
This is called a memory leak, because you're allocating memory but you never free it. If you call push() enough times, you could run out of memory.
You must allocate only once. At the moment you are creating a memory leak each time you call the function push. Nobody refers to the memory once you leave the function. You can make it static to keep the information. Be aware that you are also limiting the number of values that you can hold at 10.
void push(int p)
{
static int i=0;
static int *ptr =(int *)malloc((10)*sizeof(int)); // To keep the values
int b;
ptr[i]=p;
for(b=0;b<=i;b++){
printf("%d",ptr[b]);
printf("\n");
}
i++;
if( i >= 10 ) i = 0; // To make sure there is no overflow
}
Better yet you could pass in the location where you want to save the information.
void push(int p, int *ptr)
{
static int i=0;
int b;
ptr[i] = b;
for(b=0;b<=i;b++){
printf("%d",ptr[b]);
printf("\n");
}
i++;
if( i >= 10 ) i = 0; // To make sure there is no overflow
}
int main()
{
int a;
int values[10];
while(1)
{
scanf("%d",&a);
push(a, values);
}
}
Related
So I'm getting this message when I try to load my array using pointers.
I don't know why this keep appearing since the last program had no problem
#include<stdio.h>
#define T 10
void FLoad(int *);
void main () {
int a[T];
void FLoad(a);
}
void FLoad(int *a) {
int x;
for (x = 0; x < T; x++)
scanf("%d", a+x);
}
And here is a little program that works perfectly
#include <stdio.h>
void FImp(int *, int );
main () {
int a[] = {-10,-5,3,4}, tam;
tam = sizeof(a) / sizeof(int);
FImp(a, tam);
}
void FImp(int *a, int t) {
int x;
for (x = 0; x < t; x++)
printf("%d ",*(a + x));
putchar('\n');
}
You are using incorrect syntax when calling your function
void main()
{
int a[T];
void FLoad(a);
}
should be
void main()
{
int a[T];
FLoad(a);
}
or even better
int main(void)
{
int a[T];
FLoad(a);
}
You don't specify the function return value when you call it.
void FLoad(a);
This won't call the function. The compiler will consider this as a function declaration. So call the function without void it will work fine.
The function func takes void parameter buf. I want to insert values from 1 to 10 into buf by calling func. However, the addresses of buf are different in func and main and the printed values are not from 1 to 10. Can anyone help me with this problem? Thanks!
void func(int n, void *buf);
int main()
{
void *buf;
func(10,buf);
for(int i=0;i<10;i++){
printf("%d\n", ((char*)buf)[i]);
}
printf("in main, after func: %p\n",buf);
}
void func(int n, void *buf)
{
(char*)buf;
char my_array[n];
for(int i=0;i<n;i++){
my_array[i]=i;
}
buf = my_array;
printf("in func: %p\n",buf);
}
You set buf to a pointer that points to memory on the stack (i.e. my_array). Since it is no longer valid as soon as the program leaves func this does not work.
Either use dynamic memory, but then you need to free it explicitly. Another option is to create the memory outside and only fill it inside of func:
#include <stdio.h>
void func(char* buf, int n)
{
for(int i = 0; i < n; i++) {
buf[i]=i;
}
}
int main()
{
char buf[10];
func(buf, sizeof(buf));
for(int i=0; i < sizeof(buf); i++) {
printf("%d\n", buf[i]);
}
}
Apart from that: Never use void* unless you have a VERY good reason for it!
You need to allocate your memory outside of your function, otherwise it will not be valid when you return, because it was allocated on the stack.
void func(int n, int *buf);
int main()
{
const int n = 10;
int my_array[n];
func(n, my_array);
for(int i = 0; i < n; i++){
printf("%d\n", ((int*)my_array)[i]);
}
printf("in main, after func: %p\n", my_array);
}
void func(int n, int *buf)
{
for(int i = 0; i < n; i++){
buf[i] = i;
}
}
I have used the below code for a simple operation
(to reverse a string). But the program is not executing. It gets a run time error (SIGSEGV) . I used a GCC compiler. Please help me in debugging the program.
#include <stdio.h>
#include <stdlib.h>
int *create(int n) {
int *a;
a = (int *)malloc(n * sizeof(int));
return a;
}
void get(int *a, int n) {
int i;
for (i = 0; i < n; i++) {
scanf("%d", *(a + i));
}
}
void reverse(int *a, int n) {
int i;
for (i = n - 1; i >= 0; i--) {
printf("\n %d", *(a + i));
}
}
int main() {
int n, *a;
scanf("%d", &n);
a = create(n);
get(a, n);
reverse(a, n);
return 0;
}
scanf("%d",*(a+i)); invokes undefined behavior because you passed int where int* is expected.
You must pass pointer to tell scanf() where to store the data read, so stop dereferencing and try using scanf("%d",(a+i)); instead.
More notes are:
You should check if readings are successful.
They say you shouldn't cast the result of malloc() in C.
I'm stuck understanding a problem i have and i would appreciate your help.
I'm very new to C so please bear with me.
I have the following code:
#include <stdlib.h>
#include <stdio.h>
int h(int value){
if (value % 2 == 0){
// (***)
printf("Even");}
return 0;
}
void g(double value){
int i;
for(i=1; i<value; i++){
printf("%d", i);
}
h(value);
}
int f(int value){
static int sum;
sum += value;
printf("%d", sum + value);
g(sum);
return 0;
}
int main(){
int *p,
double *q;
p = (int *)malloc(10*sizeof(int));
q = (double *)malloc(10*sizeof(double));
f(1);
f(2);
f(3);
}
and i know how to find the output but now i'm being asked to draw how the memory looks at the place (***) in function h(). suppose that the stack starts at the address 100 and the length of return address is 4 bytes . i need to write on the stack the length of each cell containing a memory.
Can anyone teach me how to do this?
My program is as follows
#include<stdio.h>
int *intial(int);
int main (void)
{
int i, *b;
b=intial(5);
for(i=0;i<5;i++)
printf("%d\t",*(b+i));
getch();
}
int *intial(int t)
{
int i, *a;
for(i=0;i<t;i++)
a[i]=i;
return a;
}
But i am getting garbage values.
I also tried this
int *intial(int t)
{
int i, a[10];
for(i=0;i<t;i++)
a[i]=i;
return a;
}
But it is not working.
In order to work properly, your function should read
int *intial(int t)
{
int i;
int *a = malloc(t * sizeof(*a));
if (!a) return NULL; // error checking
for(i=0;i<t;i++) {
a[i]=i;
}
return a;
}
The "calling treaty" for this function is that the pointer returned is a malloc()ed one which the caller has the obligion for to free() it.
Of course, the caller as well should do proper error checking:
int main()
{
int i;
int *b;
b=intial(5);
if (!b) {
fprintf(stderr, "Malloc error.\n");
return 1;
}
for(i=0;i<5;i++) {
printf("%d\t",*(b+i)); // *(b+i) should be replaced with b[i] for readability
}
free(b); // free the memory
return 0;
}
You need to allocate memory for your array with malloc(). Otherwise a, and hence b, will not point to anything in particular.