ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm writing a program which accept size of array as argument and than it should dynamically allocate memory for that size.
So far I've done that my program accept input and store it via 'atoi(argv[1])'. Next I create array via int* arr and with mmap store input number to it.
I want my program to return the size of memory which was allocated.
an array has many emblements, of different sizes for each element. Unlike a string, or type char that is a certain length , and only one, making it easy to get the size of the length and return its value in the form of an integer.
an array has many emblements, of different sizes for each element. Unlike a string, or type char that is a certain length , and only one, making it easy to get the size of the length and return its value in the form of an integer.
I don't know if you understand my questions due to my bad english or am I wrong with searching for solution on those links.
What I want to is to input like 100, and than array with 100 numbers should be made. Program should return how much of memory was allocated to program and not the size of array..
I don't know if you understand my questions due to my bad english or am I wrong with searching for solution on those links.
What I want to is to input like 100, and than array with 100 numbers should be made.
you'd have to figure it out.
1.how many elements you need
2.how to get what size each element needs to be.
then create them on the fly (dynamically)
using a loop maybe???
Quote:
Program should return how much of memory was allocated to program and not the size of array..
this code right here gives you what memory was allocated - sizeof(arr)/sizeof(int)
To determine the size of your array in bytes, you can use the sizeof operator:
int a[17];
int n = sizeof(a);
Quote:
The minimum size for char is 8 bits, the minimum size for short and int is 16 bits, for long it is 32 bits and long long must contain at least 64 bits.
Code:
8 bits per byte,
1024 bytes consists of 8192 bits
1 MB = 1,000,000 bits/bytes
#include <stdio.h>
#include <string.h>
int main(int argc, char** argv)
{
char buffer[10]; /* Array of 10 chars */
/* Copy at most 9 characters from argv[1] into buffer.
* sizeof (char) is defined to always be 1.
*/
strncpy(buffer, argv[1], sizeof buffer * sizeof buffer[0]);
/* Ensure that the buffer is null-terminated: */
buffer[sizeof buffer - 1] = '\0';
return 0;
}
Code:
10 char = 8 bits per char
80 bits of memory was used
#include <stdio.h>
#include <string.h>
int main(int argc, char** argv)
{
int i, szy;
int total;
const char * me = "hello";
szy=sizeof(argv);
for ( i = 0; i < sizeof(argv); i++) {
printf("Argv:\n");
/*
printf("Sizeof(argv): %d\n", sizeof(argv));
printf("length: %d\n", sizeof(argv)/sizeof(int));
printf("length-2: %d\n", sizeof(argv)/sizeof(argv[i]));
printf("length-3: %d\n", sizeof(argv[i+1])/sizeof(argv[i+1]));
printf("i= %d\n", i);
printf("sizeof argv[i]= %d\n", sizeof(argv[i]));
printf("char: %s\n", argv[i]);
printf("sizeof argv i: %d, %d\n", sizeof(argv[i]), i);
printf("\n");
printf("szy= %d\n", szy);
printf("\n");
printf("char size = %d\n", sizeof(me));
printf("%s,\n", me);
* */
total += sizeof(argv[i]);
printf("Total value = %d, i = %d\n", total , i );
}
printf("\n\n");
printf("Final Total value = %d, i = %d\n", total , i );
printf("sizeof szy = %d bytes\n", sizeof(szy));
printf("Sizeof i = %d bytes \n", sizeof(i));
printf("Sizeof me = %d bytes \n", sizeof(me));
total=total+sizeof(szy)+sizeof(i)+ sizeof(me);
printf("Total Mem used in program = %d\n", total);
return 0;
}
results
Code:
userx@voided1.what~/Public>> gcc testsize.c
userx@voided1.what~/Public>> ./a.out 1234567891011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
Argv:
Total value = 8, i = 0
Argv:
Total value = 16, i = 1
Argv:
Total value = 24, i = 2
Argv:
Total value = 32, i = 3
Argv:
Total value = 40, i = 4
Argv:
Total value = 48, i = 5
Argv:
Total value = 56, i = 6
Argv:
Total value = 64, i = 7
Final Total value = 64, i = 8
sizeof szy = 4 bytes
Sizeof i = 4 bytes
Sizeof me = 8 bytes
Total Mem used in program = 80
maybe check my code for math but.
Final Total value = 64, i = 8
so argv gets minimum 8 bits
const char * me = "hello"; gets 8 bits
int szy gets 16 bits even to show vrgv gets 8 bits
GCC wants mmap's return value to be cast as an (int*), otherwise your code seems to be working. To make it return the size of the array you just return sizeof(int) * input, unless mmap does something weird I don't know about (which is possible).
Edit: Also I assume in the mmap statement you should have input * sizeof(int). Also, when you say "size of memory allocated", are you allowed to assume what OS you're on? I'd venture a guess that they dole out chunks of memory rounded up to some multiple of a kilobyte or something, I'm just guessing here.
I'm curious why you can't use malloc, too. It's fine if you're working on homework or something, since you posted a minimum example and asked a basic question about a function, I'm just interested what the project is
Quote:
These two statements are contradictions.
This isn't quite true:
Quote:
From wikipedia:
Anonymous mapping maps an area of the process's virtual memory not backed by any file. The contents are initialized to zero.[3] In this respect an anonymous mapping is similar to malloc, and is used in some malloc(3) implementations for certain allocations. However, anonymous mappings are not part of the POSIX standard, though implemented by almost all operating systems by the MAP_ANONYMOUS and MAP_ANON flags.
The replies given so far cover the possible cases of the question as asked pretty well.
It would be helpful if you could restate your question more precisely to explain the actual problem being solved. Explaining why you cannot use malloc() seems to be an important constraint which has been omitted.
I'm writing a program which accept size of array as argument and than it should dynamically allocate memory for that size.
So far I've done that my program accept input and store it via 'atoi(argv[1])'. Next I create array via int* arr and with mmap store input number to it.
I want my program to return the size of memory which was allocated.
There are two errors - one already noted was that you have to cast the return value of mmap with (int *).
The second is that the mmap parameter input is the number of bytes you want, not the number integers you want. To get the correct size array you have to use input*sizeof(int) instead.
Note: this is not POSIX compliant so it may not work everywhere.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.