Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can somebody tell me how to initialize a variable using malloc, when I do not know the actual size required. For now I'm using :
int* a = (int*)malloc(100* sizeof(int));

I'm only assigning 100 temporarily.How do I initialise without using the size?
Posted
Updated 5-Dec-11 4:18am
v2

You can't - malloc needs to know how many bytes you want it to allocate - or it couldn't be sure to fit it into an available memory block.
 
Share this answer
 
You mean you don't know the actual size ahead of time? Both malloc() and new support the use of variables as input.
//Example...
int *a = (int*) malloc(x*sizeof(int));
//Where x can be any positive integer


edit:
by the way... if you don't know the size because it's going to keep changing, then you can use something with a dynamic size like a list instead of a fixed size array.
 
Share this answer
 
v2
Comments
Sumal.V 5-Dec-11 10:35am    
Phew! lists!!!! They are all big terms for me....
Yup will try that. Cheers Albert
Albert Holguin 5-Dec-11 11:14am    
lol, there's a lot to learn but it becomes easier with time and practice
Albert Holguin 5-Dec-11 11:22am    
btw, a "linked list" is a concept... there are several implementations that you can use, for example, MFC has a CList or a CArray (their array also allows you to add/remove things dynamically), STL has a list, Boost has a list, or you can always make your own.
Sumal.V 5-Dec-11 11:27am    
Oh! Yes I have a c++ book by Sams(old one). Found all these concepts! Will take me a while, until then have to somehow handle my manager ;)
Legor 7-Dec-11 8:32am    
If you're free to use all features of C++ just use a std::vector which is an easy to use implementation of a linked list.
One of the thing I will never understand is why who is learning C++ is always tempted (or directed to) learn as it is C.

C++ support classes and abstract types and has a standard library whose specification is part of the language and that should be studied together with the language.

Give a look to std::vector[^].

All you need is a
std::vector<int> a;


and then, every time you have a value to insert in it, just call
a.push_back(your_value);


You can then know the size with a.size() and access the values as usual as a[x].
 
Share this answer
 
v2
Comments
Niklas L 6-Dec-11 5:24am    
Thank you! I cannot understand why people insist on using new (not to mention malloc!) instead of standard library classes, when learning C++. I suppose the literature is of poor quality in general.
Sumal.V 6-Dec-11 5:49am    
Okay, But I replaced my array:-
int* a = (int*)malloc(100* sizeof(int)); to
std::list< int > a;

But again when I use this in my loop, as a[], I'm getting some error!
Emilio Garavaglia 6-Dec-11 7:42am    
Because there is a difference between vector and list.
Vectors are meant for "by end growing" and "random access by index", while list are meant for "random insertion" and "sequential access". There is no operator[] in list-s.

Jut read about std::vector and std::list documentation!
Legor 7-Dec-11 8:34am    
Totally agreed. It's mysterious to me why everyone mingles with ANSI-C dynamic memory when they have the possibility to use STL types.
Hi,


You can't initialize without using the size.
You need to init it with a size then expand you buffer on your need.
You need to use realloc function to expand your memory buffer :

MSDN realloc[^]


[EDIT]
you should have a kind of linked list but with different data type ... look at this example it could inspire you :
linked list[^]


Best regards.
EL GAABEB.
 
Share this answer
 
v4
Comments
Sumal.V 5-Dec-11 10:36am    
Thanks
elgaabeb 5-Dec-11 10:37am    
You are welcome :).
I assume you want an array of ints?
Assumming an int is 4 bytes, you just allocated 400 bytes to a array of 100 ints.

Now beyond the obvious, the answer is, "it depends".

Sometimes you have to loop through your data and calculate how much you need.
Ex, concatenating two strings into one, you need to get the length of both strings, allocate a buffer large enough to hold both, then copy both in appropriately.

Some APIs have functions you call once to get the size, allocate the amount of data you were given, then call the function again to get the data out.

if you want to allocate on the byte level, allocate char instead.

Unless you get more specific, I am not sure how else to help you.
 
Share this answer
 
Comments
Sumal.V 5-Dec-11 10:33am    
Thanks Paul,
Well I'm using this variable to accept the values dynamically.Because the data I'm dealing with ranges from zero to many. Its a real time situation. Hence I've considered a temporary size now for testing purpose. How do I do that?
elgaabeb 5-Dec-11 10:40am    
Could you tell as from where you are receving data ?? from socket??
Sumal.V 5-Dec-11 10:45am    
Its from the sea bed.... the data from from the side scan images..I use these variables for calculating the running total.
elgaabeb 5-Dec-11 11:04am    
In this case I suggest you a List or structure which is growing dependings on your need ... look at Albert response it is interesting and I'll try to update my response with an article to have an idea about that...
Sumal.V 5-Dec-11 11:08am    
Yeah I will try that.. I basically shifted from Java to c++ and so have lil problems. And fresh graduate working under experienced people. Phew! Its a good experience but hard sometimes.
Thanks for your help!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900