473,387 Members | 1,493 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,387 software developers and data experts.

size of returning pointer

i have a fn of type
short* funname (char*)

when i call this function it returns short array
how to find the the size of that returned array
Apr 4 '08 #1
9 2193
DumRat
93
i have a fn of type
short* funname (char*)

when i call this function it returns short array
how to find the the size of that returned array
You can't. Return an object instead. Or pass another param by reference
to the function to get the size. I don't know which is the better of these two methods.
Apr 4 '08 #2
mac11
256 100+
Another common way of doing it (especially in C) is to supply the buffer and size of that buffer to the function and have it just fill the buffer rather than allocate it.

Also, this way the caller is less likely to forget that he needs to deallocate the buffer.
Apr 4 '08 #3
Studlyami
464 Expert 256MB
Creating an array in a function then returning the array is dangerous. The array will go out of scope as soon as the function ends, so there is no guarantee that the pointer will still be pointing to valid data. If you need to fill an array in the function pass the array into the function and use it.
Apr 4 '08 #4
can can declare that array as global variable?so that it's scope never ends with the function...
Apr 5 '08 #5
Studlyami
464 Expert 256MB
can can declare that array as global variable?so that it's scope never ends with the function...
you COULD, but shouldn't. Before weakness gets to post this link i will.
Apr 5 '08 #6
Ganon11
3,652 Expert 2GB
If you allocate memory for the array (using malloc/new/whatever) and return a pointer to it, it should remain valid in any scope. Just remember to free()/delete that pointer eventually.
Apr 5 '08 #7
Savage
1,764 Expert 1GB
If you allocate memory for the array (using malloc/new/whatever) and return a pointer to it, it should remain valid in any scope. Just remember to free()/delete that pointer eventually.
How it should?

e.g

Expand|Select|Wrap|Line Numbers
  1. void *Func(void)
  2. {
  3.  void *p=malloc(10);
  4.  return p;
  5. }
Here p is in data zone of Func,but it points to the beginning of some memory segment on the heap,when function exits data zone is deallocated and therefore
p is no more.How are you going to find address of that memory block?
Apr 5 '08 #8
Ganon11
3,652 Expert 2GB
You're right that p doesn't exist, but your return value will carry the value of p back to wherever it was called. What is the value of p? The address of that block of memory. So the block of memory was created and still exists, and you can get the address of it by capturing the return value of your function, but p itself no longer exists (remember p was just a local variable allocated on the stack).
Apr 5 '08 #9
Studlyami
464 Expert 256MB
your return value will carry the value of p back to wherever it was called. What is the value of p? The address of that block of memory. So the block of memory was created and still exists,
After the return you know where the structure begins, but you don't know how many values are in it. In order to avoid that you will need to do what DumRat suggested and pass in an int by reference to get the size of the array.
Apr 7 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Cheng Mo | last post by:
I tried in g++ sizeof(int*) siezof(long*) sizeof(Foo*)//Foo is a self-defined class They all has same size. Do all pointers have same size in a given platform with given compiler? Thanks &...
5
by: theAnswer | last post by:
hello, Is there a way to implement a little function to return the size of an array (not a string)? Something like int size(int *array) thanx
3
by: mandark_br | last post by:
I have a function that returns a int pointer to a allocated memory. How I know the size of this allocated memory? Compiler = gcc Ex.: int main(void) {
54
by: Neo | last post by:
Hi Folks, I've a simple qestion related to dynamic memory allocation in C here is the code: #include <stdio.h> int main() {
31
by: bilbothebagginsbab5 AT freenet DOT de | last post by:
Hello, hello. So. I've read what I could find on google(groups) for this, also the faq of comp.lang.c. But still I do not understand why there is not standard method to "(...) query the...
11
by: Sontu | last post by:
Consider the following code: int main(void) { char buffer; func(buffer); } void func(char *bufpas) {
7
by: serrand | last post by:
Hello all, i wonder if we can know the size of a variable from a pointer : static void *my_free_hook (void *ptr, const void *caller) { size_t sz; /* bla bla bla */ /* HOW CAN I DETERMINE...
29
by: Vasileios Zografos | last post by:
Hi everyone, I need to build a function to plug it in a program (that I didnt make or can change) that should be called something like this: float someFunction(float x) { ...
4
by: Kenneth Brody | last post by:
I looked at my copy of n1124, and I didn't see anything about this particular situation... What happens if you realloc() to a size of zero? Implementations are allowed to return NULL on...
33
by: Adam Chapman | last post by:
Hi, Im trying to migrate from programming in Matlab over to C. Im trying to make a simple function to multiply one matrix by the other. I've realised that C can't determine the size of a 2d...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
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
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...

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.