-1

I have the following struct

struct NETWORK_ENDPOINT {
  unsigned char Type;
  unsigned char Protocol;
  unsigned char IPv4[IPV4SIZE + 1];
  unsigned int PortNumber;

  unsigned char SocketIndex;
  unsigned char RESERVED;
  unsigned char *InboundData;
  unsigned int InboundDataSize;

  unsigned char *OutboundData;
  unsigned int OutboundDataSize;
};

In the code I'm allocating with :

struct NETWORK_ENDPOINT *Endpoint = malloc(sizeof(struct NETWORK_ENDPOINT));

Then later in the code I'm allocating the OutboundData with.

Endpoint->OutboundData = malloc(20); // malloc() size may vary, 
                                     // but in the problem situation it is 20

Then I do :

memcpy(Endpoint->OutboundData, Data, 20); 

Then the problem : From the debugger I can see that Endpoint is given address @0x1fd6, and the OutboundData is given address @0x1fca, so only 12 between. Shouldn't is be atleast 20 ?

The memcpy() function then will fill out in OutboundData ( can see in memory that data is correctly placed ), but once it passes 12 bytes, it will begin overwriting the start of the struct Endpoint, corrupting the Type and Protocol and half the IP, thereby making it useless afterwards.

anyone got any idea what I'm going wrong here ? Been working on this for days now, but whatever I try it does not fix this issue...

Have tried to increase the HEAP size, but it seems to stay at 12 bytes between the two memory locations no matter what I do..

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Aune
  • 245
  • 1
  • 4
  • 9
  • Warning: when calling `sizeof` in `malloc` (and the like) [you should always write it](http://stackoverflow.com/a/17258659/1151654) as `ptr = malloc(sizeof(*ptr) * ...);` instead of `ptr = malloc(sizeof(ptrtype*) * ...);`. – Eregrith May 11 '15 at 09:39
  • I think this question needs a better title. – Sourav Ghosh May 11 '15 at 09:43

1 Answers1

5

OutboundData is given address @0x1fca, so only 12 between

Why you're interested in the address of OutboundData?

After malloc(), you should be checking the value of OutboundData, however, you won't be knowing the size of allocated memory thr' this.

Just to be clear, you're not copying to the address of OutboundData, rather , you are copying to the address pointed by OutboundData.

Then,

but once it passes 12 bytes, it will begin overwriting the start of the struct Endpoint, corrupting the Type and Protocol and half the IP, thereby making it useless afterwards.

No, it won't. The value and address of OutboundData are different and the value of OutboundData is used in memcpy().

IMHO, as long as

  • malloc() is success
  • Data is atleast of size 20 (for this case)

your memcpy() should work fine.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • Reason for mentioning the address of OutboundData, is that it looks like malloc allocates the OutboundData to close to the memory previously allocated to the endpoint struct. After OutboundData = malloc..., Outbound data get's a value ( aka allocation successfull ? ). Endpoint->Outbound data should give me the address ? (*Endpoint).Outbound data = address, *(*Endpoint).OutboundData = first value byte ? – Aune May 11 '15 at 10:11
  • Or simpler put: unsigned char *A; then A = address, and *A = value ? – Aune May 11 '15 at 10:33
  • @user1244472 Second comment is perfect. – Sourav Ghosh May 11 '15 at 10:36
  • Then should not the same apply to the given problem ? So that (*Endpoint).OutboundData = Address `? or Endpoint->OutboundData = Address ? – Aune May 11 '15 at 10:37
  • @user1244472 think of it this way --> the return value of `malloc()` is stored in `Endpoint->OutboundData`, now, what does `malloc()` return? – Sourav Ghosh May 11 '15 at 10:41
  • You mentioned in your previous answer that "The value and address of OutboundData are different and the value of OutboundData is used in memcpy().", I read this as I'm not using the correct notation in the memcpy. But I thought that Endpoint->OutboundData was the address pointed to by the Endpoint->OutboundData pointer , not the value ? Or did I misunderstand your meaning ? – Aune May 11 '15 at 10:43
  • @user1244472 I think I'm the one not able to explain properly. Actually I'm not a native english speaker, so sometimes I mess up. What itried to mean is in the case, `char *p`, `&p` is of type `char **`, `p` is of type `char *` and `*p` is of type `char`. Hope this make things clear. :-) – Sourav Ghosh May 11 '15 at 10:47
  • malloc returns a pointer ( void *), that is why I thought that Endpoint->OutboundData would contain a pointer to the memory allocated, thus I can use this in the memcpy as the address of the dest * argument – Aune May 11 '15 at 10:47
  • @user1244472 that is what i'm saying, your code is fine. There is no logical errors, atleast. The error you're thinking, is not an error in this case, at all. – Sourav Ghosh May 11 '15 at 10:48
  • Your explanations are just fine, I'm just not getting it :) Tried this &Endpoint->OutboundData and it worked, but I just can't get my mind wrapped around why as I thought that when you use ptr = malloc, the ptr would already contain the address and the & would not be necessary.. – Aune May 11 '15 at 10:51