Community
cancel
Showing results for 
Search instead for 
Did you mean: 
AARAV
Beginner
951 Views

Edison malloc problem?

I'm trying to test the UART code on the Edison from the mraa library. As a part of the code, I'm declaring a character array and mallocing it. However, something weird is happening.

No matter what size I try to malloc, the size of the array is returned by the sizeof() function is always 4. I don't know if this is a problem with memory management by the kernel or if something is wrong with the sizeof() function.

/*

* Author: Thomas Ingleby <</span>mailto:thomas.c.ingleby@intel.com thomas.c.ingleby@intel.com>

* Author: Brendan Le Foll <</span>mailto:brendan.le.foll@intel.com brendan.le.foll@intel.com>

* Copyright (c) 2014, 2015 Intel Corporation.

*

* Permission is hereby granted, free of charge, to any person obtaining

* a copy of this software and associated documentation files (the

* "Software"), to deal in the Software without restriction, including

* without limitation the rights to use, copy, modify, merge, publish,

* distribute, sublicense, and/or sell copies of the Software, and to

* permit persons to whom the Software is furnished to do so, subject to

* the following conditions:

*

* The above copyright notice and this permission notice shall be

* included in all copies or substantial portions of the Software.

*

* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,

* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF

* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND

* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE

* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION

* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION

* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

*/

# include "stdio.h"

# include "mraa.h"

# include

# define num_char 7

int main(int argc, char** argv)

{

mraa_uart_context uart;

uart = mraa_uart_init(0);

mraa_uart_set_baudrate(uart,9600);

if (uart == NULL) {

fprintf(stderr, "UART failed to setup\n");

return EXIT_FAILURE;

}

char* buffer;

buffer = (char*)(malloc(num_char));

//memset(buffer, '\0', sizeof(buffer));

printf("Size:%d\n",sizeof(buffer));

/*while(1)

{

mraa_uart_write(uart,"a",1);

mraa_uart_read(uart, buffer, num_char);

printf("%s\n",buffer);

sleep(1);

}*/

mraa_uart_stop(uart);

mraa_deinit();

return EXIT_SUCCESS;

}

Thanks in advance,

Aravind

0 Kudos
5 Replies
idata
Community Manager
49 Views

Hi Aravind,

Thank you for contacting us!

It seems to be related to how you are using the function sizeof(), in this case you are printing the size of the pointer (char * buffer), so, that will be 4. I'd suggest you try these:

Char * buffer; //to declare a pointer

 

buffer = (char*) malloc(num_char); //Reserve the quantity of memory (bytes) specified by num_char

 

if (buffer == NULL){ //check if the memory was reserved

 

printf("The memory hasn't been reserved");

 

}

 

printf("Pointer address: %d \n", bufffer); //print the address of the memory location

 

printf("Memory contain: %d \n", *buffer) //print the contained in the memory

Hope this information helps, if you need more help, please let me know.

Regards,

 

-Yermi
idata
Community Manager
49 Views

Hi Aravind,

I would like to know if you have tried my previous suggestions. If you need more assistance, please let me know.

Regards,

 

-Yermi

 

AARAV
Beginner
49 Views

Hi Yermi,

I haven't had time to test it out yet. I'll post the result on this thread once I'm able to.

Thanks,

Aravind

idata
Community Manager
49 Views

Hi Aravind,

 

 

Great! We'll be waiting for you. Have a nice day!

 

 

Regards,

 

-Yermi

 

idata
Community Manager
49 Views

Hi Aravind,

We would like to know if you have had time to test it, and also, if you need more help, please let us know.

Regards,

 

-Yermi

 

Reply