new and memset

D

divya_rathore_

Consider the dynamic allocation of a 2D array:

int **temp;
temp = new int*[height];
for (y=0; y<height; y++) temp[y] = new int[width];

I have 2 Questions:

[a] Does new initializes memory to 0?

I want to initialize this to some value not necessarily 0. Is it
ok to use 'memset' to this dynamically allocated array?

If yes, what would be the syntax? Would it be:

memset(temp, 0, width*height*sizeof(int));


thanks in advance,
D. Rathore
(remove underscores for email ID)
 
L

Luke Meyers

[a] No, thankfully. That would add a O(N) overhead cost, needlessly,
to every memory allocation.

Sure. memset works with any pointer, it doesn't matter if it's
pointing to the stack or to the heap. Your syntax looks fine for
setting it to 0. Remember that the second parameter is converted to an
unsigned char.
 
D

divya_rathore_

Luke said:
Sure. ...
Remember that the second parameter is converted to an
unsigned char.


What shall be done to convert it to int? Or float, if the 2D array is
of floats?

thanks in advance,
D. Rathore
(remove underscores for email ID)
 
P

puzzlecracker

Consider the dynamic allocation of a 2D array:

int **temp;
temp = new int*[height];
for (y=0; y<height; y++) temp[y] = new int[width];

I have 2 Questions:

[a] Does new initializes memory to 0?

I want to initialize this to some value not necessarily 0. Is it
ok to use 'memset' to this dynamically allocated array?

If yes, what would be the syntax? Would it be:

memset(temp, 0, width*height*sizeof(int));


thanks in advance,
D. Rathore
(remove underscores for email ID)


ever consider programming safely?
temp = new int*[height];
for (y=0; y<height; y++) temp[y] = new int[width

does you environment incur prohibitive cost using vector<vector<int> >
?
 
L

Luke Meyers

What shall be done to convert it to int? Or float, if the 2D array is
of floats?

It's not converted to an int, it's converted to an unsigned char, which
is the usual way to refer to an individual byte value. memset only
operates on bytes, it will not initialize an array of floats that
represent zero value for you. It will set all bytes in the specified
range to the exact byte value attained by implicitly converting the
supplied int to an unsigned char.

I really question what benefit you expect to achieve from this.
Relying on default initializations to null values is generally
indicative of a problematic design.

Luke
 
J

Jonathan Mcdougall

Consider the dynamic allocation of a 2D array:

int **temp;
temp = new int*[height];
for (y=0; y<height; y++) temp[y] = new int[width];

I have 2 Questions:

[a] Does new initializes memory to 0?

No, but

new int[width]();

does.
I want to initialize this to some value not necessarily 0. Is it
ok to use 'memset' to this dynamically allocated array?


No. memset() operates on bytes. If sizeof(int) is more than a byte,
you'll get weird results. Usually, memset() is used to zero-initialize
an array or a structure, not to assign an arbitrary value. What's more,
some types (such as floats or non-pods) cannot be memset'ed.
If yes, what would be the syntax? Would it be:

memset(temp, 0, width*height*sizeof(int));

That would work.

If you want to put another value than 0, or if your types are not
integrals, do not use memset. Either use a loop or another container,
such as standard containers.


Jonathan
 
J

Jonathan Mcdougall

Luke said:
It's not converted to an int, it's converted to an unsigned char, which
is the usual way to refer to an individual byte value. memset only
operates on bytes, it will not initialize an array of floats that
represent zero value for you. It will set all bytes in the specified
range to the exact byte value attained by implicitly converting the
supplied int to an unsigned char.

I really question what benefit you expect to achieve from this.
Relying on default initializations to null values is generally
indicative of a problematic design.

Is it?


Jonathan
 
D

divya_rathore_

Thank you Jonathan and others for the suggestions and the constructive
criticism.

regards,
- D. Rathore
 
J

Jim Langston

Consider the dynamic allocation of a 2D array:

int **temp;
temp = new int*[height];
for (y=0; y<height; y++) temp[y] = new int[width];

I have 2 Questions:

[a] Does new initializes memory to 0?

I want to initialize this to some value not necessarily 0. Is it
ok to use 'memset' to this dynamically allocated array?

If yes, what would be the syntax? Would it be:

memset(temp, 0, width*height*sizeof(int));


You are not guaranteed that the different blocks of memory allocated will be
contiguous.

temp[0] and temp[1] may not be width bytes apart. You'll be lucky if they
are, but there is no guarantee.

Some of the reasons they wouldn't be next to each other include, but are not
limited to:
1. Previously you allocated a memory, and some of it was freed that is big
enough for width bytes, so your first new grabs that memory, but there is
allocated memory in the way for the second new, so it grabs it at another
point in memory.
2. Your first block of memory grabed with new is toward the end of a memory
page. The next allocation won't fit in one page so the OS decides to grab
that memory at the start of a new page.
3. Any other reason your OS decides to not grab the memory right after each
other.

It may work for you 99 times out of 100. But that 100th time you're gonna
get weird errors and not be able to track them down. Don't treat your temp
array as a contiguous block of memory unless you actually allocate it that
way.

Better is:
.... = new int[width]();

or

for ( y = 0; y < height; y++ )
memset( temp[y], 0, width*sizeof(int) );
 
D

divya_rathore_

Jim said:
Better is:
... = new int[width]();

or

for ( y = 0; y < height; y++ )
memset( temp[y], 0, width*sizeof(int) );


Way to go, Jim! That's what I was looking for :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top