LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-08-2013, 11:14 PM   #1
ejspeiro
Member
 
Registered: Feb 2011
Distribution: Ubuntu 14.04 LTS (Trusty Tahr)
Posts: 203

Rep: Reputation: 26
Revisiting writing a "recalloc" function in C++


Hello guys,

in this thread, we discussed an actually solved the issue of how to use the memset function to implement a "recalloc" function, that is, a function that would increase the size of a previously allocated array, but clearing the new positions to 0.

I ported the code to a C++ class, which I compile using the -Werror flag, thus getting the following output:

Code:
mtk_dok_sparse_matrix.cc: In member function ‘int MTK_DOKSparseMatrix::add_value(int, int, double)’:
mtk_dok_sparse_matrix.cc:106:39: error: pointer of type ‘void *’ used in arithmetic [-Werror=pointer-arith]
mtk_dok_sparse_matrix.cc:113:37: error: pointer of type ‘void *’ used in arithmetic [-Werror=pointer-arith]
mtk_dok_sparse_matrix.cc:122:37: error: pointer of type ‘void *’ used in arithmetic [-Werror=pointer-arith]
It was working in my C code, so I would like to revisit this issue and discuss the portability of the approach implemented in the aforementioned original thread (this one).

If the code in the previous thread is not enough, I will gladly post the class and the related method yielding the issue, but I think it should be enough.

Thanks in advanced!

\m/
 
Old 04-09-2013, 02:24 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Typecast 'void *' to 'char *' (or 'inptr_t') before calculating.
 
1 members found this post helpful.
Old 04-09-2013, 10:38 AM   #3
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 139Reputation: 139
In both C and C++, sizeof(void) is undefined, and so pointer arithmetic a void* is impossible. Although GCC provides an extension that treats sizeof(void) as 1, I believe C++ is more strict in saying that this is not allowed.
 
2 members found this post helpful.
Old 04-09-2013, 09:16 PM   #4
ejspeiro
Member
 
Registered: Feb 2011
Distribution: Ubuntu 14.04 LTS (Trusty Tahr)
Posts: 203

Original Poster
Rep: Reputation: 26
Quote:
Originally Posted by NevemTeve View Post
Typecast 'void *' to 'char *' (or 'inptr_t') before calculating.
Wow! So simple! And when read next to:

Quote:
Originally Posted by JohnGraham View Post
In both C and C++, sizeof(void) is undefined, and so pointer arithmetic a void* is impossible. Although GCC provides an extension that treats sizeof(void) as 1, I believe C++ is more strict in saying that this is not allowed.
It all makes sense together! My assumption is that

Code:
sizeof(char) = sizeof(void) // In C.
I will try the code, and hopefully it will work!
 
  


Reply

Tags
c++, void



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Qt "cannot call constructor for""a function-style cast remove the redundant ::QBrush" Peterius Linux - Software 4 11-24-2010 04:57 PM
Replacing "function(x)" with "x" using sed/awk/smth Griffon26 Linux - General 3 11-22-2006 10:47 AM
When "function pointer" meets "template"... I can't get rid of this compiling er cyu021 Linux - Software 3 12-17-2004 07:52 PM
"Function not implemented" error in call to "sem_open()" Krishnendu8 Linux - Newbie 1 06-07-2003 02:52 AM
"Function not imlemented" error in call to "sem_open()" Krishnendu8 Linux - Networking 0 06-07-2003 02:19 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:03 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration