public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* memory allocation
@ 1999-11-04  2:15 Anand Ganesh
  1999-11-04  3:00 ` Martin Kahlert
                   ` (2 more replies)
  0 siblings, 3 replies; 11+ messages in thread
From: Anand Ganesh @ 1999-11-04  2:15 UTC (permalink / raw)
  To: help-gcc

Hi,
	Why doesnt gcc crib when I make an array declaration that it cant
handle ?
eg
float array[10000][5][5];

That there is a problem I realised b'cos some unrelated variables were
getting changed. The reason I declared such an array instead of mallocing
is that it is easier to pass 3D arrays rather than float*** which you cant
use as a 3D array anymore (since dimensions are not known).

Is there a way to get a warning from gcc ? (I did gcc -Wall)

Thanks in advance for any help,
ag.

	


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: memory allocation
  1999-11-04  2:15 memory allocation Anand Ganesh
@ 1999-11-04  3:00 ` Martin Kahlert
  1999-11-30 23:28   ` Martin Kahlert
  1999-11-04  6:16 ` Arthur Gold
  1999-11-30 23:28 ` Anand Ganesh
  2 siblings, 1 reply; 11+ messages in thread
From: Martin Kahlert @ 1999-11-04  3:00 UTC (permalink / raw)
  To: help-gcc

In article < Pine.SGI.4.10.9911040409470.31041-100000@whirlwind.cs.utexas.edu >,
	Anand Ganesh <anandg@cs.utexas.edu> writes:
> Hi,
> 	Why doesnt gcc crib when I make an array declaration that it cant
> handle ?
> eg
> float array[10000][5][5];
> 
> That there is a problem I realised b'cos some unrelated variables were
> getting changed. The reason I declared such an array instead of mallocing
> is that it is easier to pass 3D arrays rather than float*** which you cant
> use as a 3D array anymore (since dimensions are not known).
Then you will probably do out of bound writes inside your code.

array is just 5*5*10000*4=1MB in size.
There shouldn't be any problem with that.

If you get a segfault, then try the shell command limit
(i assume you are on Unix-shells here)

Hope, that helps,
Martin.

-- 
The early bird gets the worm. If you want something else for       
breakfast, get up later.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: memory allocation
  1999-11-04  2:15 memory allocation Anand Ganesh
  1999-11-04  3:00 ` Martin Kahlert
@ 1999-11-04  6:16 ` Arthur Gold
  1999-11-05 14:01   ` Anand Ganesh
  1999-11-30 23:28   ` Arthur Gold
  1999-11-30 23:28 ` Anand Ganesh
  2 siblings, 2 replies; 11+ messages in thread
From: Arthur Gold @ 1999-11-04  6:16 UTC (permalink / raw)
  To: help-gcc

What do you mean by "can't handle"?
I suspect there's something else going on in your code.

Have you run your program through gdb?
HTH,
--ag

Anand Ganesh wrote:
> 
> Hi,
>         Why doesnt gcc crib when I make an array declaration that it cant
> handle ?
> eg
> float array[10000][5][5];
> 
> That there is a problem I realised b'cos some unrelated variables were
> getting changed. The reason I declared such an array instead of mallocing
> is that it is easier to pass 3D arrays rather than float*** which you cant
> use as a 3D array anymore (since dimensions are not known).
> 
> Is there a way to get a warning from gcc ? (I did gcc -Wall)
> 
> Thanks in advance for any help,
> ag.
> 
> 

-- 
Artie Gold, Austin, TX
mailto:agold@bga.com or mailto:agold@cs.utexas.edu
--
"If you come to a fork in the road, take it." L. P. Berra

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: memory allocation
  1999-11-04  6:16 ` Arthur Gold
@ 1999-11-05 14:01   ` Anand Ganesh
  1999-11-30 23:28     ` Anand Ganesh
  1999-11-30 23:28   ` Arthur Gold
  1 sibling, 1 reply; 11+ messages in thread
From: Anand Ganesh @ 1999-11-05 14:01 UTC (permalink / raw)
  To: help-gcc

On Thu, 4 Nov 1999, Arthur Gold wrote:

> What do you mean by "can't handle"?
> I suspect there's something else going on in your code.

- I mean that enough memory is not allocated (b'cos it's not available ?)
for the array

> 
> Have you run your program through gdb?
- yes I have

Fact is I had other arrays which were not just arrays of floats -
but arrays of large structs. I figured that each array was 4-5M and I
had about 5-6 of them strewn all over the code. That was causing the
trouble. I had declared them statically (not malloc ie). Shouldnt gcc warn
me that things are likely to go wrong ? The program works now that I
shifted to pointers and use malloc instead.

- is there a way to malloc a 3D array ? - or do I have to do it in 3
stages ?

-regarding Martin's response - no I was not exceeding array bounds - since
the program works now :-)

Thanks,
ag.


<
I changed a struct Big big[10000]; to 
struct Big* big; 
big = (struct Big*)malloc(10000*sizeof(struct Big));
and then things worked.
>



> HTH,
> --ag

> 
> Anand Ganesh wrote:
> > 
> > Hi,
> >         Why doesnt gcc crib when I make an array declaration that it cant
> > handle ?
> > eg
> > float array[10000][5][5];
> > 
> > That there is a problem I realised b'cos some unrelated variables were
> > getting changed. The reason I declared such an array instead of mallocing
> > is that it is easier to pass 3D arrays rather than float*** which you cant
> > use as a 3D array anymore (since dimensions are not known).
> > 
> > Is there a way to get a warning from gcc ? (I did gcc -Wall)
> > 
> > Thanks in advance for any help,
> > ag.
> > 
> > 
> 
> -- 
> Artie Gold, Austin, TX
> mailto:agold@bga.com or mailto:agold@cs.utexas.edu
> --
> "If you come to a fork in the road, take it." L. P. Berra
> 
> 


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: memory allocation
  1999-11-05 14:01   ` Anand Ganesh
@ 1999-11-30 23:28     ` Anand Ganesh
  0 siblings, 0 replies; 11+ messages in thread
From: Anand Ganesh @ 1999-11-30 23:28 UTC (permalink / raw)
  To: help-gcc

On Thu, 4 Nov 1999, Arthur Gold wrote:

> What do you mean by "can't handle"?
> I suspect there's something else going on in your code.

- I mean that enough memory is not allocated (b'cos it's not available ?)
for the array

> 
> Have you run your program through gdb?
- yes I have

Fact is I had other arrays which were not just arrays of floats -
but arrays of large structs. I figured that each array was 4-5M and I
had about 5-6 of them strewn all over the code. That was causing the
trouble. I had declared them statically (not malloc ie). Shouldnt gcc warn
me that things are likely to go wrong ? The program works now that I
shifted to pointers and use malloc instead.

- is there a way to malloc a 3D array ? - or do I have to do it in 3
stages ?

-regarding Martin's response - no I was not exceeding array bounds - since
the program works now :-)

Thanks,
ag.


<
I changed a struct Big big[10000]; to 
struct Big* big; 
big = (struct Big*)malloc(10000*sizeof(struct Big));
and then things worked.
>



> HTH,
> --ag

> 
> Anand Ganesh wrote:
> > 
> > Hi,
> >         Why doesnt gcc crib when I make an array declaration that it cant
> > handle ?
> > eg
> > float array[10000][5][5];
> > 
> > That there is a problem I realised b'cos some unrelated variables were
> > getting changed. The reason I declared such an array instead of mallocing
> > is that it is easier to pass 3D arrays rather than float*** which you cant
> > use as a 3D array anymore (since dimensions are not known).
> > 
> > Is there a way to get a warning from gcc ? (I did gcc -Wall)
> > 
> > Thanks in advance for any help,
> > ag.
> > 
> > 
> 
> -- 
> Artie Gold, Austin, TX
> mailto:agold@bga.com or mailto:agold@cs.utexas.edu
> --
> "If you come to a fork in the road, take it." L. P. Berra
> 
> 


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: memory allocation
  1999-11-04  6:16 ` Arthur Gold
  1999-11-05 14:01   ` Anand Ganesh
@ 1999-11-30 23:28   ` Arthur Gold
  1 sibling, 0 replies; 11+ messages in thread
From: Arthur Gold @ 1999-11-30 23:28 UTC (permalink / raw)
  To: help-gcc

What do you mean by "can't handle"?
I suspect there's something else going on in your code.

Have you run your program through gdb?
HTH,
--ag

Anand Ganesh wrote:
> 
> Hi,
>         Why doesnt gcc crib when I make an array declaration that it cant
> handle ?
> eg
> float array[10000][5][5];
> 
> That there is a problem I realised b'cos some unrelated variables were
> getting changed. The reason I declared such an array instead of mallocing
> is that it is easier to pass 3D arrays rather than float*** which you cant
> use as a 3D array anymore (since dimensions are not known).
> 
> Is there a way to get a warning from gcc ? (I did gcc -Wall)
> 
> Thanks in advance for any help,
> ag.
> 
> 

-- 
Artie Gold, Austin, TX
mailto:agold@bga.com or mailto:agold@cs.utexas.edu
--
"If you come to a fork in the road, take it." L. P. Berra

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: memory allocation
  1999-11-04  3:00 ` Martin Kahlert
@ 1999-11-30 23:28   ` Martin Kahlert
  0 siblings, 0 replies; 11+ messages in thread
From: Martin Kahlert @ 1999-11-30 23:28 UTC (permalink / raw)
  To: help-gcc

In article < Pine.SGI.4.10.9911040409470.31041-100000@whirlwind.cs.utexas.edu >,
	Anand Ganesh <anandg@cs.utexas.edu> writes:
> Hi,
> 	Why doesnt gcc crib when I make an array declaration that it cant
> handle ?
> eg
> float array[10000][5][5];
> 
> That there is a problem I realised b'cos some unrelated variables were
> getting changed. The reason I declared such an array instead of mallocing
> is that it is easier to pass 3D arrays rather than float*** which you cant
> use as a 3D array anymore (since dimensions are not known).
Then you will probably do out of bound writes inside your code.

array is just 5*5*10000*4=1MB in size.
There shouldn't be any problem with that.

If you get a segfault, then try the shell command limit
(i assume you are on Unix-shells here)

Hope, that helps,
Martin.

-- 
The early bird gets the worm. If you want something else for       
breakfast, get up later.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* memory allocation
  1999-11-04  2:15 memory allocation Anand Ganesh
  1999-11-04  3:00 ` Martin Kahlert
  1999-11-04  6:16 ` Arthur Gold
@ 1999-11-30 23:28 ` Anand Ganesh
  2 siblings, 0 replies; 11+ messages in thread
From: Anand Ganesh @ 1999-11-30 23:28 UTC (permalink / raw)
  To: help-gcc

Hi,
	Why doesnt gcc crib when I make an array declaration that it cant
handle ?
eg
float array[10000][5][5];

That there is a problem I realised b'cos some unrelated variables were
getting changed. The reason I declared such an array instead of mallocing
is that it is easier to pass 3D arrays rather than float*** which you cant
use as a 3D array anymore (since dimensions are not known).

Is there a way to get a warning from gcc ? (I did gcc -Wall)

Thanks in advance for any help,
ag.

	


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: memory allocation
  2013-09-19  1:04   ` zhaobin xv
@ 2013-09-19  2:38     ` Ian Lance Taylor
  0 siblings, 0 replies; 11+ messages in thread
From: Ian Lance Taylor @ 2013-09-19  2:38 UTC (permalink / raw)
  To: zhaobin xv; +Cc: gcc-help

On Wed, Sep 18, 2013 at 6:04 PM, zhaobin xv <xvzhaobin@gmail.com> wrote:
>
> I am studing cmplier and linux on arm,I want to know how complier
> (such as gcc) to allocate memory?
>
> a. Global and static variables locate at data segment
> b. When a function is called, memory is allocated on the stack to hold
>  parameter values, local variables, and the address of the calling
> function
>  c. the struct is aligned based on the greatest alignment requirement
>  of it's members.

Yes, that is all correct.


> according to elf or ABI?

The full ELF specification, including the appropriate ELF processor
supplement for the processor in question, is itself the ABI for an
operating system that uses ELF.  So when you ask whether this is ELF
or the ABI, you need to be clear on what you mean by ELF.


> do the segment originate from coff?

If you mean precisely "segment," then, no, COFF does not have
segments.  It only has sections.  As far as I know ELF was the first
object file format to have a clear notion of supporting an arbitrary
number of segments that are composed of an arbitrary number of
sections.

Ian

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: memory allocation
       [not found] ` <CAKOQZ8zsfKUFv0PDhUmDWDgqD9p-xMYUfW_eR9B-TWx5yjmjeQ@mail.gmail.com>
@ 2013-09-19  1:04   ` zhaobin xv
  2013-09-19  2:38     ` Ian Lance Taylor
  0 siblings, 1 reply; 11+ messages in thread
From: zhaobin xv @ 2013-09-19  1:04 UTC (permalink / raw)
  To: Ian Lance Taylor, gcc-help

2013/9/17 Ian Lance Taylor <iant@google.com>:
> On Mon, Sep 16, 2013 at 1:43 AM, zhaobin xv <xvzhaobin@gmail.com> wrote:
>>
>> As I know in C :
>> a. Global and static variables locate at data segment
>> b. When a function is called, memory is allocated on the stack to hold
>> parameter values, local variables, and the address of the calling
>> function
>> c. the struct is aligned based on the greatest alignment requirement
>> of it's members.
>> I want to know what these base on to define?
>> Are there some manual or book about these?
>
> This question is not appropriate for the mailing list gcc@gcc.gnu.org,
> which is for the development of GCC itself.  It would be appropriate
> for gcc-help@gcc.gnu.org.  Please take any followups to gcc-help.
> Thanks.
>
> The document you are looking for is generally called the ABI for the
> target processor and operating system.  There are different ABIs for
> each processor, and different operating systems on each processor also
> have different ABIs.  If you tell us which specific processor and
> operating system you are interested in we may be able to point you at
> the ABI document for that target.
>
> Ian
 Thank lan for your answer
I am studing cmplier and linux on arm,I want to know how complier
(such as gcc) to allocate memory?

a. Global and static variables locate at data segment
b. When a function is called, memory is allocated on the stack to hold
 parameter values, local variables, and the address of the calling
function
 c. the struct is aligned based on the greatest alignment requirement
 of it's members.

according to elf or ABI?

do the segment originate from coff?


xvzhaobin

^ permalink raw reply	[flat|nested] 11+ messages in thread

* re: memory allocation
@ 2003-09-13 15:00 Dan Kegel
  0 siblings, 0 replies; 11+ messages in thread
From: Dan Kegel @ 2003-09-13 15:00 UTC (permalink / raw)
  To: GCC Mailing List, gcc-help, laczo.tibor

Laczó Tibor wrote:
[ In Redhat Linux, how come calling delete on objects in C++
   doesn't return memory to the operating system?  Even after
   my program deletes all its big objects, RSS doesn't shrink. ]

That's an FAQ.  http://www.lysator.liu.se/c/c-faq/c-3.html says
Q. I have a program which mallocs but then frees a lot of memory,
but memory usage (as reported by ps) doesn't seem to go back down.
A. Most implementations of malloc/free do not return freed memory to
the operating system (if there is one), but merely make it available
for future malloc calls within the same process.

glibc's manual also mentions this:
http://www.gnu.org/manual/glibc-2.2.5/html_node/Freeing-after-Malloc.html#Freeing%20after%20Malloc
which says
"Occasionally, free can actually return memory to the operating system
and make the process smaller. Usually, all it can do is allow a
later call to malloc to reuse the space. In the meantime,
the space remains in your program as part of a free-list used internally by malloc."

Please take this discussion to gcc-help@gcc.gnu.org
or maybe comp.lang.c++.

gcc@gcc.gnu.org is for gcc maintainers, not users.
- Dan

-- 
Dan Kegel
http://www.kegel.com
http://counter.li.org/cgi-bin/runscript/display-person.cgi?user=78045

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2013-09-19  2:38 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-11-04  2:15 memory allocation Anand Ganesh
1999-11-04  3:00 ` Martin Kahlert
1999-11-30 23:28   ` Martin Kahlert
1999-11-04  6:16 ` Arthur Gold
1999-11-05 14:01   ` Anand Ganesh
1999-11-30 23:28     ` Anand Ganesh
1999-11-30 23:28   ` Arthur Gold
1999-11-30 23:28 ` Anand Ganesh
2003-09-13 15:00 Dan Kegel
     [not found] <CANEcUBX9ntHdZ7qEJg7ke=MADcxfP_uOwNnA==XvKbf3kyH1Lw@mail.gmail.com>
     [not found] ` <CAKOQZ8zsfKUFv0PDhUmDWDgqD9p-xMYUfW_eR9B-TWx5yjmjeQ@mail.gmail.com>
2013-09-19  1:04   ` zhaobin xv
2013-09-19  2:38     ` Ian Lance Taylor

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).