Allocating 10kb of memory and printing addresses and content [closed] - c

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I want to create a program which should allocate 10kb of memory and prints the memory addresses at groups of 4 bytes and its content. Here is what i want the output to be like:
0XAABBCCEB CDCDCDCD
0XAABBCCD8 FFA0B0C0
0XAABBCCD4 00FF00FF
0XAABBCCD0 00000000
I don't really know how to get to that output. I know that i need to use malloc and i know that i need to use the right operator to print it in hexadecimal form, but i don't know how to print the content and how to allocate exactly 10kb

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#define _10KB 10240
int main(){
int i;
uint32_t *arr;
if((arr = malloc(_10KB)) == NULL){
perror("malloc failed");
}
for(i = 0; i < (_10KB/4); i++){
printf("0x%p %.8x\n", &arr[i], arr[i]);
}
free(arr);
return 0;
}

Related

How does the code that prints array elements works? [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
This code prints the array elements, but I can't understand how does k[x-1] gives the array elements.
#include<stdio.h>
int main()
{
int x[]={2,4,6,8,10},k=1;
while (k<=5)
{
printf ("%3d",k[x-1]);
k++;
}
return 0;
}
Array indexes start at 0 in C. An array like int x[]={2,4,6,8,10} will have a value x[0]=2 and so forth. Typically, when iterating through an array, a convention like this is used:
for (int i = 0; i < length; i++)
printf("%3d",x[i]);
Since the code you provided begins the indexing at 1, you have to subtract one to fetch the proper element.

What does the following code segment do? [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
What does the following code segment do?
#include <stdio.h>
#include <stdlib.h>
int main(){
int num=0;
while(malloc(1<<10)) ++num;
}
First of all, 1<<10 is an expression that can be calculated at compile time, and it equals 1024. So your code is equivalent to
#include <stdlib.h>
int main()
{
int num=0;
while(malloc(1024)) ++num;
}
So what it does is to allocate chunks of 1024 bytes of memory until it fails to do so. Each time, the value of num is increased by one.
Overflowing the variable num will cause undefined behavior because it is signed. However, since you are not using the variable, it is likely to be optimized away.

Fill char *array from 0 to x [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 have a problem with a char *array in c (gcc linux)
on debian it works, but on another systems (yocto,raspbian) come a segmentation fault
The working Code in Debian:
char *myarray;
for (i=0;i<999;i++){
printf(myarray, "%i", i);
//do something with string to compare in file
}
But this Code fault on another Systems, i have tried to make a Array:
char *myarray[999]={"0","1","2"};
for (i=0;i<999;i++){
//do something with string[i] to compare in file
}
This Code also works but i dont like to fill a array from hand to "999"
I haven't found a method to make a char *string[arr] from "0"-"999" in a loop
Well , you can use sprintf-
char *array[1000];
for(int i=0;i<1000;i++){
array[i]=malloc(10*sizeof(**array)); //allocate memory to pointer
if(array[i]!=NULL){ //check return of malloc
sprintf(array[i],"%d",i);
}
}
Note- Just remember to free the allocated memory.
It's very unclear what you're after.
If you want to build an array holding the strings "0" through "999", you can do it using snprintf():
char array[1000][4]; /* Wastes some space, but not a great deal. */
for(int i = 0; i < 1000; ++i)
snprintf(array[i], sizeof array[i], "%d", i);
then you can print e.g. 452 like so:
printf("452 is %s\n", array[452]);

Is there any chance to store many variables without array? [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 7 years ago.
Improve this question
I wanna get some numbers from keyboard. But how to store that number without array[] ? Have i chance to do that ? I don't know exact how many numbers come from keyboard. If i had permission of array, its simple. But array is not allowed.
In your situation, I'd still go with arrays, but if you insist on using pointers, this code below will help you. Regardless of whether you need arrays or pointers, you still need to define an upper limit on how many elements can be stored in memory.
It is now up to you to modify the code to make it efficient and pretty to your assignment needs.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
int numelements=10;
int curelement=0;
int* data=calloc(1,numelements*sizeof(int));
int* p=data;
int* res=data;
while (curelement < numelements){
scanf("%d",p);
if (*p==0){break;} //exit if number entered is zero.
p++;
curelement++;
}
//print results
while(*res != 0){
printf("%d ",*res);
res++;
}
free(data);
return 0;
}

What is the correct way to use pointer of pointer in a function? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I use a function that displays a block of memory's content pointed by a pointer.
But didn't get the desired output, i am new to this, please correct my if i am wrong.
when I input size =3, element = 1,2,3 , I got output = 1 only.
Here's the code:
#include <stdio.h>
#include <stdlib.h>
void merge(int **arr1);
int main(void) {
int size1;
printf("Give me the size of first array\n");
scanf("%d", &size1);
int *arr1 = malloc(size1*sizeof(int));
int *p1=arr1;
printf("Give me the elements of first array\n");
int index1;
for(index1 = 0 ; index1<size1; index1++)
scanf("%d", p1++);
merge(&arr1);
return;
}
void merge(int **arr1) {
while(**arr1) //**arr1 is the content of the passed array, if there
// is an int in it, print that out and increment to next one
{
printf("%d", **arr1); // ** is the content and * is the address i think, right?
*arr1++;
}
}
Your merge() code expects the array to be terminated by zero. The calling code is not doing it, so the behavior is unspecified (I got segfault when I tried your code).
The other issue is that you should put parentheses around *arr1:
(*arr1)++;
When I run your code with this modification and enter zero for the last element, your code runs fine.

Resources