473,471 Members | 1,937 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

new and memset

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
9 12325
[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

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

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
> 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
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
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
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

"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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mantorok Redgormor | last post by:
When is it that memset caused problems? I recall from posts in the past where someone used memset in their code that invoked undefined behavior. What about the following? char the_array; ...
6
by: bob_jenkins | last post by:
{ const void *p; (void)memset((void *)p, ' ', (size_t)10); } Should this call to memset() be legal? Memset is of type void *memset(void *, unsigned char, size_t) Also, (void *) is the...
26
by: 69dbb24b2db3daad932c457cccfd6 | last post by:
Hello, I have to initialize all elements of a very big float point array to zero. It seems memset(a, 0, len) is faster than a simple loop. I just want to know whether it is safe to do so, since I...
21
by: jacob navia | last post by:
Many compilers check printf for errors, lcc-win32 too. But there are other functions that would be worth to check, specially memset. Memset is used mainly to clear a memory zone, receiving a...
14
by: Patrick Kowalzick | last post by:
Dear all, I have an existing piece of code with a struct with some PODs. struct A { int x; int y; };
27
by: volunteers | last post by:
I met a question about memset and have no idea right now. Could anybody give a clue? Thanks memset is sometimes used to initialize data in a constructor like the example below. What is the...
23
by: AndersWang | last post by:
Hi, dose anybody here explain to me why memset would be faster than a simple loop. I doubt about it! In an int array scenario: int array; for(int i=0;i<10;i++) //ten loops
18
by: dykeinthebox | last post by:
Consider the following program: #include <stdlib.h> #include <string.h> int main( void ) { void *p = malloc( 4 ); if ( p ) {
18
by: Gaijinco | last post by:
I'm having a headache using memset() Given: int v; memset((void*)v, 1, sizeof(v)); Can I be 100% positive than v = 1 for i 0, or there is something else I have to do?.
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.