0

I have a variable:

unsigned int* data = (unsigned int*)malloc(height * width)

I want to set same int to all array values. I can't use memset because it works with bytes.

How can i do that?

R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
Erik Sapir
  • 23,209
  • 28
  • 81
  • 141

5 Answers5

15

Using C++:

std::vector<unsigned int> data(height * width, value);

If you need to pass the data to some legacy C function that expects a pointer, you can use &data[0] or &data.front() to get a pointer to the contiguous data in a well-defined manner.

If you absolutely insist on using pointers throughout (but you have no technical reason to do this, and I wouldn’t accept it in code review!), you can use std::fill to fill the range:

unsigned int* data = new int[height * width];
std::fill(data, data + height * width, value);
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • Can it work with pointers? I need data to be of type unsigned int* and i don't want to allocate again in this case. Meaning, i already have allocated array, i just need to fill it with same value – Erik Sapir Jul 07 '11 at 15:53
  • @Erik, yes you can use `pointer` with `vector` - as here http://msdn.microsoft.com/en-us/library/7e4tx21z.aspx – Steve Townsend Jul 07 '11 at 15:55
  • @Erik Sapir: If you can let `vector<>` handle the memory allocation for you, just return `&data[0]`. It's well-specified, and unless the vector changes size, it's guaranteed to work. – greyfade Jul 07 '11 at 15:56
  • std:fill is exactly what i wanted! – Erik Sapir Jul 07 '11 at 16:08
  • OK edit - it works but it is still a bit slow (compared to memset). – Erik Sapir Jul 07 '11 at 16:13
  • @Erik `std::fill` internally uses `memset` where possible (i.e. for `char` types). For other scalar types (e.g. `int`) it uses SIMD instructions where available. In short, it uses the fastest possible solution in all cases (at least on modern compilers). There is nothing you can do to make it faster if you already have optimizations enabled. – Konrad Rudolph Jul 07 '11 at 16:25
  • @thkala How do you get that weird idea? Why should C library code be more heavily optimised than C++ library code? Anyway, it’s just wrong, see my previous comment. – Konrad Rudolph Jul 07 '11 at 16:26
2

Assuming your array memory dimension is invariant:

#include <vector>

unsigned int literal(500);
std::vector<unsigned int> vec(height * width, literal);
vector<unsigned int>::pointer data = &vec[0];

Boost.MultiArray might be of interest, since you appear to be indexing points in a space here (dimension of your 1D array comes from height and width).

Steve Townsend
  • 53,498
  • 9
  • 91
  • 140
  • vec will be released when function ends, no? – Erik Sapir Jul 07 '11 at 15:56
  • @Erik - yes, RAII (http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization) means memory will be released when `vec` goes out of scope. @Kerrek - point taken, hence the proviso about invariance. – Steve Townsend Jul 07 '11 at 15:59
1

If you are confident that you want an array, do it the C++ way, and don't listen to anyone who says "malloc", "for" or "free candy":

#include <algorithm>

const size_t arsize = height * width;
unsigned int * data = new unsigned int[arsize];
std::fill(data, data + arsize, value);

/* dum-dee-dum */

delete[] data; // all good now (hope we didn't throw an exception before here!)

If you don't know for sure that you need an array, use a vector like Konrad says.

Community
  • 1
  • 1
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
0

I think you'll have to use a for loop!

int i;
for (i = 0; i < height * width; i++)
  data[i] = value;
aardvarkk
  • 14,955
  • 7
  • 67
  • 96
  • Only in the nonplussed language. – R. Martinho Fernandes Jul 07 '11 at 15:51
  • 1
    This is wrong 'height*width' is not necessarily the number of items in the array. – Mike Kwan Jul 07 '11 at 15:51
  • 1
    You're right -- my answer assumes they're single bytes. I was trying to keep the code sample short. If they're not single bytes (which I don't think they are because the questions specifically states they aren't), then isn't the malloc() call incorrect? – aardvarkk Jul 07 '11 at 15:52
  • You don't have to use a for loop when you can use `std::fill()`. – greyfade Jul 07 '11 at 16:02
  • 1
    It wasn't initially clear whether he wanted C or C++. And judging by his use of malloc(), I (incorrectly, it would appear!) chose C! – aardvarkk Jul 07 '11 at 16:03
0

You have tagged this a both C and C++. They are not the same language.

In C, you probably want a code fragment like:

// WARNING: UNTESTED
unsigned int* data = malloc(height * width * sizeof (unisgned int));
int i;
for(i = 0; i < height*width; i++)
    data[i] = 1941;
Robᵩ
  • 163,533
  • 20
  • 239
  • 308