LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-23-2016, 07:22 AM   #1
SafetyMark
LQ Newbie
 
Registered: Oct 2016
Posts: 7

Rep: Reputation: Disabled
C - Size of allocated memory.


Hi,

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.

Thanks for your help.

Code:
...
input = (atoi(argv[1]));
int* arr = mmap (0, input, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, fd, 0);
...
 
Old 11-23-2016, 08:17 AM   #2
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
That's not what mmap() does.

Check out malloc().

Remember that you want to malloc( number_of_items * sizeof(the_array_element)).
 
Old 11-23-2016, 08:26 AM   #3
SafetyMark
LQ Newbie
 
Registered: Oct 2016
Posts: 7

Original Poster
Rep: Reputation: Disabled
What if I'm not able to use malloc?
 
1 members found this post helpful.
Old 11-23-2016, 08:57 AM   #4
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
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.

sizeof( .. )

check these links

http://stackoverflow.com/questions/3...-my-array-in-c

http://stackoverflow.com/questions/9...f-the-solution

http://www.geeksforgeeks.org/using-s...y-paratmeters/

Last edited by BW-userx; 11-23-2016 at 09:12 AM.
 
Old 11-23-2016, 09:14 AM   #5
SafetyMark
LQ Newbie
 
Registered: Oct 2016
Posts: 7

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by BW-userx View Post
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.

sizeof( .. )

check these links

http://stackoverflow.com/questions/3...-my-array-in-c

http://stackoverflow.com/questions/9...f-the-solution

http://www.geeksforgeeks.org/using-s...y-paratmeters/
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..
 
Old 11-23-2016, 09:30 AM   #6
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by SafetyMark View Post
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)

Code:
{
    int arr[]={1,2,3,4,5};
    clrscr();
    printf("Length: %d\n",sizeof(arr));
    printf("Length: %d\n",sizeof(arr)/sizeof(int));
    show(arr);
    getch();
}
void show(int ar[])
{
   printf("Length: %d", sizeof(ar));
   printf("Length: %d", sizeof(ar)/sizeof(int));
}
you need to know your data types to figure that out.
https://en.wikipedia.org/wiki/C_data_types

Quote:
Executive summary:

int a[17];
n = sizeof(a)/sizeof(a[0]);

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
sizeof

https://en.wikipedia.org/wiki/Sizeof

Code:
#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

Last edited by BW-userx; 11-23-2016 at 09:54 AM.
 
Old 11-23-2016, 10:57 AM   #7
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
char get 8 bits no matter what miminm size.

Code:
#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

Last edited by BW-userx; 11-23-2016 at 11:48 AM.
 
Old 11-23-2016, 11:26 AM   #8
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Member Response

Quote:
Originally Posted by SafetyMark View Post
I'm writing a program which accept size of array as argument and than it should dynamically allocate memory for that size.
Quote:
Originally Posted by SafetyMark View Post
What if I'm not able to use malloc?
These two statements are contradictions.

Why can't you use malloc()? If the memory is in a certain segment or mapped resource, it is important to point that out to people first.
 
1 members found this post helpful.
Old 11-23-2016, 11:44 AM   #9
crazy-yiuf
Member
 
Registered: Nov 2015
Distribution: Debian Sid
Posts: 119

Rep: Reputation: 51
Code:
int* arr = mmap (0, input, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, fd, 0);
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.

Edit again: here's more info on this:
https://stackoverflow.com/questions/...part-of-a-page

Rounding up to the page size should be easy to program. See also:
https://stackoverflow.com/questions/...ram-and-malloc

Last edited by crazy-yiuf; 11-23-2016 at 12:34 PM.
 
Old 11-23-2016, 11:49 AM   #10
crazy-yiuf
Member
 
Registered: Nov 2015
Distribution: Debian Sid
Posts: 119

Rep: Reputation: 51
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.
 
Old 11-23-2016, 12:01 PM   #11
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
It would be good to know what the actual problem/question is.
 
Old 11-23-2016, 02:32 PM   #12
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
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.
 
Old 11-23-2016, 04:21 PM   #13
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Quote:
Originally Posted by SafetyMark View Post
Hi,

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.

Thanks for your help.

Code:
...
input = (atoi(argv[1]));
int* arr = mmap (0, input, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, fd, 0);
...
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Memory map or physical address of some memory allocated by malloc demon007 Programming 1 02-04-2012 06:17 AM
memcached using more than allocated memory mahmoud Linux - Software 0 05-24-2011 10:15 AM
[SOLVED] Memory leak: How risky not to free allocated memory. kaz2100 Linux - General 1 12-24-2008 12:00 AM
[C] get allocated size Ephracis Programming 6 12-29-2006 09:55 PM
About allocated memory in C++ Ephracis Programming 2 12-28-2004 02:56 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration