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?
Related
The first codeļ¼
#include <stdio.h>
int *func() {
int n = 100;
return &n;
}
int main() {
int *p = func(), n;
n = *p;
printf("value=%d\n", n);
return 0;
}
output:
value=100
The second code:
#include <stdio.h>
int *func() {
int n = 100;
return &n;
}
int main() {
int *p = func(), n;
printf("c is an interesting language!\n");
n = *p;
printf("value=%d\n", n);
return 0;
}
output:
c is an interesting language!
value=30
I use the debugging program and found that the printf function changed the value of n. What operation does it do inside? If anyone could give me a detailed answer, I would be grateful!
This question already has answers here:
Why is it safer to use sizeof(*pointer) in malloc
(3 answers)
What happens if I use malloc with the wrong size?
(4 answers)
Closed 2 years ago.
Here is my code with the print statement followed by its output.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int *myF(int a[],int s, int n);
int *myFunc(int x[],int size,int nnum) {
int i;
size += 1;
int *y = malloc(size);
for(i=0;i<size;i++) {
if(i==0)
*y = nnum;
else {
*(y+i) = x[i-1];
printf(" %d",*(y+i)); // <-- ***THIS THING RIGHT HERE***
}
}
return y;
}
int main (int argc, char *argv[]) {
int i;
int x[7] = {1,2,3,4,5,6,7};
int *P = myFunc(x,7,12);
for(i=0;i<8;i++) {
if(i==0) printf("\n");
printf(" %d",*(P+i));
}
return 0;
}
OUTPUT:
1 2 3 4 5 6 7
12 1 2 3 4 5 6 7
(The second matrix is what I want the code to output)
Here is my code without the print statement followed by its output.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int *myF(int a[],int s, int n);
int *myFunc(int x[],int size,int nnum) {
int i;
size += 1;
int *y = malloc(size);
for(i=0;i<size;i++) {
if(i==0)
*y = nnum;
else {
*(y+i) = x[i-1];
// printf(" %d",*(y+i)); <-- ***THIS THING RIGHT HERE*** commented out
}
}
return y;
}
int main (int argc, char *argv[]) {
int i;
int x[7] = {1,2,3,4,5,6,7};
int *P = myFunc(x,7,12);
for(i=0;i<8;i++) {
if(i==0) printf("\n");
printf(" %d",*(P+i));
}
return 0;
}
OUTPUT:
12 1 2 3 4 5 83 0
(This is not what I want...)
Can somebody please explain where this code is pulling 83 and 0 for the last two elements in the array just because I chose to include or exclude a random print statement?
Any help would be greatly appreciated because I can't understand how C is pulling numbers out of thin air like this.
Your malloc size is not correct and causes undefined behavior.
The actual memory size is the length of array * size of array type.
The printf in main is actually printing something random in the memory.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int *myF(int a[],int s, int n);
int *myFunc(int x[],int size,int nnum) {
int i;
size += 1;
//int *y = malloc(size * sizeof(int) );
int *y = malloc(size );
for(i=0;i<size;i++) {
if(i==0)
*y = nnum;
else {
*(y+i) = x[i-1];
printf(" %d",*(y+i)); // <-- ***THIS THING RIGHT HERE***
}
}
return y;
}
int main (int argc, char *argv[]) {
int i;
int x[7] = {1,2,3,4,5,6,7};
int *P = myFunc(x,7,12);
for(i=0;i<8;i++) {
if(i==0) printf("\n");
printf(" %d",*(P+i));
}
free(P);
return 0;
}
If you're interesting in why printf changed the behavior, this post has in-depth explanation.
Also don't forget to free P in the end.
This question already has answers here:
How do I return multiple values from a function in C?
(8 answers)
Closed 3 years ago.
I want to try write Kadane algorithms using C. Instead of just return maximum subarray value, I also want to return the start and end index.
And here's the code :
#include <limits.h>
#include <stdio.h>
int kadane(int A[], int size){
int current_max = INT_MIN;
int global_max = INT_MIN;
int start, last;
for (int i = 0; i <= size; i++){
if (A[i] > A[i] + current_max){
current_max = A[i];
start = i;
} else {
current_max += A[i];
};
if (current_max >= global_max){
global_max = current_max;
last = i;
};
};
return (start, last ,global_max);
}
int main(){
int sum, size, start, last;
int A[] = {3,-4,5,1,9,-10,11,2,5,-1,2};
size = sizeof(A)/sizeof(A[0]);
start, last, sum = kadane(A, size-1);
printf("start at %d ; end at %d; sum : %d\n", start, last, sum);
return 0;
}
Although the answer of maximum sum is right, the value of start and last are really weird. I use printf to check the value of start and last in the for loop of kadane function, and it seems work fine there. So I thought the problem might have something to do with the way I return variable.
so I modify some part of code like this :
int kadane(int A[], int size){
......
......
return (&start, &last ,global_max);
}
and then using pointer to store them :
int main(){
.......
.......
int *start;
int *last;
*start, *last, sum = kadane(A, size-1);
printf("start at %d ; end at %d; sum : %d\n", *start, *last, sum);
return 0;
}
then I got "Segmentation Fault 11" error.
I try to understand and search what I'm doing wrong here but I just can't find it.The only solution that work is to store variable start and last in global so that I can just call then anywhere without return.But I feel like that is not a suitable solution. Can anyone help me here?
You have two solutions:
1: Return a struct which contains all the types you need.
struct data {
int start;
int last;
int global_max;
};
struct data kadane(int A[], int size){
stuct data test = { 1, 1, 1 };
......
......
return test;
}
void main() {
......
struct data t = kadane(A, ize);
}
2: Use pointers to pass out values.
void kadane(int A[], int size, int *start, int *last, int *global_max) {
......
......
*start = 1;
*last= 1;
*global_max= 1;
}
void main() {
......
int a, b, c;
kadane(A, size, &a, &b, &c);
}
From
How do I return multiple values from a function in C?
I am doing a simple minheap program but when size of heap is 4 i get an error program stopped working.
i checked heap size 2,3,5,6,7 program is working fine.
Why am i getting this error only when heapsize is 4?
I am using codeblocks 16.01, windows 10, gcc compiler.
minHeap.c
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
void MinHeapfy(int A[], int i, int N)
{
int l,r,temp,smallest;
l = 2*i;
r = 2*i + 1;
if((l<=N) && (A[l]<A[i]))
{
smallest = l;
}
else
{
smallest = i;
}
if((r<=N) && (A[r]<A[smallest]))
{
smallest = r;
}
if(smallest != i)
{
temp = A[smallest];
A[smallest] = A[i];
A[i] = temp;
MinHeapfy(A,smallest,N);
}
}
void BuildHeap(int A[],int N)
{
int i;
int f = floor(N/2);
printf("f %d", f);
for(i=f;i>=1;i--)
{
printf("i %d\n",i);
MinHeapfy(A,i,N);
}
}
int main()
{
int *A,N,T,data,q,i=1;
scanf("%d",&N);
A = (int *)calloc(N,sizeof(int));
for(i=1;i<=N;i++)
{
scanf("%d",&data);
A[i]=data;
}
BuildHeap(A,N);
for(i=1;i<=N;i++)
{
printf("%d ",A[i]);
}
return 0;
}
I can share some things that I learned till now:
void BuildHeap(int *A, int N);
void MinHeapfy(int *A, int i, int N);
In this 2 functions you just send a pointer not an array(difference between int *A and int A[5])
int main()
{
/*here it is better for you to declare like this
to see more easier which is which pointer and variable
(PS: at the moment you don't use q)*/
int N,T,data,i;//,q
int *A;
scanf("%d",&N);
A = (int *)calloc(N,sizeof(int));
for(i=0;i<N;i++)
{
scanf("%d",&data);
A[i]=data;
}
BuildHeap(A,N);
for(i=0;i<N;i++)
{
printf("%d ",A[i]);
}
return 0;
}
The 2 fors that you use must start from 0 to N
When you write:
A[0] <=> address pointed by A + 0 * sizeof(data type of A)
So 0 is the first address of the array
and the last address is N-1
A[N-1] <=> address pointed by A + (N-1) * sizeof(data type of A)
#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);
}
}