I used to think that i knew to program in C but this problem is making
me thing otherwise.
i'm trying to do something trivial, i suppose.
i have this struct:
typedef struct{
int socket;
char ip[16];
}peers;
in a main() i malloc this way:
Peers=malloc(si zeof(peers));
Then, my program would eventually want to save more "peers" so
i said, why dont i Realloc this thing x2 x3 x4 x5 and so on.. as
needed. So i created this function:
peers* ingresaripsock( peers* lista,int sock, char ip[16],int *tm)
{
int offset= (*tm)++;
if (offset>0) lista = realloc(lista,o ffset * sizeof(peers)+1 );
strcpy((lista + offset)->ip, ip);
(lista + offset)-> socket=sock;
return lista;
}
i print the results with:
void imprimirpeers(p eers* lista, int tm){
int i=0;
for(i=0;i<tm;i+ +){
printf("Ip: %s - Sock: %d\n",(lista + i) ->ip , (lista + i)
->socket);
}
}
but when i printf the Results, it's all weird data. I believe the
problem is around the Realloc becuase i tried this code but instead of
using realloc i malloc'd from the first time with space for like 20
positions (commented the realloc part in the function) and it worked
just fine. But i want to use what i Really need, dont want to do malloc
( 20000000) and use a couple of bytes...
Please i really need some help- 18 2344 if*****@gmail.c om wrote: if (offset>0) lista = realloc(lista,o ffset * sizeof(peers)+1 );
This is a severe mistake. Never use the form
ptr = realloc(ptr, size);
Instead have the results of the realloc() assigned to a temporary
variable to give you at least the option of what to do if realloc fails.
yes, after writing the post, i read some of that, i'm switchin to a
pointer to pointer data structure. hope it works that way.
Thanks.
In article <11************ **********@o13g 2000cwo.googleg roups.com>, if*****@gmail.c om wrote: I used to think that i knew to program in C but this problem is making me thing otherwise. i'm trying to do something trivial, i suppose. i have this struct:
typedef struct{ int socket; char ip[16]; }peers;
in a main() i malloc this way: Peers=malloc(si zeof(peers)); Then, my program would eventually want to save more "peers" so i said, why dont i Realloc this thing x2 x3 x4 x5 and so on.. as needed. So i created this function:
peers* ingresaripsock( peers* lista,int sock, char ip[16],int *tm) { int offset= (*tm)++;
if (offset>0) lista = realloc(lista,o ffset * sizeof(peers)+1 );
Your calculation how many bytes are needed looks interesting.
On 10 Oct 2005 14:06:47 -0700, if*****@gmail.c om wrote: I used to think that i knew to program in C but this problem is making me thing otherwise. i'm trying to do something trivial, i suppose. i have this struct:
typedef struct{ int socket; char ip[16]; }peers;
in a main() i malloc this way: Peers=malloc(s izeof(peers)); Then, my program would eventually want to save more "peers" so i said, why dont i Realloc this thing x2 x3 x4 x5 and so on.. as needed. So i created this function:
peers* ingresaripsock( peers* lista,int sock, char ip[16],int *tm)
Is *tm a count of how may structures your currently have space for?
{ int offset= (*tm)++;
offset is the original value of *tm. if (offset>0) lista = realloc(lista,o ffset * sizeof(peers)+1 );
What did you think the +1 does. What you have done, if realloc
succeeds, is allocate space for the same number of structures plus one
more byte. You did not allocate space for an extra struct. Did you
really mean
... = realloc(lista, (offset+1) * sizeof(peers)); strcpy((list a + offset)->ip, ip);
As it stands now, this invokes undefined behavior. lista points to an
allocated area capable of holding offset structures. The structures
are numbered 0, 1, ..., offset-1. You are now writing beyond the end
of your allocated area.
(lista + offset)-> socket=sock;
return lista; }
i print the results with:
void imprimirpeers(p eers* lista, int tm){ int i=0;
for(i=0;i<tm;i ++){ printf("Ip: %s - Sock: %d\n",(lista + i) ->ip , (lista + i) ->socket); } }
but when i printf the Results, it's all weird data. I believe the problem is around the Realloc becuase i tried this code but instead of using realloc i malloc'd from the first time with space for like 20 positions (commented the realloc part in the function) and it worked just fine. But i want to use what i Really need, dont want to do malloc ( 20000000) and use a couple of bytes...
Please i really need some help-
<<Remove the del for email>>
Thank you all for all the feedback,
now i have a pointer to pointer structure.
typedef struct{
int socket;
char id[16];
}reqs;
reqs** ingresaridsock( reqs** lista,int sock, char id[16],int *tm){
int i;
reqs* aux;
reqs **temp;
int offset= (*tm)++;
aux = malloc(sizeof(r eqs));
if (offset > 0 ){
if ((temp = realloc(lista, (offset+1) * sizeof(*lista)) )==NULL)
exit(1);
lista=temp;}
strcpy(aux->id, id);
aux->socket= sock;
lista[offset]= aux;
return lista;
Now, it seems to work just fine. BUT!, i add 1,2,3,4 and on the fifth
item,
i get Segmentation Fault! Knowing that i'm not freeing memory as i
should
shouldn't it be possible to store more than 4 miserable items without
freeing?!.
thanks again.
Barry Schwarz ha escrito: On 10 Oct 2005 14:06:47 -0700, if*****@gmail.c om wrote:
I used to think that i knew to program in C but this problem is making me thing otherwise. i'm trying to do something trivial, i suppose. i have this struct:
typedef struct{ int socket; char ip[16]; }peers;
in a main() i malloc this way: Peers=malloc(s izeof(peers)); Then, my program would eventually want to save more "peers" so i said, why dont i Realloc this thing x2 x3 x4 x5 and so on.. as needed. So i created this function:
peers* ingresaripsock( peers* lista,int sock, char ip[16],int *tm)
Is *tm a count of how may structures your currently have space for?
{ int offset= (*tm)++;
offset is the original value of *tm.
if (offset>0) lista = realloc(lista,o ffset * sizeof(peers)+1 );
What did you think the +1 does. What you have done, if realloc succeeds, is allocate space for the same number of structures plus one more byte. You did not allocate space for an extra struct. Did you really mean ... = realloc(lista, (offset+1) * sizeof(peers));
strcpy((list a + offset)->ip, ip);
As it stands now, this invokes undefined behavior. lista points to an allocated area capable of holding offset structures. The structures are numbered 0, 1, ..., offset-1. You are now writing beyond the end of your allocated area.
(lista + offset)-> socket=sock;
return lista; }
i print the results with:
void imprimirpeers(p eers* lista, int tm){ int i=0;
for(i=0;i<tm;i ++){ printf("Ip: %s - Sock: %d\n",(lista + i) ->ip , (lista + i) ->socket); } }
but when i printf the Results, it's all weird data. I believe the problem is around the Realloc becuase i tried this code but instead of using realloc i malloc'd from the first time with space for like 20 positions (commented the realloc part in the function) and it worked just fine. But i want to use what i Really need, dont want to do malloc ( 20000000) and use a couple of bytes...
Please i really need some help-
<<Remove the del for email>>
Here is something new,
if the first time i malloc(15*sizeo f(*Reqs)) it works fine. So i
commented the Realloc part in the function i tried it. But Something i
did not expect ,happened, it didn't just stored 15 items but it seemed
to be "endless", i stored tons of items as if there was no need for a
Realloc. WHY?!.
ifmu...@gmail.c om ha escrito: Thank you all for all the feedback, now i have a pointer to pointer structure.
typedef struct{ int socket; char id[16]; }reqs;
reqs** ingresaridsock( reqs** lista,int sock, char id[16],int *tm){ int i; reqs* aux; reqs **temp; int offset= (*tm)++; aux = malloc(sizeof(r eqs)); if (offset > 0 ){ if ((temp = realloc(lista, (offset+1) * sizeof(*lista)) )==NULL) exit(1); lista=temp;} strcpy(aux->id, id); aux->socket= sock; lista[offset]= aux; return lista;
Now, it seems to work just fine. BUT!, i add 1,2,3,4 and on the fifth item, i get Segmentation Fault! Knowing that i'm not freeing memory as i should shouldn't it be possible to store more than 4 miserable items without freeing?!.
thanks again.
Barry Schwarz ha escrito:
On 10 Oct 2005 14:06:47 -0700, if*****@gmail.c om wrote:
I used to think that i knew to program in C but this problem is making me thing otherwise. i'm trying to do something trivial, i suppose. i have this struct:
typedef struct{ int socket; char ip[16]; }peers;
in a main() i malloc this way: Peers=malloc(s izeof(peers)); Then, my program would eventually want to save more "peers" so i said, why dont i Realloc this thing x2 x3 x4 x5 and so on.. as needed. So i created this function:
peers* ingresaripsock( peers* lista,int sock, char ip[16],int *tm)
Is *tm a count of how may structures your currently have space for?
{ int offset= (*tm)++;
offset is the original value of *tm.
if (offset>0) lista = realloc(lista,o ffset * sizeof(peers)+1 );
What did you think the +1 does. What you have done, if realloc succeeds, is allocate space for the same number of structures plus one more byte. You did not allocate space for an extra struct. Did you really mean ... = realloc(lista, (offset+1) * sizeof(peers));
strcpy((list a + offset)->ip, ip);
As it stands now, this invokes undefined behavior. lista points to an allocated area capable of holding offset structures. The structures are numbered 0, 1, ..., offset-1. You are now writing beyond the end of your allocated area.
(lista + offset)-> socket=sock;
return lista; }
i print the results with:
void imprimirpeers(p eers* lista, int tm){ int i=0;
for(i=0;i<tm;i ++){ printf("Ip: %s - Sock: %d\n",(lista + i) ->ip , (lista + i) ->socket); } }
but when i printf the Results, it's all weird data. I believe the problem is around the Realloc becuase i tried this code but instead of using realloc i malloc'd from the first time with space for like 20 positions (commented the realloc part in the function) and it worked just fine. But i want to use what i Really need, dont want to do malloc ( 20000000) and use a couple of bytes...
Please i really need some help-
<<Remove the del for email>> if*****@gmail.c om wrote: typedef struct{ int socket; char id[16]; }reqs;
reqs** ingresaridsock( reqs** lista,int sock, char id[16],int *tm){ int i; reqs* aux; reqs **temp; int offset= (*tm)++; aux = malloc(sizeof(r eqs)); if (offset > 0 ){ if ((temp = realloc(lista, (offset+1) * sizeof(*lista)) )==NULL) exit(1); lista=temp;}
You never allocate anything when *tm was 0.
If lista were NULL (or an array of size 0), then you would
cause undefined behaviour.
strcpy(aux->id, id); aux->socket= sock; lista[offset]= aux; return lista;
Now, it seems to work just fine. BUT!, i add 1,2,3,4 and on the fifth item, i get Segmentation Fault!
If the above was not the problem, then perhaps you have a buffer
overflow in the strcpy, or some other heap corruption. Can you
post a complete, compilable program that demonstrates the problem?
BTW, please don't top-post ( http://en.wikipedia.org/wiki/Top-posting )
There's no problem about the tm being 0.
the thing with posting the whole thing is that i don't know how you're
gonna test it. I'm testing it with 3 vmwares as clients and 1 vmware as
server which is storing the info.
Again, something that's still bugging me is the "endless" list i'm
getting...
if you want i can mail you the code.
Thanks for the help-
On 10 Oct 2005 18:11:39 -0700, if*****@gmail.c om wrote: Thank you all for all the feedback, now i have a pointer to pointer structure.
typedef struct{ int socket; char id[16]; }reqs;
reqs** ingresaridsock( reqs** lista,int sock, char id[16],int *tm){ int i; reqs* aux; reqs **temp; int offset= (*tm)++; aux = malloc(sizeof(r eqs)); if (offset > 0 ){ if ((temp = realloc(lista, (offset+1) * sizeof(*lista)) )==NULL) exit(1); lista=temp;} strcpy(aux->id, id); aux->socket= sock; lista[offset]= aux; return lista;
Now, it seems to work just fine. BUT!, i add 1,2,3,4 and on the fifth item, i get Segmentation Fault! Knowing that i'm not freeing memory as i should shouldn't it be possible to store more than 4 miserable items without freeing?!.
On which statement does the seg fault occur?
How are you calling this function? Show us main.
<<Remove the del for email>> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Michael |
last post by:
How would I go about shrinking the buffer that was allocated with new, or
expanding it in place? I basically need a realloc equivalent for new.
Thanks in advance.
Michael
|
by: mordac |
last post by:
Hi, writing a heap ADT, need to handle insertion into the heap when it
is full. Attempting to use realloc to do this, but realloc is changing
the contents of my heap! The following is my incHeapSize function,
which is supposed to increase the malloced space for an integer array
by 1:
Heap incHeapSize(Heap aHeap) {
int* tempHeap;
aHeap->maxSize++;
tempHeap = realloc(aHeap->heapArray, aHeap->maxSize);
|
by: Marlene Stebbins |
last post by:
The bigint struct defines a big integer and represents it as a string of
characters:
typedef struct bigint {
int sign;
int size;
int initflag;
char *number;
} bigint;
|
by: Walter Roberson |
last post by:
If realloc() finds it necessary to move the memory block, then
does it free() the previously allocated block?
The C89 standard has some reference to undefined behaviour if one
realloc()'s memory that was freed by realloc(), but the only way
explicitly mentioned in the C89 standard to free memory via realloc()
is to realloc() it down to 0 bytes.
I had always assumed it would automatically free the previous memory,
but is the behaviour...
|
by: bwaichu |
last post by:
Is it generally better to set-up a buffer (fixed sized array) and read
and write to that
buffer even if it is larger than what is being written to it? Or is it
better to allocate memory and realloc it for the size of the what is
being written each time? In other words, what is the decision factor
between deciding to use a fixed size buffer or allocating memory
space and reallocing the size?
I don't think the code below is optimal...
| |
by: ivan.leben |
last post by:
Let's say I have a piece of allocated memory which I want to expand and
reuse if possible or allocate in a different part of RAM if resizing is
not possible, however, in the latter case I don't care about the old
data and I don't need to copy it to the new location. Is there a
standard function that could do that? As far as I understand the
description of the realloc() function, it _always_ copies the data to
the new location, even if that...
|
by: subramanian |
last post by:
I have taken the following prototype from K & R.
void *realloc(void *p, size_t size);
Suppose p was earlier allocated by malloc. Suppose I am calling realloc
with larger size value.
If realloc is successful, will the return pointer be the same as p or
will it be different.
K & R 2nd edition says "realloc returns a pointer to the new space".
|
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 malloc(0), and realloc()
says it reutrns NULL on failure. (And, on failure, the old pointer
has not been freed.)
Is it possible for an implementation to return NULL for realloc(ptr,0)
|
by: Francois Grieu |
last post by:
When running the following code under MinGW, I get
realloc(p,0) returned NULL
Is that a non-conformance?
TIA,
Francois Grieu
#include <stdio.h>
#include <stdlib.h>
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |