Where can I ask this?

Hi all,

I need to ask a question about memory allocation,
someone would not mind about telling me where can I
ask this?

I am using linux, but I think it is general…

Thanks in advance,

In?cio._______________________________________________________
Yahoo! Acesso Gr?tis - Internet r?pida e gr?tis.
Instale o discador agora! http://br.acesso.yahoo.com/

wel you could ask in IRC on a programming chat room but whatever, go ahead
and ask away…an off topic message wont kill anyone hopefully :P> ----- Original Message -----

From: jinacio3@yahoo.com.br (J Inacio)
To:
Sent: Thursday, July 21, 2005 12:36 PM
Subject: [SDL] where can I ask this?

Hi all,

I need to ask a question about memory allocation,
someone would not mind about telling me where can I
ask this?

I am using linux, but I think it is general…

Thanks in advance,

In?cio.


Yahoo! Acesso Gr?tis - Internet r?pida e gr?tis.
Instale o discador agora! http://br.acesso.yahoo.com/


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Hi all,

I need to ask a question about memory allocation,
someone would not mind about telling me where can I
ask this?

I am using linux, but I think it is general…

Thanks in advance,

In?cio.

Maybe here: http://www.gamedev.net/community/forums/ ?

Koshmaar

Hi,

Selon J Inacio :

I need to ask a question about memory allocation,
someone would not mind about telling me where can I
ask this?

Long answer: http://www.catb.org/~esr/faqs/smart-questions.html

Short answer: try http://www.google.com first. If you can’t find your
information, you’ll at least find places where this subject is not off-topic.

Best regards,

Xavier

Hi there

The question is:

How can I keep track of the amount of memory
allocated?
If I malloc 1024 * 1024 * 8, after the memory was
allocated, there is a way to know the exact amount of
memory wich was allocated?
I am thinking about a resource manager…
I would like to keep control over the amount of
memory wich was allocated and freed…

Does anyone have any Idea about it?

Thanks for your patience and your time …

  • In?cio Ferrarini.

PS: I know it is still off topic (it is related to
memory management in C/C++).

— J Inacio <@J_Inacio1> escreveu:> Hi all,

I need to ask a question about memory allocation,
someone would not mind about telling me where can I
ask this?

I am using linux, but I think it is general…

Thanks in advance,

In?cio.


Yahoo! Acesso Gr?tis - Internet r?pida e gr?tis.
Instale o discador agora!
http://br.acesso.yahoo.com/


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


Converse com seus amigos em tempo real com o Yahoo! Messenger

If you’re using C you could write methods that work similarly to
malloc() and free() but also keep track of the total memory allocated:

long total_mem_allocated = 0;

void* my_malloc(size_t nbytes) {
total_mem_allocated += (long)nbytes;
return malloc(nbytes);
}

void my_free(void* ptr) {
total_mem_allocated -= sizeof(*ptr);
if (total_mem_allocated < 0)
fprintf(stderr, “You’re using the wrong version of free() somewhere\n”);
free(ptr);
}

This code’s untested but should give you the most basic idea of what
you can do. You can always make things as complicated as you like as
well (such as keeping track of every single pointer you allocate
memory for) but the more you do during allocation the slower it will
take.

ErikOn 7/25/05, J Inacio wrote:

Hi there

The question is:

How can I keep track of the amount of memory
allocated?
If I malloc 1024 * 1024 * 8, after the memory was
allocated, there is a way to know the exact amount of
memory wich was allocated?
I am thinking about a resource manager…
I would like to keep control over the amount of
memory wich was allocated and freed…

Does anyone have any Idea about it?

Thanks for your patience and your time …

  • In?cio Ferrarini.

PS: I know it is still off topic (it is related to
memory management in C/C++).

— J Inacio escreveu:

Hi all,

I need to ask a question about memory allocation,
someone would not mind about telling me where can I
ask this?

I am using linux, but I think it is general…

Thanks in advance,

In?cio.


Yahoo! Acesso Gr?tis - Internet r?pida e gr?tis.
Instale o discador agora!
http://br.acesso.yahoo.com/


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


Converse com seus amigos em tempo real com o Yahoo! Messenger
http://br.download.yahoo.com/messenger/


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

— Erik Gillespie <erik.gillespie at gmail.com>
escreveu:

Thanks,
You’ve put me in the right direction, although I’ve
tried the aproach (sizeof pointer), but it always
returned the size of the pointer, not the allocated
area wich the pointer points to …
I will try to implement it myself.
It is easy to create a linked list or something like
that to keep track of memory allocated… using the
pointer base address as a field, memory allocated as
other…
I think it will work…

Again, thank you very much> If you’re using C you could write methods that work

similarly to
malloc() and free() but also keep track of the total
memory allocated:

long total_mem_allocated = 0;

void* my_malloc(size_t nbytes) {
total_mem_allocated += (long)nbytes;
return malloc(nbytes);
}

void my_free(void* ptr) {
total_mem_allocated -= sizeof(*ptr);
if (total_mem_allocated < 0)
fprintf(stderr, “You’re using the wrong
version of free() somewhere\n”);
free(ptr);
}

This code’s untested but should give you the most
basic idea of what
you can do. You can always make things as
complicated as you like as
well (such as keeping track of every single pointer
you allocate
memory for) but the more you do during allocation
the slower it will
take.

Erik

On 7/25/05, J Inacio <@J_Inacio1> wrote:

Hi there

The question is:

How can I keep track of the amount of memory
allocated?
If I malloc 1024 * 1024 * 8, after the memory was
allocated, there is a way to know the exact amount
of
memory wich was allocated?
I am thinking about a resource manager…
I would like to keep control over the amount of
memory wich was allocated and freed…

Does anyone have any Idea about it?

Thanks for your patience and your time …

  • In?cio Ferrarini.

PS: I know it is still off topic (it is related
to
memory management in C/C++).

— J Inacio <@J_Inacio1> escreveu:

Hi all,

I need to ask a question about memory
allocation,

someone would not mind about telling me where
can I

ask this?

I am using linux, but I think it is general…

Thanks in advance,

In?cio.


Yahoo! Acesso Gr?tis - Internet r?pida e gr?tis.
Instale o discador agora!
http://br.acesso.yahoo.com/


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


Converse com seus amigos em tempo real com o
Yahoo! Messenger
http://br.download.yahoo.com/messenger/


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


Yahoo! Acesso Gr?tis - Internet r?pida e gr?tis.
Instale o discador agora! http://br.acesso.yahoo.com/

— Erik Gillespie <erik.gillespie at gmail.com>
escreveu:

Thanks,
You’ve put me in the right direction, although I’ve
tried the aproach (sizeof pointer), but it always
returned the size of the pointer, not the allocated
area wich the pointer points to …

Correct.
Most malloc implementations have a function you can call to get the
size of an allocated block; however, this is nonstandard, nonportable,
and probably undocumented.

On Linux (glibc 2), it’s this function (defined in malloc.h):

/* Report the number of usable allocated bytes associated with
allocated chunk ptr. */
size_t malloc_usable_size (void *ptr);

I will try to implement it myself.
It is easy to create a linked list or something like
that to keep track of memory allocated… using the
pointer base address as a field, memory allocated as
other…
I think it will work…

If you need to be portable, that works.

– JoshOn 7/25/05, J Inacio wrote:

Again, thank you very much

If you’re using C you could write methods that work
similarly to
malloc() and free() but also keep track of the total
memory allocated:

long total_mem_allocated = 0;

void* my_malloc(size_t nbytes) {
total_mem_allocated += (long)nbytes;
return malloc(nbytes);
}

void my_free(void* ptr) {
total_mem_allocated -= sizeof(*ptr);
if (total_mem_allocated < 0)
fprintf(stderr, “You’re using the wrong
version of free() somewhere\n”);
free(ptr);
}

This code’s untested but should give you the most
basic idea of what
you can do. You can always make things as
complicated as you like as
well (such as keeping track of every single pointer
you allocate
memory for) but the more you do during allocation
the slower it will
take.

Erik

On 7/25/05, J Inacio wrote:

Hi there

The question is:

How can I keep track of the amount of memory
allocated?
If I malloc 1024 * 1024 * 8, after the memory was
allocated, there is a way to know the exact amount
of
memory wich was allocated?
I am thinking about a resource manager…
I would like to keep control over the amount of
memory wich was allocated and freed…

Does anyone have any Idea about it?

Thanks for your patience and your time …

  • In?cio Ferrarini.

PS: I know it is still off topic (it is related
to
memory management in C/C++).

— J Inacio escreveu:

Hi all,

I need to ask a question about memory
allocation,

someone would not mind about telling me where
can I

ask this?

I am using linux, but I think it is general…

Thanks in advance,

In?cio.


Yahoo! Acesso Gr?tis - Internet r?pida e gr?tis.
Instale o discador agora!
http://br.acesso.yahoo.com/


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


Converse com seus amigos em tempo real com o
Yahoo! Messenger
http://br.download.yahoo.com/messenger/


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


Yahoo! Acesso Gr?tis - Internet r?pida e gr?tis.
Instale o discador agora! http://br.acesso.yahoo.com/


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Blast, I knew that, it just slipped my mind. Well there are plenty of
routes you can go. You could pass in the size along with the pointer,
you could create a table of pointers, you could force all of your
types to “derive” from a base class that contains a ->size() method
(if you’re using C++, etc.

But as stated before all of this is going to slow you down and/or
force you to do things that will look weird from a 3rd-eye view.

ErikOn 7/25/05, J Inacio wrote:

— Erik Gillespie <@Erik_Gillespie>
escreveu:

Thanks,
You’ve put me in the right direction, although I’ve
tried the aproach (sizeof pointer), but it always
returned the size of the pointer, not the allocated
area wich the pointer points to …
I will try to implement it myself.
It is easy to create a linked list or something like
that to keep track of memory allocated… using the
pointer base address as a field, memory allocated as
other…
I think it will work…

Again, thank you very much

If you’re using C you could write methods that work
similarly to
malloc() and free() but also keep track of the total
memory allocated:

long total_mem_allocated = 0;

void* my_malloc(size_t nbytes) {
total_mem_allocated += (long)nbytes;
return malloc(nbytes);
}

void my_free(void* ptr) {
total_mem_allocated -= sizeof(*ptr);
if (total_mem_allocated < 0)
fprintf(stderr, “You’re using the wrong
version of free() somewhere\n”);
free(ptr);
}

This code’s untested but should give you the most
basic idea of what
you can do. You can always make things as
complicated as you like as
well (such as keeping track of every single pointer
you allocate
memory for) but the more you do during allocation
the slower it will
take.

Erik

On 7/25/05, J Inacio wrote:

Hi there

The question is:

How can I keep track of the amount of memory
allocated?
If I malloc 1024 * 1024 * 8, after the memory was
allocated, there is a way to know the exact amount
of
memory wich was allocated?
I am thinking about a resource manager…
I would like to keep control over the amount of
memory wich was allocated and freed…

Does anyone have any Idea about it?

Thanks for your patience and your time …

  • In?cio Ferrarini.

PS: I know it is still off topic (it is related
to
memory management in C/C++).

— J Inacio escreveu:

Hi all,

I need to ask a question about memory
allocation,

someone would not mind about telling me where
can I

ask this?

I am using linux, but I think it is general…

Thanks in advance,

In?cio.


Yahoo! Acesso Gr?tis - Internet r?pida e gr?tis.
Instale o discador agora!
http://br.acesso.yahoo.com/


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


Converse com seus amigos em tempo real com o
Yahoo! Messenger
http://br.download.yahoo.com/messenger/


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


Yahoo! Acesso Gr?tis - Internet r?pida e gr?tis.
Instale o discador agora! http://br.acesso.yahoo.com/


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

But as stated before all of this is going to slow you down and/or
force you to do things that will look weird from a 3rd-eye view.

To avoid the speed problem, simply use one debug and one release version of
your app, if this is possible of course !–

P.O.M.P.E.I. du 47-20

Hi again,

Well… I am thinking about keep track of memory
allocated to store the files, a control to swap-in and
swap-out files…
Like limit a maximum allocated size… I will study
more the C++ streams to know the best aproach to
handle files…
I know I can make it as complex as I want to (it is
possible in C and C++), but I will study a little bit
harder before I start coding the streams thing (the
core of the thing).

Thanks for your patience and time,

  • In?cio.

— Erik Gillespie <erik.gillespie at gmail.com>
escreveu:> If you’re using C you could write methods that work

similarly to
malloc() and free() but also keep track of the total
memory allocated:

long total_mem_allocated = 0;

void* my_malloc(size_t nbytes) {
total_mem_allocated += (long)nbytes;
return malloc(nbytes);
}

void my_free(void* ptr) {
total_mem_allocated -= sizeof(*ptr);
if (total_mem_allocated < 0)
fprintf(stderr, “You’re using the wrong
version of free() somewhere\n”);
free(ptr);
}

This code’s untested but should give you the most
basic idea of what
you can do. You can always make things as
complicated as you like as
well (such as keeping track of every single pointer
you allocate
memory for) but the more you do during allocation
the slower it will
take.

Erik

On 7/25/05, J Inacio <@J_Inacio1> wrote:

Hi there

The question is:

How can I keep track of the amount of memory
allocated?
If I malloc 1024 * 1024 * 8, after the memory was
allocated, there is a way to know the exact amount
of
memory wich was allocated?
I am thinking about a resource manager…
I would like to keep control over the amount of
memory wich was allocated and freed…

Does anyone have any Idea about it?

Thanks for your patience and your time …

  • In?cio Ferrarini.

PS: I know it is still off topic (it is related
to
memory management in C/C++).

— J Inacio <@J_Inacio1> escreveu:

Hi all,

I need to ask a question about memory
allocation,

someone would not mind about telling me where
can I

ask this?

I am using linux, but I think it is general…

Thanks in advance,

In?cio.


Yahoo! Acesso Gr?tis - Internet r?pida e gr?tis.
Instale o discador agora!
http://br.acesso.yahoo.com/


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


Converse com seus amigos em tempo real com o
Yahoo! Messenger
http://br.download.yahoo.com/messenger/


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


Converse com seus amigos em tempo real com o Yahoo! Messenger

Really people… Can no one else on this list but I help this poor man?

J Inacio… since I don’t know your first name, I’ll just call you J-Man.

So, J-Man, you want to do what?On Jul 25, 2005, at 8:00 AM, J Inacio wrote:

I would like to keep control over the amount of
memory wich was allocated and freed…

Oh, you would like to keep control over the amount of memory you’re
using.

J-Man, you are clearly a novice. This is not meant to offend you, but
to help you. But what you want is wrong. Please tell us what you are
trying to do. Please avoid over-generalization. Many people believe
that if they describe their problem / desire in terms that are very
general, they can avoid wasting people’s time with details that are
specific to the project at hand.

For an experienced person, this is good advice. It allows the
programmer to describe his situation concisely, and precisely. It saves
time, and avoid confusion.

For a novice, this is bad advice. (Please let me know if the following
english phrasing is difficult.) As a novice, you are probably going
about this all wrong. You are probably trying to perform a task that
doesn’t need to be done, or trying to solve your problem in the wrong
way.

So please, for you, and us: tell us what you’re trying to do, without
over-generalizing.

Well… I am thinking about keep track of memory
allocated to store the files, a control to swap-in and
swap-out files…
Like limit a maximum allocated size… I will study
more the C++ streams to know the best aproach to
handle files…
I know I can make it as complex as I want to (it is
possible in C and C++), but I will study a little bit
harder before I start coding the streams thing (the
core of the thing).

I would take a higher level approach…if you specific concern is how
much memory is being used by textures/bitmaps/sprites/whatever, it’s
fairly easy to keep track of that in both total and individual
allocations. If you have a Bitmap class, just add a member for each one
that stores the size of the image’s memory footprint, and a static
variable in the class that is added to in the constructor and subtracted
from in the destructor.

At the lower level, though…

If you want to know all the memory your app allocates, you can write
your own malloc/realloc/free/new/delete wrappers (see below) and just be
careful to always use them instead of malloc directly. Overriding
operator new in C++ goes a long way to making this automatic.

If you want to know ALL the memory allocated, including when libraries
call malloc() behind the scenes (such as SDL allocating memory for a
shadow surface), that gets a little tougher, but on Linux systems, you
can hook malloc() so you catch every call to the memory functions.
Google for “__malloc_hook” … not sure what other C runtimes offer on
other platforms.

In writing your own allocation functions, the cheapest way to manage
this portably would to be something like this:

static size_t total_allocated_memory = 0;

void my_malloc(size_t memsize) {
/
over-allocate a little */
size_t *retval = (size_t ) malloc(memsize + sizeof (size_t));
if (retval) { /
store size before actual buffer */
*retval = memsize;
total_allocated_memory += memsize;
return(retval + 1);
}
return(NULL);
}

void my_free(void *ptr) {
if (ptr) {
size_t *realptr = (size_t ) ptr;
realptr–; /
move back to start of actual malloc() ptr */
total_allocated_memory -= *realptr;
free(realptr);
}
}

Basically, over-allocate the memory so you can store the size of the
buffer right before the block. In reality, this is usually what malloc()
and operator new are doing internally anyhow, but it’s much faster than
keeping a linked list or hashtable of what pointer has how much
allocated memory, etc.

–ryan.

Hi there

Thanks for your advices :slight_smile:

Well, I am a little paranoic about resources I am
using, trying to keep track of each byte I am using (I
know, nowadays a lot of people have 1+ GB ram, but,
anyways, it is paranoia).
I know, nowadays bytes in ram memmory are so cheap
that is worthless the effort to save them.
Well, it was wonderfull to see a lot of different
(why not say new?) points of view, I could keep
control, but it would be slower.

Well, it was trying to do that for fun, nothing

I am not that newbie (just coding in Java from 4
years ago), and when I was a C and C++ (was for hobby,
a long ago) programmer I never worried how much memory
was allocated :slight_smile:

Thanks everybody for your patience and time

J. In?cio.

PS: J. is Jos?, but my first name is almos used
always like J only (Jos? is very common in Brazil, so
it will be a lot confusing…)

Thanks again :slight_smile:
— Donny Viszneki escreveu:> Really people… Can no one else on this list but I

help this poor man?

J Inacio… since I don’t know your first name, I’ll
just call you J-Man.

So, J-Man, you want to do what?

On Jul 25, 2005, at 8:00 AM, J Inacio wrote:

I would like to keep control over the amount of
memory wich was allocated and freed…

Oh, you would like to keep control over the amount
of memory you’re
using.

J-Man, you are clearly a novice. This is not meant
to offend you, but
to help you. But what you want is wrong. Please tell
us what you are
trying to do. Please avoid over-generalization. Many
people believe
that if they describe their problem / desire in
terms that are very
general, they can avoid wasting people’s time with
details that are
specific to the project at hand.

For an experienced person, this is good advice. It
allows the
programmer to describe his situation concisely, and
precisely. It saves
time, and avoid confusion.

For a novice, this is bad advice. (Please let me
know if the following
english phrasing is difficult.) As a novice, you are
probably going
about this all wrong. You are probably trying to
perform a task that
doesn’t need to be done, or trying to solve your
problem in the wrong
way.

So please, for you, and us: tell us what you’re
trying to do, without
over-generalizing.


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


Converse com seus amigos em tempo real com o Yahoo! Messenger

Hi there

Thanks for your advices :slight_smile:

Well, I am a little paranoic about resources I am
using, trying to keep track of each byte I am using (I
know, nowadays a lot of people have 1+ GB ram, but,
anyways, it is paranoia).
I know, nowadays bytes in ram memmory are so cheap
that is worthless the effort to save them.

I disagree strongly. Some people think that computers are becoming
obsolete so fast these days because hardware technology just keeps
getting better and better: not true. Computers doing the same things
they did a few years ago do it more slowly today because many
programmers don’t take the necessary time to write an efficient
program.

I just finished writing a curses-based “party shuffle” music player in
C because my iTunes occupies a whopping 65 MB!! No thank you, I much
prefer my player which only occupies 344 KB of RAM. At a glance I also
notice that Vim is using 172 KB with millions of features and many
documents open, while Apple’s TextEdit.app requires 7.11 MB with two
documents open.

I applaud J-Man’s efforts to decrease the memory footprint of his
application(s).

Well, it was wonderfull to see a lot of different
(why not say new?) points of view, I could keep
control, but it would be slower.

Well, it was trying to do that for fun, nothing

I am not that newbie (just coding in Java from 4
years ago), and when I was a C and C++ (was for hobby,
a long ago) programmer I never worried how much memory
was allocated :slight_smile:

4 years in Java doesn’t necessarily mean much. I know 4-year computer
science majors who have been using several languages for longer than
that, and yet they still don’t have a lot of practical knowledge and
understanding that makes them an experienced programmer in my opinion.

Thanks everybody for your patience and time

J. In?cio.

PS: J. is Jos?, but my first name is almos used
always like J only (Jos? is very common in Brazil, so
it will be a lot confusing…)

Ah, ok Jos?. Thanks for telling me!On Jul 28, 2005, at 8:19 AM, J Inacio wrote:

Thanks again :slight_smile:
— Donny Viszneki escreveu:

Really people… Can no one else on this list but I
help this poor man?

J Inacio… since I don’t know your first name, I’ll
just call you J-Man.

So, J-Man, you want to do what?

On Jul 25, 2005, at 8:00 AM, J Inacio wrote:

I would like to keep control over the amount of
memory wich was allocated and freed…

Oh, you would like to keep control over the amount
of memory you’re
using.

J-Man, you are clearly a novice. This is not meant
to offend you, but
to help you. But what you want is wrong. Please tell
us what you are
trying to do. Please avoid over-generalization. Many
people believe
that if they describe their problem / desire in
terms that are very
general, they can avoid wasting people’s time with
details that are
specific to the project at hand.

For an experienced person, this is good advice. It
allows the
programmer to describe his situation concisely, and
precisely. It saves
time, and avoid confusion.

For a novice, this is bad advice. (Please let me
know if the following
english phrasing is difficult.) As a novice, you are
probably going
about this all wrong. You are probably trying to
perform a task that
doesn’t need to be done, or trying to solve your
problem in the wrong
way.

So please, for you, and us: tell us what you’re
trying to do, without
over-generalizing.


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


Converse com seus amigos em tempo real com o Yahoo! Messenger
http://br.download.yahoo.com/messenger/


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl