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

Multiple malloc in the same pointer

Hello,

I'm using malloc several times in the same pointer, such as:

int i;
my_structure_t* my_structure

while(true)
{
my_structure = malloc(sizeof(my_structure_t));
printf("%d\n", my_structure);
}

But I'm finding out that, no matter how many times I call malloc in
the pointer, every time my pointer points to the same memory position.

Is this correct or am I speaking nonsense here? If I'm correct, how
can I do to allocate new memory on each call?

I'm using gcc on linux.

Regards,

André

Aug 21 '07 #1
12 2498
On 21 Aug, 14:14, André Wagner <andre....@gmail.comwrote:
Hello,

I'm using malloc several times in the same pointer, such as:

int i;
my_structure_t* my_structure

while(true)
{
my_structure = malloc(sizeof(my_structure_t));
printf("%d\n", my_structure);

}

But I'm finding out that, no matter how many times I call malloc in
the pointer, every time my pointer points to the same memory position.

Is this correct or am I speaking nonsense here? If I'm correct, how
can I do to allocate new memory on each call?

I'm using gcc on linux.

Regards,

André
You have promised printf() an integer, but
you are actually passing it a pointer.

It is quite plausible that all the pointers are
different, but whatever part of them printf()
is interpreting as an integer is the same.
--

Aug 21 '07 #2
André Wagner wrote:
Hello,

I'm using malloc several times in the same pointer, such as:
Don't tell us "such as", show us the smallest self-contained program
which demonstrates the problem.
int i;
my_structure_t* my_structure

while(true)
{
my_structure = malloc(sizeof(my_structure_t));
printf("%d\n", my_structure);
}
%d is not the correct printf mask for a pointer (and I'm fairly sure
that it should be a void pointer that you pass to printf). Ignoring the
void pointer issue, if, for example, you have 64-bit pointers and 32-bit
ints, this would probably show the same value for the 32-bits of the
pointer that happened to be printed...

Correct the mask (and pass void *) for a start and then show us a
minimal program demonstrating the problem if it still seems wrong.
Aug 21 '07 #3
But I'm finding out that, no matter how many times I call malloc in
the pointer, every time my pointer points to the same memory position.

You have promised printf() an integer, but
you are actually passing it a pointer.

It is quite plausible that all the pointers are
different, but whatever part of them printf()
is interpreting as an integer is the same.
That was a good guess (I haven't thought of that) but I changed the %d
to %p, and still shows the same address.

André

Aug 21 '07 #4
André Wagner wrote:
Hello,

I'm using malloc several times in the same pointer, such as:

int i;
my_structure_t* my_structure

while(true)
{
my_structure = malloc(sizeof(my_structure_t));
Better might be:
my_structure = malloc(sizeof *my_structure);
printf("%d\n", my_structure);
Do:
printf("%p\n", (void *)my_structure);

And see again.

<snip>

Aug 21 '07 #5
I already found out, it was a bug in my program. I was freeing the
memory somewhere, so malloc was just taking the same memory position
over and over again.

I am sorry for the inconvenience.

André

On 21 ago, 10:29, André Wagner <andre....@gmail.comwrote:
But I'm finding out that, no matter how many times I call malloc in
the pointer, every time my pointer points to the same memory position.
You have promised printf() an integer, but
you are actually passing it a pointer.
It is quite plausible that all the pointers are
different, but whatever part of them printf()
is interpreting as an integer is the same.

That was a good guess (I haven't thought of that) but I changed the %d
to %p, and still shows the same address.

André

Aug 21 '07 #6
André Wagner wrote:
But I'm finding out that, no matter how many times I call malloc in
the pointer, every time my pointer points to the same memory position.

You have promised printf() an integer, but
you are actually passing it a pointer.

It is quite plausible that all the pointers are
different, but whatever part of them printf()
is interpreting as an integer is the same.

That was a good guess (I haven't thought of that) but I changed the %d
to %p, and still shows the same address.
Show us /the actual code/. If you don't understand what's going wrong,
you don't understand which bits matter, so you must show us /exactly/
what you wrote.

--
Chris "insert Narnia quote here" Dollin

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

Aug 21 '07 #7
André Wagner wrote:
>>But I'm finding out that, no matter how many times I call malloc in
the pointer, every time my pointer points to the same memory position.
You have promised printf() an integer, but
you are actually passing it a pointer.

It is quite plausible that all the pointers are
different, but whatever part of them printf()
is interpreting as an integer is the same.

That was a good guess (I haven't thought of that) but I changed the %d
to %p, and still shows the same address.
It didn't do that for a small test that I just ran. Show us some real
code which demonstrates the problem...
Aug 21 '07 #8
André Wagner wrote:
I already found out, it was a bug in my program. I was freeing the
memory somewhere, so malloc was just taking the same memory position
over and over again.
So the code which showed the problem was not like the code you posted?

This shows why we keep asking for the smallest compilable program that
shows the problem -

1) in producing that program you'd very likely debug the issue anyway

2) if there is still a program, there's a chance we can find it, but
only with a real program not some vague hints at what the program may
(or may not) look like.
Aug 21 '07 #9
André Wagner wrote:
I already found out, it was a bug in my program.

Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or:
<http://www.caliburn.nl/topposting.html>
Aug 21 '07 #10
On Aug 22, 1:35 am, André Wagner <andre....@gmail.comwrote:
while(true)
{
my_structure = malloc(sizeof(my_structure_t));
printf("%d\n", my_structure);
}
I already found out, it was a bug in my program. I was freeing the
memory somewhere, so malloc was just taking the same memory position
over and over again.
Even if the code was as posted, you could still get the
same address every time, if the compiler is smart enough
to notice that the value of 'my_structure' is no longer
accessible after each loop iteration.

Aug 21 '07 #11
On Tue, 21 Aug 2007 15:48:29 -0700, Old Wolf <ol*****@inspire.net.nz>
wrote:
>On Aug 22, 1:35 am, André Wagner <andre....@gmail.comwrote:
>while(true)
{
my_structure = malloc(sizeof(my_structure_t));
printf("%d\n", my_structure);
}
I already found out, it was a bug in my program. I was freeing the
memory somewhere, so malloc was just taking the same memory position
over and over again.

Even if the code was as posted, you could still get the
same address every time, if the compiler is smart enough
to notice that the value of 'my_structure' is no longer
accessible after each loop iteration.
That would seem to violate the requirement that allocated memory
remain allocated until explicitly freed. There doesn't seem to be an
exception for memory no longer accessed. Or would this be allowed
under the "as if" rule? But then, what about someone trying to test
if malloc really returns NULL on failure by repeatedly allocating
memory until he runs out?
Remove del for email
Aug 25 '07 #12
In article <gg********************************@4ax.com>,
Barry Schwarz <sc******@doezl.netwrote:
>That would seem to violate the requirement that allocated memory
remain allocated until explicitly freed. There doesn't seem to be an
exception for memory no longer accessed. Or would this be allowed
under the "as if" rule?
Yes, you can't tell whether it's still allocated. I suppose you could
argue that you *can* tell, because if it was still allocated you be
bound to run out of memory eventually, but I don't think it's the
intent of the standard to prevent optimisations that you can detect
because they make things run better.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Aug 25 '07 #13

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

Similar topics

19
by: john smith | last post by:
Can someone please explain to me what is happening when I do a malloc(0). This is what I did. int* p = (int*)malloc(0); Then I printed the value of p and of course it was non-null. But...
9
by: WL | last post by:
Hey, all. I'm creating an array of strings (char **argv style) on the fly, and using realloc to create string pointers, and malloc for the strings itself (if that makes any sense). I'm using the...
9
by: Richard Hunt | last post by:
What is the best way to get two values back from a function? I am working through 'The C Programming Language', but I felt like taking a bit of time off to write another program. The program...
7
by: Rano | last post by:
/* Hello, I've got some troubles with a stupid program... In fact, I just start with the C language and sometime I don't understand how I really have to use malloc. I've readden the FAQ...
32
by: Clunixchit | last post by:
How can i read lines of a file and place each line read in an array? for exemple; array=line1 array=line2 ...
19
by: santosh | last post by:
Hi all, In the following program I allocate a block of pointers to type char, initialised to zero. I then point each of those pointers to a block of allocated memory of fixed size (33 bytes). A...
13
by: a.zeevi | last post by:
free() multiple allocation error in C ==================================== Hi! I have written a program in C on PC with Windows 2000 in a Visual C environment. I have an error in freeing...
68
by: James Dow Allen | last post by:
The gcc compiler treats malloc() specially! I have no particular question, but it might be fun to hear from anyone who knows about gcc's special behavior. Some may find this post interesting;...
25
by: jbholman | last post by:
I am pretty new to C and doing my first project in C. I actually read almost the entire FAQ, but can't seem to figure out this problem. I have a structure. I have a list of these structures. ...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.