473,383 Members | 1,855 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

function that uses size of array??

I have a function that takes two pointers to an array. The first point
to the first element while the other points to the last element.
int nums[] = { 1, 2, 3, 4, 5, 7, 8, 9};
int* result;
int end = (sizeof(nums)/4)-1;
int s = 7;

result = ::myfind(nums, nums + end, s);

the implementation goes like:
int* myfind(int* arr_start, int* arr_end, int& s) {
int arr_size = (arr_end - arr_start)+1;
int* result = arr_end;

for (int i = 0; i < arr_size; i++) {
if (*(arr_start+i) == s)
{
result = arr_start+i;
}
}

return result;

}

I have been told that elements in an array does not necessary lie after
each other in memory. So if I wanted a char array or string array
instead I guess the above function would not work?

In that case how do I make sure that I access the right elements in the
right order in an array and that I can calculate its size with:

int arr_size = (arr_end - arr_start)+1;
Apr 26 '07 #1
5 2480
I have been told that elements in an array does not necessary lie
after each other in memory. So if I wanted a char array or string
array instead I guess the above function would not work?
For an array, they do. For a std::list, they don't.
The function would work for a std::vector.
In that case how do I make sure that I access the right elements in
the right order in an array and that I can calculate its size with:
If elements do not come in line, how do you know that *(++pFirst) will
point to the second element?

It's prefectly OK to use this for arrays. However, since we are 2007
and this is C++, please _do_ use std::vector instead.
Apr 26 '07 #2
Gernot Frisch wrote:
>I have been told that elements in an array does not necessary lie
after each other in memory. So if I wanted a char array or string
array instead I guess the above function would not work?

For an array, they do. For a std::list, they don't.
The function would work for a std::vector.
>In that case how do I make sure that I access the right elements in
the right order in an array and that I can calculate its size with:

If elements do not come in line, how do you know that *(++pFirst) will
point to the second element?

It's prefectly OK to use this for arrays. However, since we are 2007
and this is C++, please _do_ use std::vector instead.

ok but will I get the correct size of a std::list with integers if I use:
int* myfind(int* arr_start, int* arr_end, int& s) {
int arr_size = (arr_end - arr_start)+1;

does arr_size not only give the right result if elements are placed
after each other in memory?
Apr 26 '07 #3
desktop wrote:
:: Gernot Frisch wrote:
:::: I have been told that elements in an array does not necessary lie
:::: after each other in memory. So if I wanted a char array or string
:::: array instead I guess the above function would not work?
:::
::: For an array, they do. For a std::list, they don't.
::: The function would work for a std::vector.
:::
:::: In that case how do I make sure that I access the right elements in
:::: the right order in an array and that I can calculate its size with:
:::
::: If elements do not come in line, how do you know that *(++pFirst)
::: will point to the second element?
:::
::: It's prefectly OK to use this for arrays. However, since we are 2007
::: and this is C++, please _do_ use std::vector instead.
:::
:::
::
:: ok but will I get the correct size of a std::list with integers if I
:: use:
::
::
:: int* myfind(int* arr_start, int* arr_end, int& s) {
:: int arr_size = (arr_end - arr_start)+1;
::
::
::
:: does arr_size not only give the right result if elements are placed
:: after each other in memory?

Yes, but with std::list and std::vector you don't have to compute the size
yourself, just call the container's size() member function.

Also, you might not need the size at all, as the containers also provide
begin() and end() functions!
Bo Persson


Apr 26 '07 #4
On Apr 26, 10:52 am, desktop <f...@sss.comwrote:
I have a function that takes two pointers to an array. The first point
to the first element while the other points to the last element.

int nums[] = { 1, 2, 3, 4, 5, 7, 8, 9};
int* result;
int end = (sizeof(nums)/4)-1;
An integer on my system is not 4 bytes.
int s = 7;

result = ::myfind(nums, nums + end, s);

the implementation goes like:

int* myfind(int* arr_start, int* arr_end, int& s) {
int arr_size = (arr_end - arr_start)+1;
int* result = arr_end;

for (int i = 0; i < arr_size; i++) {
if (*(arr_start+i) == s)
{
result = arr_start+i;
}
}

return result;

}

I have been told that elements in an array does not necessary lie after
each other in memory. So if I wanted a char array or string array
instead I guess the above function would not work?

In that case how do I make sure that I access the right elements in the
right order in an array and that I can calculate its size with:

int arr_size = (arr_end - arr_start)+1;
What padding a particular compiler might give to some type is not your
concern.
sizeof() will return the size including the padding, if any.
Both arrays and std::vector hold elements in contiguous memory space.
The problem is you can't assume that an integer has 4 bytes, let the
compiler/platform decide that.
On mine, its 8 bytes (but i don't care).

The term 'end' does not depict the last element, it points to one past
the last element.
So once you've reached the end, you are no longer in the container.
back() usually returns the last element.

You are aware that your array has 8 elements, right? Its got no 6.
Replace all T's with int if templates disturb you.

#include <iostream>

template< typename T >
T* find_ptr(T* const pbegin, T* const pend, const T& t)
{
size_t size = pend - pbegin;
std::cout << "size = " << size;
std::cout << std::endl;
for(T* p = pbegin; p != pend; ++p)
{
if(t == *p)
return p;
}
return pend;
}

int main()
{
int nums[] = { 1, 2, 3, 4, 5, 7, 8, 9 };
// p_end is one past the end of container
int* const p_end = &nums[ sizeof(nums)/sizeof(int) ];

int* p_result = find_ptr(&nums[0], p_end, 7);
// if p_end wasn't returned, something was found
if( p_end != p_result )
{
std::cout << "found at: " << p_result;
std::cout << "\tvalue: " << *p_result;
std::cout << std::endl;
} else {
std::cout << "p_end reached, element not found.\n";
}
}

/*
size = 8
found at: 0x7fff7ef0e554 value: 7
*/

/* another way... pass the whole array by reference
template< typename T, const size_t Size >
T* find_ref(T (& array)[ Size ], const T& t)
{
size_t size = sizeof(array)/sizeof(T);
std::cout << "size = " << size;
std::cout << std::endl;
for(size_t i = 0; i < size; ++i)
{
if(t == array[i])
return &array[i];
}
// one past the last element
return &array[Size];
}
*/
Apr 26 '07 #5
On Thu, 26 Apr 2007 16:52:03 +0200, desktop <ff*@sss.comwrote in
comp.lang.c++:
I have a function that takes two pointers to an array. The first point
to the first element while the other points to the last element.
int nums[] = { 1, 2, 3, 4, 5, 7, 8, 9};
int* result;
int end = (sizeof(nums)/4)-1;
This is a very, very dumb thing to do. What if you compile this on a
platform where sizeof(int) is not 4? There are platforms in use today
where it is 1, 2, or 8, and quite possible others.

Much, much smarter to do:

int end = (sizeof nums / sizeof *nums);

Then if you decide to change nums from an array of int to an array of
short or long or double, you won't have to touch this line.
int s = 7;

result = ::myfind(nums, nums + end, s);

the implementation goes like:
int* myfind(int* arr_start, int* arr_end, int& s) {
int arr_size = (arr_end - arr_start)+1;
int* result = arr_end;

for (int i = 0; i < arr_size; i++) {
if (*(arr_start+i) == s)
{
result = arr_start+i;
}
}

return result;

}

I have been told that elements in an array does not necessary lie after
each other in memory. So if I wanted a char array or string array
instead I guess the above function would not work?
Who told you this? What are the qualifications of this person that
you should believe him/her? If the person actually said this, and you
are not just misinterpreting what he/she said, than the person is very
unqualified to be considered an authority on C++.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Apr 27 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
4
by: Scott Lyons | last post by:
Hey all, Can someone help me figure out how to pass a dynamic array into a function? Its been giving me some trouble, and my textbook of course doesnt cover the issue. Its probably something...
12
by: Susan Cranford | last post by:
Please forgive, I have looked at so much info I can't figure out how to put it together even though I know it must be fairly simple. I have an array of input text boxes (txtDOBn) where n is...
11
by: Edd | last post by:
Hello all, I've made a data structure and an associated set of functions to enable me to store a dynamically-sized array of elements of whatever data type I like. Well that's the idea anyway......
15
by: Daniel Rudy | last post by:
Hello, Consider the following code: /* resolve_hostname this resolves the hostname into an ip address. */ static void resolve_hostname(char result, const char hostname, const char server) {
89
by: Cuthbert | last post by:
After compiling the source code with gcc v.4.1.1, I got a warning message: "/tmp/ccixzSIL.o: In function 'main';ex.c: (.text+0x9a): warning: the 'gets' function is dangerous and should not be...
5
by: Szabolcs | last post by:
I am looking for a way to generalize the function template below, so that it will work with any type, not just double. Is this at all possible in C++? I'd like to replace double (*fun)(double)...
26
by: aruna.mysore | last post by:
Hi all, I have a specific problem passing a function pointer array as a parameter to a function. I am trying to use a function which takes a function pointer array as an argument. I am too sure...
5
by: Immortal Nephi | last post by:
I would like to design an object using class. How can this class contain 10 member functions. Put 10 member functions into member function pointer array. One member function uses switch to call...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.