By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,385 Members | 1,737 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,385 IT Pros & Developers. It's quick & easy.

new and memset

P: n/a
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?

[b] 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)

Dec 25 '05 #1
Share this Question
Share on Google+
9 Replies


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

[b] 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.

Dec 25 '05 #2

P: n/a

Luke Meyers wrote:
[b] 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)

Dec 25 '05 #3

P: n/a

di************@gmail.com wrote:
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?

[b] 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> >
?

Dec 25 '05 #4

P: n/a
> 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

Dec 26 '05 #5

P: n/a
di************@gmail.com wrote:
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.
[b] 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

Dec 26 '05 #6

P: n/a
Luke Meyers wrote:
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.


Is it?
Jonathan

Dec 26 '05 #7

P: n/a
Thank you Jonathan and others for the suggestions and the constructive
criticism.

regards,
- D. Rathore

--------------------------------
Jonathan Mcdougall wrote:
If yes, what would be the syntax? Would it be:

memset(temp, 0, width*height*sizeof(int));
That would work.

Jonathan


Dec 26 '05 #8

P: n/a

"di************@gmail.com" <di**********@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
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?

[b] 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) );



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

Dec 26 '05 #9

P: n/a

Jim Langston wrote:
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 :)

Dec 26 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.